Posted By

digiteyes on 02/11/10


Tagged

object model sharepoint


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

jlaws0n


Impersonate Site with SharePoint System Account


 / Published in: C#
 

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

  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. }

Report this snippet  

You need to login to post a comment.