How to use explicit impersonation in SharePoint 2007

Tuesday, 28 July 2009 15:38 by myro

There are different ways for using impersonation in SharePoint 2007. Probably the most common way is calling the RunWithElevatedPrivileges method defined into the SPSecurity class. If you don't know how to impersonate using this method, you should visit the msdn reference.

But sometimes this approach doesn't work, infact this method is useless if you are trying to impersonate when writing event handlers. An another possible solution, is using the Explicit Impersonation in SharePoint 2007. This method will impersonate through user's SPUserToken class.

Here's the code snippet for impersonating a different user:

SPUser user = SPContext.Current.Web.AllUsers[@"DOMAIN\LOGINNAME"];
SPUserToken token = user.UserToken;
using (SPSite site = new SPSite(SPContext.Current.Site.Url,token))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPList list = web.Lists["YourList"];
        list.Items[0]["Title"] = "Your new title";
        list.Items[0].Update();
    }
}

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   , ,
Categories:   SharePoint 2007
Actions:   Bookmark and Share | Permalink | Comments (0) | Comment RSSRSS comment feed
If you consider this post usefull for your purposes, please consider visiting my sponsors to help me out with the myrocode.com maintenance. Thank you.