Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
SDK Documentation
SPSecurity Class
SPSecurity Methods
 RunWithElevatedPrivileges Method
Community Content
In this section
Statistics Annotations (3)
Collapse All/Expand All Collapse All
This page is specific to
The 2007 product release

Other versions are also available for the following:
SPSecurity.RunWithElevatedPrivileges Method (Microsoft.SharePoint)
Executes the specified method with Full Control rights even if the user does not otherwise have Full Control.

Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
Visual Basic (Declaration)
<SharePointPermissionAttribute(SecurityAction.Demand, Impersonate:=True)> _
<SharePointPermissionAttribute(SecurityAction.Demand, ObjectModel:=True)> _
Public Shared Sub RunWithElevatedPrivileges ( _
    secureCode As CodeToRunElevated _
)
Visual Basic (Usage)
Dim secureCode As CodeToRunElevated

SPSecurity.RunWithElevatedPrivileges(secureCode)
C#
[SharePointPermissionAttribute(SecurityAction.Demand, Impersonate=true)] 
[SharePointPermissionAttribute(SecurityAction.Demand, ObjectModel=true)] 
public static void RunWithElevatedPrivileges (
    CodeToRunElevated secureCode
)

Parameters

secureCode

A SPSecurity.CodeToRunElevated object that represents a method that is to run with elevated rights.

The secureCode object can be created from any method that is parameterless and returns void. See SPSecurity.CodeToRunElevated.

You can also bypass using the SPSecurity.CodeToRunElevated constructor by defining and anonymous method inside the call to RunWithElevatedPrivileges. See the examples.

The first example shows RunWithElevatedPrivileges used with the SPSecurity.CodeToRunElevated constructor. In this example, GetSitesAndGroups is a parameterless method that returns void and is defined somewhere that can be accessed by the Button1_Click method.

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

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
});

You must create a new SPSite object inside the delegate because SPSite objects created outside do not have Full Control even when referenced inside the delegate. Use the using keyword to ensure that the object is disposed in the delegate. The next example shows this.

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite site = new SPSite(web.Site.ID))
    {
    // implementation details omitted
    }
});
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
RunWithElevatedPrivileges Example      Ron Lewandowski ... murahiman   |   Edit   |   Show History

The example is this Chapter is not very useful. Please try this one here to fullfill your needs.

Add a TreeView and a Button to your Form.

Implement the button click method.

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using System.Security.Permissions;
namespace Uploader
{
public class Worker
{
public Worker()
{
        }
        private SPSite site     = null;
private SPWeb web = null;
private SPFolder folder = null;
        /// <summary>
/// This Method is called throug Delegate elevatedGetSite which is definend in cmdOpenCnn_Click
/// </summary>
private void EstablishSharepoint()
{
site = new SPSite("http://srv-moss-tp1:36000");
web = site.OpenWeb();
}
/// <summary>
/// If you click the Button to establish a connection to your SharePoint-Site,
/// you run this code with elevated Privileges.
/// </summary>
private void cmdOpenCnn_Click(object sender, EventArgs e)
{
SPSecurity.CodeToRunElevated elevatedGetSite = new SPSecurity.CodeToRunElevated(EstablishSharepoint);
SPSecurity.RunWithElevatedPrivileges(elevatedGetSite);
            // After code execution in EstablishSharepoint(), following code will be executed
// and fills a TreeView (tv in this example) with all First-Level-Folders and their Subfolders
            TreeNode MainNode = new TreeNode("Web-Folders");
TreeNode n = null;
int index = 0;
            foreach (SPFolder f in web.Folders)
{
n = new TreeNode();
n.Text = f.Name;
n.Tag = f.ParentWeb.Url + "/" + f.Url;
n.ToolTipText = f.ParentWeb.Url + "/" + f.Url;
index = MainNode.Nodes.Add(n);
                foreach (SPFolder sf in f.SubFolders)
{
n = new TreeNode();
n.Text = sf.Name;
n.Tag = sf.ParentWeb.Url + "/" + sf.Url;
n.ToolTipText = sf.ParentWeb.Url + "/" + sf.Url;
MainNode.Nodes[index].Nodes.Add(n);
}
}
tv.Nodes.Add(MainNode);
}
}
}

RunWithElevatedPrivileges - VB.net example in a webpart      Bonnie White   |   Edit   |   Show History

The ASP.NET style web part does not have a partchache like the WSS 2.0 webpart. We needed a way to persist data long term.

Updates to properties from code do not persist unless you call SetPersonalizationDirty, but users with read permissions do not have sufficient rights to call this. One solution is below:

This code demonstrates how we can "cache" data into a web part's property from code. In our situation, data comes from a back-end system that is expensive to query and does not change often. The first time the web part runs, it caches its data into the DataCache property. Subsequent page loads display data from the property instead of the back-end system. Users can refresh the cache though the web part menu.

Imports System
Imports System.ComponentModel
Imports System.Configuration
Imports System.Data
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Xml
Imports Microsoft.SharePoint.WebPartPages
Imports Microsoft.SharePoint

Public Class iManageFolderView
Inherits System.Web.UI.WebControls.WebParts.WebPart

Implements IWebActionable

Dim _verb As WebPartVerbCollection

'used to build the error message to display to the user
Dim strErrorMsg As New StringBuilder

'property variables
Dim _ctext As String = ""
Dim _bAllowCaching As Boolean = True
Dim _bSortDescending As Boolean = False
Dim _DataCache As String = ""

Private ds As New DataSet
Private myTitle As String
Private reftome As String


#Region " Properties"

<WebBrowsable(False), Personalizable(True), WebDisplayName("iManage Folder ID"), Category("Miscellaneous"), DefaultValue(""), WebDescription("iManage Folder ID")> _
Property [cText]() As String
Get
Return _ctext
End Get

Set(ByVal Value As String)
_ctext = Value
End Set
End Property

<WebBrowsable(False), Personalizable(True), WebDisplayName("Data Cache"), Category("Miscellaneous"), DefaultValue(""), WebDescription("Data Cache")> _
Property [DataCache]() As String
Get
Return _DataCache
End Get

Set(ByVal Value As String)
_DataCache = Value
End Set
End Property

<WebBrowsable(True), Personalizable(True), WebDisplayName("Allow Caching"), Category("Miscellaneous"), DefaultValue(True), WebDescription("Allow Caching")> _
Property [bAllowCaching]() As Boolean
Get
Return _bAllowCaching
End Get

Set(ByVal Value As Boolean)
_bAllowCaching = Value
End Set
End Property


<WebBrowsable(True), Personalizable(True), WebDisplayName("Sort Descending"), Category("Miscellaneous"), DefaultValue(False), WebDescription("Sorts the list in descending order.")> _
Property [bSortDescending]() As Boolean
Get
Return _bSortDescending
End Get

Set(ByVal Value As Boolean)
_bSortDescending = Value
End Set
End Property
#End Region


Public Overrides ReadOnly Property Verbs() As System.Web.UI.WebControls.WebParts.WebPartVerbCollection
Get
Try
If _verb Is Nothing Then
Dim _verbList As New ArrayList
Dim onlyVerb As New WebPartVerb("Refresh", New WebPartEventHandler(AddressOf ClearCache))
onlyVerb.Text = "Refresh Folder Listing"
onlyVerb.Description = "Refreshes folder listing"
onlyVerb.Visible = True
onlyVerb.Enabled = True
_verbList.Add(onlyVerb)
_verb = New WebPartVerbCollection(_verbList)
End If
Catch ex As Exception
Me.strErrorMsg.Append("Error Occured During Verbs()")
Me.strErrorMsg.Append("<br />")
Me.strErrorMsg.Append("Error Message:")
Me.strErrorMsg.Append(ex.Message)
Me.strErrorMsg.Append("<br />")
Me.strErrorMsg.Append("Stack Trace: ")
Me.strErrorMsg.Append(ex.StackTrace)
End Try
Return _verb
End Get
End Property

Protected Sub ClearCache(ByVal sender As System.Object, ByVal e As WebParts.WebPartEventArgs)
Me.DataCache = ""

End Sub

Protected Overrides Sub CreateChildControls()
MyBase.CreateChildControls()

Try
'if we have not entered an imanage folder yet, do not continue
If Me.cText = "" Then
strErrorMsg.Append("Open the Tool Pane and select an Interwoven folder to display.")
Exit Sub
End If

'if we are allowing caching and data has been cached, use it,
'else, get data and populate the cache for next time
If bAllowCaching = True And DataCache <> "" Then
Dim oXMLDoc As New XmlDocument
oXMLDoc.LoadXml(Me.DataCache)
ds.ReadXml(New XmlNodeReader(oXMLDoc))

'debug
Me.strErrorMsg.Append("<br />Showing cached data.")
Else
Me.strErrorMsg.Append("<br />Getting data from iManage.")

Me.reftome = Me.ID

'imanage variables


. . .
Custom code fills the dataset’s table with an XML document here
. . .
ds.Tables.Add(dt)

'if caching is turned on in this webpart, populate the cache for next time.
If bAllowCaching Then
SPSecurity.RunWithElevatedPrivileges(AddressOf ElevatedSaveCache)
End If
End If

. . . display the data from the dataset

Catch ex As Exception
Me.strErrorMsg.Append(ex.Message & ex.StackTrace)
End Try

End Sub

Sub ElevatedSaveCache()

Try

Dim siteColl As SPSite = SPContext.Current.Site
Dim site As SPWeb = SPContext.Current.Web
Using ElevatedsiteColl As SPSite = New SPSite(siteColl.ID)
Using ElevatedSite As SPWeb = ElevatedsiteColl.OpenWeb(site.ID)
ElevatedSite.AllowUnsafeUpdates = True
Dim wpm As SPLimitedWebPartManager = ElevatedSite.GetLimitedWebPartManager(Me.Page.Request.RawUrl, PersonalizationScope.Shared)
Dim wp As iManageFolderView = wpm.WebParts(Me.reftome)

wp.DataCache = Me.ds.GetXml
wp.Title = Me.myTitle
wpm.SaveChanges(wp)
ElevatedSite.AllowUnsafeUpdates = False

End Using
End Using

Catch ex As Exception
Me.strErrorMsg.Append(ex.Message & ex.StackTrace)
End Try

End Sub


Protected Overrides Sub RenderContents(ByVal writer As System.Web.UI.HtmlTextWriter)
Try
MyBase.RenderContents(writer)

If strErrorMsg.Length > 0 Then
writer.WriteLine(strErrorMsg.ToString)
End If

Catch ex As Exception
writer.WriteLine(String.Concat("Error Message: ", ex.Message, "<br /> Stack Trace: ", ex.StackTrace))
End Try

End Sub

Public Overrides ReadOnly Property WebBrowsableObject() As Object
Get
Return MyBase.WebBrowsableObject
End Get
End Property

Public Overrides Function CreateEditorParts() As System.Web.UI.WebControls.WebParts.EditorPartCollection
Try

Dim editors As List(Of EditorPart) = New List(Of EditorPart)

Dim part As EditorPart = New FolderViewEditorPart()
part.ID = Me.ID & "_FolderViewEditorPart"
editors.Add(part)


' create the editorpart collection and return it
Dim epc As EditorPartCollection = New EditorPartCollection(editors)
Return epc

Catch ex As Exception
strErrorMsg.Append(ex.Message & ex.StackTrace)
Return MyBase.CreateEditorParts
End Try

End Function


End Class

Tags What's this?: Add a tag
Flag as ContentBug
Possible bug in the article      nagyrepa   |   Edit   |   Show History
SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite site = new SPSite(web.Site.ID))
    {
    // implementation details omitted
    }
});
web.Site.ID without "using" is not too nice.

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker