Skip to main content

very good alternate to RunWithElevatedPrivileges

Scenario:
You might want to impersonate a specific WSS user identity before creating a new object so that WSS user is recognized as the owner of the new object.
In order to impersonate a WSS user identity, you must first create an SPUserToken object. You can do this by accessing the UserToken property of an SPUser object. Once you have the SPUserToken object, you can use it to create a new SPSite object using an overloaded version of the SPSite class constructor.

Solution:
This is a very good alternate to RunWithElevatedPrivileges.

Code:
1. SPWeb siteCollection = SPContext.Current.Site;
2. SPWeb site = SPContext.Current.Web;
3.
4. // get SPUser object and acquire token
5. SPUser targetUser = site.SiteUsers[@"LITWAREINC\BrianC"];
6. SPUserToken token = targetUser.UserToken;
7.
8. // create new SPSite and SPWeb object to impersonate user
9. using (SPSite impersonatedSiteCollection =
10. new SPSite(siteCollection.ID, token)) {
11. using (SPWeb impersonatedSite =
12. impersonatedSiteCollection.OpenWeb(site.ID)) {
13. // WSS identity switched to impersonate BrianC
14. // Windows identity does not change
15. }
16. }
Article: http://msdn.microsoft.com/en-us/magazine/cc163287.aspx

Comments

Popular posts from this blog

Site Logo Not Changing on Web Part Pages

I tested and reproduced your issue in my local machine. Since the Web Part Pages would override the content in PlaceHolderPageTitleInTitleArea place holder, the site logo would not change automatically. So would you please try remove or comment the following control TitleBarWebPart: See the similar scenario and solution: http://emanonsolutions.blogspot.com/2010/02/left-navigation-webpart-pages.html Hope this can help.

Create a Custom Site Definition with Additional Content in SharePoint 2010 Using Visual Studio 2010

·          Web Templates ·          Site Definitions and Configurations ·          Deciding Between Custom Web Templates and Custom Site Definitions ·          Understanding Onet.xml Files ·          How to: Create a Custom Web Template ·          Overview of Creating Custom Site Definitions Site Template Configurator utility http://stefan-stanev-sharepoint-blog.blogspot.com/search/label/SharePoint%202010 Create a Custom Site Definition with Additional Content in SharePoint 2010 Using Visual Studio 2010 http://community.bamboosolutions.com/blogs/sharepoint-2010/archive/2010/11/11/sharepoint-2010-cookbook-how-to-create-a-custom-site-definition-with-additional-content-in-sharepoint-2010-using-visual-studio-2010.aspx http://blogs.msdn.com/b/allenwang/...

SharePoint 2010 Branding

Real World Branding with SharePoint 2010 Publishing Sites http://msdn.microsoft.com/en-us/library/gg430141.aspx#Y13640 The most important source of information is the  SDK . I can’t stress that enough. There is so much more documentation with this release than there was with 2007. However, we are all in the habit of looking to each other for information first. Researching the SDK and walking through the Development classes, even if you aren’t a developer. is an important step in understanding the complex relationships between the objects in SharePoint and ASP.NET. Planning Strategy and Design Visual Design SharePoint 2010 Layered Photoshop File!  – Erik Swenson Universal Usability Guidelines The Pursuit of Interface Design Simplicity  -Luke Wroblewski Visible Narratives: Understanding Visual Organization  -Luke Wroblewski Common Visual Design Misconceptions  – Luke Wroblewski Usability 20 Do’s and Don’ts of Effective Web Design Introduction to Good Usab...