Sometimes when creating SharePoint web or console applications, you may need to execute specific code blocks in another user's context.

Impersonating users in SharePoint will require a couple of things:

  • the account that the web or console app uses has privileges to impersonate other users (typically this would be the system account)
  • specific users' user tokens

Step 1: Log in as the system account, or get a handle to the system account in your code

string
siteStr = "http://mysharepointsite/";

 
 

//we just need to get a handle to the site for us

//to get the system account user token

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

 
 

       //add your code here

   }

}

Step 2: Before you impersonate, get the user token of the user you are switching to. For example:

//get this current user's user token

SPUserToken userToken = web.AllUsers[user].UserToken;

 
 

//create an SPSite object in the context of this user

SPSite s = new
SPSite(siteStr, userToken);

 
 

SPWeb w = s.OpenWeb();

Console.WriteLine("Currently logged in as: "
+

                  w.CurrentUser.ToString() +

                  "("
+ w.CurrentUser.Name + ")"

                 );

Complete code follows:

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 + ")"

                    );

}

 

 

From: http://www.sharepointdeveloperhq.com/2009/04/how-to-programmatically-impersonate-users-in-sharepoint/

How to Programmatically Impersonate Users in SharePoint的更多相关文章

  1. Programmatically Disable Event Firing on List Item Update in SharePoint 2010

    1. Microsoft.SharePoint.dll Create EventFiring.cs 1.Right-click on the project, select Add and click ...

  2. Creating a SharePoint Sequential Workflow

    https://msdn.microsoft.com/en-us/library/office/hh824675(v=office.14).aspx Creating a SharePoint Seq ...

  3. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q147-Q151)

    Question  147 Your company has an existing SharePoint 2010 public-facing Web site. The Web site runs ...

  4. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q144-Q146)

    Question  144 You are planning a feature upgrade for a SharePoint 2010 farm. The original feature wi ...

  5. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q112-Q115)

    Question  112 You are designing a public-facing SharePoint 2010 Web site for an elementary school th ...

  6. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q16-Q18)

    Question 16 You are designing a SharePoint 2010 solution to manage statements of work. You need to d ...

  7. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q59-Q62)

    Question 59You are designing a collection of Web Parts that will be packaged into a SharePoint 2010 ...

  8. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q75-Q77)

    Question 75You are designing a feature for a SharePoint 2010 solution that will be activated by defa ...

  9. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q78-Q80)

    Question 78 You are designing an application configuration approach for a custom SharePoint 2010 app ...

随机推荐

  1. php中的gethostbyname函数有问题

    在根据域名获取ip的批量执行中,gethostbyname有些域名得到的ip是不正确的,不知道是不是版本的bug. 解决办法是,使用执行命令的方式获取 echo exec("host dom ...

  2. vue.js 2.0开发(4)

    使用vue-cli,首先安装: npm install -g vue-cli 安装完了执行vue命令,会出现 vue init <template-name> <project-na ...

  3. delphi 文件删除,复制

    这是个相对简单的方法,不知道能不能帮到你删除:procedure TForm1.Button1Click(Sender: TObject);begin deletefile('c:\123.txt') ...

  4. WebStorm注册码

    WebStorm注册码User Name:EMBRACE License Key:===== LICENSE BEGIN =====24718-1204201000001h6wzKLpfo3gmjJ8 ...

  5. js插件添加打印功能

    <%@ page language="java" %> <%@ page contentType="text/html; charset=gb2312& ...

  6. CString std::string相互转换

    CString->std::string 例子: CString strMfc=“test“; std::string strStl; strStl=strMfc.GetBuffer(0); s ...

  7. symbol(s) not found for architecture x86_64 之 linker command failed with exit code 1 (use -v to see invocation)解决方案排查

    这样的错误 ,我的解决方案是, 第一种:   查看他说在 ****.o 中,你要查看这样的关键点,然后去查看,你 项目中有没有引进这样的文件,在项目中查找,看项目中有没有,如果没有那就是没添加进来,你 ...

  8. Winform Textbox MultiLine和WordWrap属性的使用

    突然用到,有些不明白,零乱记录下: 1.MultiLine属性为True时,Dock属性Fill值才能达到所需效果 2.MultiLine属性为False时,WordWrap属性也是无效果的,同1,T ...

  9. uva1588kickdown

    题目要求简述:给定长度分别为n1,n2(n1,n2<=100)且每列的高度只为1或者2的长条.需要将他们放入一个高度为3的容器,问能够容纳它们的最短容器长度. 思路就是固定一个字符串a,字符串b ...

  10. My SQL的内连接,外链接查询

    1.内连接:只连接匹配的行. 2.左外连接:包含左边表的全部行,以及右边表中所有匹配的行,无论右边的表有没有和左边匹配的行,左边的所有行都必须要显示. 3.右外连接:包含右边表的全部行,以及左边表中所 ...