Return to Snippet

Revision: 23723
at February 11, 2010 19:29 by digiteyes


Initial Code
private static void impersonateTest()
{
   string siteStr = "http://mysharepointsite/";
   SPSite tempSite = new SPSite(siteStr);
   SPUserToken systoken = tempSite.SystemAccount.UserToken;
   using (SPSite site = new SPSite(siteStr, systoken))
   {
       using (SPWeb web = site.OpenWeb())
       {
           //right now, logged in as Site System Account
           Console.WriteLine("Currently logged in as: " +
                              web.CurrentUser.ToString());
           switchUser(web, siteStr, "BlackNinjaSoftware/MatthewCarriere");
           switchUser(web, siteStr, "BlackNinjaSoftware/ShereenQumsieh");
           switchUser(web, siteStr, "BlackNinjaSoftware/DonabelSantos");
       }
   }
}
 
private static void switchUser(SPWeb web, string siteStr, string user)
{
   //impersonate somebody else
   SPUserToken userToken = web.AllUsers[user].UserToken;
   SPSite s = new SPSite(siteStr, userToken);
   SPWeb w = s.OpenWeb();
   Console.WriteLine("Currently logged in as: " +
                     w.CurrentUser.ToString() +
                     "(" + w.CurrentUser.Name + ")"
                    );
}

Initial URL
http://blackninjasoftware.com/tag/sharepoint-object-model/

Initial Description

                                

Initial Title
Impersonate Site with SharePoint System Account

Initial Tags
object, sharepoint

Initial Language
C#