Sharepoint – Run code with elevated rights

Sometimes, visitors or non-admin users need to run a task which need higher rights to perform. Execute specific code to run with Full control even the user does not have it. Common block message is “Access denied”, remarks prohibit executing the codes. For such purpose, Microsoft.SharePoint.dll provides method RunWithElevatedPrivilages.

    using (SPSite site = new SPSite("http://localsite/"))
         using (SPWeb web = site.OpenWeb(site))
            // code to run

Other example below can be used for parameterless methods whose void return:

protected void Button1_Click(object sender, EventArgs e)
   SPSecurity.CodeToRunElevated elevatedGetSitesAndGroups = new SPSecurity.CodeToRunElevated(GetSitesAndGroups);


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s