Monday 3 December 2012

RunWithElevatedPrivileges in SharePoint

In this article I am going to explain you that How to use RunWithElevatedPrivileges in sharepoint object model.
RunWithElevatedPrivileges
Executes the specified method with Full Control rights even if the user does not otherwise have Full Control.The next example shows the syntax that is required to define an anonymous method in the call to RunWithElevatedPrivileges.




SPSecurity.RunWithElevatedPrivileges(delegate()

{

    // implementation details omitted

});




Detail:-
The RunWithElevatedPrivileges method accepts a delegate parameter that adds a reference to a method that containsthe code that is to be executed with elevated privileges.
SPSite siteColl = SPContext.Current.Site;
SPWeb site = SPContext.Current.Web;
SPSecurity.RunWithElevatedPrivileges(delegate() {
  using (SPSite ElevatedsiteColl = new SPSite(siteColl.ID)) {
    using (SPWeb ElevatedSite = ElevatedsiteColl.OpenWeb(site.ID)) {
      string SiteCollectionOwner = ElevatedsiteColl.Owner.Name;
      string Visits = ElevatedsiteColl.Usage.Visits.ToString();
      string RootAuditEntries =
          ElevatedSite.RootFolder.Audit.GetEntries().Count.ToString();
    }
  }
});

Must read it:
In RunWithElevatedPrivileges you must then create an instance of the SPSite class and the SPWeb class. You cannot use the objects available through the Microsoft.SharePoint.SPContext.Current property. That is because those objects were created in the security context of the current user.

Thanks!
Nikhil S.


Share This!


1 comment:

  1. The Three-Titanium Rocket - Titanium Arts
    Three-Titanium Rocket is a rocket titanium ion color from the edge titanium titanium watch band as an example of what micro touch hair trimmer the titanium dioxide sunscreen rocket uses to generate rockets.

    ReplyDelete

Translate

Total Pageviews

Powered By Blogger · Designed By Seo Blogger Templates