Tuesday, June 4, 2013

RunWithElivatedPrivilages in SharePoint event handler

Here is the small code sample which will execute Event handler code with Elevated privilages.

Note : Usually Event receivers will not accept RunWithElivatedPrivilages deligate to write because of the inbuildt secutiry framework.

The only workaround for this is to use SPUserToken.

SPUserToken : This object is will work same like RunWithElivatedPrivilages but with desired user name permissions.

Here is the sample code for the same.

public override void ItemAdding(SPItemEventProperties properties)  
            SPSite site = properties.Web.Site;  
            SPUserToken sysToken = site.SystemAccount.UserToken;  
            using (SPSite mySite = new SPSite(properties.SiteId, sysToken))  
                using (SPWeb myWeb = systemSite.OpenWeb(properties.Web.ID))  
                       // Code goes here   

Thank You !!!

No comments:

Post a Comment