<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Black Ninja Software &#187; SharePoint Object Model</title>
	<atom:link href="http://blackninjasoftware.com/tag/sharepoint-object-model/feed/" rel="self" type="application/rss+xml" />
	<link>http://blackninjasoftware.com</link>
	<description>SharePoint and SQL Server Consulting, Business Intelligence and Reporting, ASP.NET Vancouver</description>
	<lastBuildDate>Wed, 02 May 2012 00:21:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>How to Programmatically Impersonate Users in SharePoint</title>
		<link>http://blackninjasoftware.com/2009/04/09/how-to-programmatically-impersonate-users-in-sharepoint/</link>
		<comments>http://blackninjasoftware.com/2009/04/09/how-to-programmatically-impersonate-users-in-sharepoint/#comments</comments>
		<pubDate>Thu, 09 Apr 2009 23:05:26 +0000</pubDate>
		<dc:creator>donabel</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[SharePoint Object Model]]></category>

		<guid isPermaLink="false">http://blackninjasoftware.com/?p=516</guid>
		<description><![CDATA[Sometimes when creating SharePoint web or console applications, you may need to execute specific code blocks in another user&#8217;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&#8217; user [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes when creating SharePoint web or console applications, you may need to execute specific code blocks in another user&#8217;s context.</p>
<p>Impersonating users in SharePoint will require a couple of things:</p>
<ul>
<li>the account that the web or console app uses has privileges to impersonate other users (typically this would be the system account)</li>
<li>specific users&#8217; user tokens</li>
</ul>
<p><span id="more-516"></span><br />
Step 1: Log in as the system account, or get a handle to the system account in your code</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">string</span> siteStr <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;http://mysharepointsite/&quot;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">//we just need to get a handle to the site for us</span>
<span style="color: #008080; font-style: italic;">//to get the system account user token</span>
SPSite tempSite <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SPSite<span style="color: #008000;">&#40;</span>siteStr<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
SPUserToken systoken <span style="color: #008000;">=</span> tempSite<span style="color: #008000;">.</span><span style="color: #0000FF;">SystemAccount</span><span style="color: #008000;">.</span><span style="color: #0000FF;">UserToken</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>SPSite site <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SPSite<span style="color: #008000;">&#40;</span>siteStr, systoken<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>SPWeb web <span style="color: #008000;">=</span> site<span style="color: #008000;">.</span><span style="color: #0000FF;">OpenWeb</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
   <span style="color: #008000;">&#123;</span>
       <span style="color: #008080; font-style: italic;">//right now, logged in as Site System Account</span>
       Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Currently logged in as: &quot;</span> <span style="color: #008000;">+</span>
                        web<span style="color: #008000;">.</span><span style="color: #0000FF;">CurrentUser</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
       <span style="color: #008080; font-style: italic;">//add your code here</span>
   <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

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

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">//get this current user's user token</span>
SPUserToken userToken <span style="color: #008000;">=</span> web<span style="color: #008000;">.</span><span style="color: #0000FF;">AllUsers</span><span style="color: #008000;">&#91;</span>user<span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">UserToken</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">//create an SPSite object in the context of this user</span>
SPSite s <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SPSite<span style="color: #008000;">&#40;</span>siteStr, userToken<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
SPWeb w <span style="color: #008000;">=</span> s<span style="color: #008000;">.</span><span style="color: #0000FF;">OpenWeb</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Currently logged in as: &quot;</span> <span style="color: #008000;">+</span>
                  w<span style="color: #008000;">.</span><span style="color: #0000FF;">CurrentUser</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span>
                  <span style="color: #666666;">&quot;(&quot;</span> <span style="color: #008000;">+</span> w<span style="color: #008000;">.</span><span style="color: #0000FF;">CurrentUser</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)&quot;</span>
                 <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>Complete code follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> impersonateTest<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   <span style="color: #6666cc; font-weight: bold;">string</span> siteStr <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;http://mysharepointsite/&quot;</span><span style="color: #008000;">;</span>
   SPSite tempSite <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SPSite<span style="color: #008000;">&#40;</span>siteStr<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
   SPUserToken systoken <span style="color: #008000;">=</span> tempSite<span style="color: #008000;">.</span><span style="color: #0000FF;">SystemAccount</span><span style="color: #008000;">.</span><span style="color: #0000FF;">UserToken</span><span style="color: #008000;">;</span>
   <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>SPSite site <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SPSite<span style="color: #008000;">&#40;</span>siteStr, systoken<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
   <span style="color: #008000;">&#123;</span>
       <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>SPWeb web <span style="color: #008000;">=</span> site<span style="color: #008000;">.</span><span style="color: #0000FF;">OpenWeb</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
       <span style="color: #008000;">&#123;</span>
           <span style="color: #008080; font-style: italic;">//right now, logged in as Site System Account</span>
           Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Currently logged in as: &quot;</span> <span style="color: #008000;">+</span>
                              web<span style="color: #008000;">.</span><span style="color: #0000FF;">CurrentUser</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
           switchUser<span style="color: #008000;">&#40;</span>web, siteStr, <span style="color: #666666;">&quot;BlackNinjaSoftware/MatthewCarriere&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
           switchUser<span style="color: #008000;">&#40;</span>web, siteStr, <span style="color: #666666;">&quot;BlackNinjaSoftware/ShereenQumsieh&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
           switchUser<span style="color: #008000;">&#40;</span>web, siteStr, <span style="color: #666666;">&quot;BlackNinjaSoftware/DonabelSantos&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
       <span style="color: #008000;">&#125;</span>
   <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> switchUser<span style="color: #008000;">&#40;</span>SPWeb web, <span style="color: #6666cc; font-weight: bold;">string</span> siteStr, <span style="color: #6666cc; font-weight: bold;">string</span> user<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   <span style="color: #008080; font-style: italic;">//impersonate somebody else</span>
   SPUserToken userToken <span style="color: #008000;">=</span> web<span style="color: #008000;">.</span><span style="color: #0000FF;">AllUsers</span><span style="color: #008000;">&#91;</span>user<span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">UserToken</span><span style="color: #008000;">;</span>
   SPSite s <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SPSite<span style="color: #008000;">&#40;</span>siteStr, userToken<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
   SPWeb w <span style="color: #008000;">=</span> s<span style="color: #008000;">.</span><span style="color: #0000FF;">OpenWeb</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
   Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Currently logged in as: &quot;</span> <span style="color: #008000;">+</span>
                     w<span style="color: #008000;">.</span><span style="color: #0000FF;">CurrentUser</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span>
                     <span style="color: #666666;">&quot;(&quot;</span> <span style="color: #008000;">+</span> w<span style="color: #008000;">.</span><span style="color: #0000FF;">CurrentUser</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)&quot;</span>
                    <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://blackninjasoftware.com/2009/04/09/how-to-programmatically-impersonate-users-in-sharepoint/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

