Tuesday, April 13, 2010

Impersonation in SharePoint 2007

We do not have SPSecurity.RunWithElevatedPrivileges with WSS 2.0 and we have used explicit .net impersonation concepts.

using SPSecurity.RunWithElevatedPrivileges all the time will not work, In that context better using .net Impersonation.

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

No comments:

Post a Comment