Impersonate Site with SharePoint System Account


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  1. private static void impersonateTest()
  2. {
  3. string siteStr = "http://mysharepointsite/";
  4. SPSite tempSite = new SPSite(siteStr);
  5. SPUserToken systoken = tempSite.SystemAccount.UserToken;
  6. using (SPSite site = new SPSite(siteStr, systoken))
  7. {
  8. using (SPWeb web = site.OpenWeb())
  9. {
  10. //right now, logged in as Site System Account
  11. Console.WriteLine("Currently logged in as: " +
  12. web.CurrentUser.ToString());
  13. switchUser(web, siteStr, "BlackNinjaSoftware/MatthewCarriere");
  14. switchUser(web, siteStr, "BlackNinjaSoftware/ShereenQumsieh");
  15. switchUser(web, siteStr, "BlackNinjaSoftware/DonabelSantos");
  16. }
  17. }
  18. }
  19.  
  20. private static void switchUser(SPWeb web, string siteStr, string user)
  21. {
  22. //impersonate somebody else
  23. SPUserToken userToken = web.AllUsers[user].UserToken;
  24. SPSite s = new SPSite(siteStr, userToken);
  25. SPWeb w = s.OpenWeb();
  26. Console.WriteLine("Currently logged in as: " +
  27. w.CurrentUser.ToString() +
  28. "(" + w.CurrentUser.Name + ")"
  29. );
  30. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.