Posts

Showing posts with the label SharePoint - Elevated Privileges

SharePoint - Elevated Privileges

When developing using the SharePoint object model, certain operations has to be run with elevated privileges. For instance, setting information into the property bag of a site needs to be done with elevated privileges. This post shows how this is done, and also includes a helper method for making it easier and shorter to do this. There is a method called  SPSecurity.RunWithElevatedPrivileges  in the object model that takes a delegate as argument. The delegate is then run with more permissions. An important note regarding this: You must not use old references to  SPWeb  or  SPSite  instances inside your delegate, since they are “contaminated” with lower permissions. What you have to do is to create new  SPSite  and  SPWeb  instances using the Ids of the sites and webs you already have a reference to, like this SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite site = new SPSite(SPContext.Current.Site.ID)) ...