Using Sharepoint objects (SPSite| SPWeb) in Sharepoint Hosted WCF service

Recently I Implemented and hosted WCF service inside Sharepoint (_vti_bin folder). so when I try to consume list and other sharepoint related object I got some SPSecurity errors. so normally what we do is something like below 


using (SPSite site = new SPSite(ProjectInfo.SiteUrl))
{
// Your code here

}

but this approche will give you an error (SPSecurity is unhandled by user code). so to overcome that issue you can use SPUserToken.SystemAccount when you try to creae a SPSite object or SPWeb object. 


using (SPSite site = new SPSite(ProjectInfo.SiteUrl, SPUserToken.SystemAccount))
{
// Your code here

}

Note : And also use SPSecurity.RunwithElevatedPrivileges to execute specified method with full control rights even if the current user doesn't have full control.

Comments

Popular posts from this blog

Deep dive into OpenXML Part 1

Deep dive into OpenXML Part 2

Convert XML data to dynamic (ExpandoObject) Object