There are times when developing custom web parts and other SharePoint solutions, you need to impersonate a user and view SharePoint from within their context.

Impersonating a user in SharePoint requires a few things:

  • the account that the web or console application users that 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

 

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

 

Complete code follows: