SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When creating a new Linq to SharePoint context, normally we don't pass a site object but we provide a request url.

So what should I do if I want to execute Linq queries under a different user context? Is there a way to do it?

Thanks in advance.

share|improve this question
This is what I am looking for. Please provide code. Thanks in advance. – user4649 Sep 4 '11 at 14:10
up vote 3 down vote accepted

Ok, I found the answer to my own question. It is possible with switching HttpContext objects.

Here are the steps:

  • Retrieve the user token from SPContext.Current
  • Set HttpContext.Current to null
  • Create a new SPSite with the user token
  • Create a new Linq to SharePoint context

This way the new context is created within the context of the prefererred user.

I can provide code if someone needs it.

share|improve this answer
You can accept your own answer :) – Tim Gabrhel Sep 17 '11 at 20:44
can you provide some code how you do that? That'd be awesome. – Roman Mar 31 '12 at 18:40

You can do as below :

SPWeb oWeb = SPContext.Current.Web;
SPUserToken token = oWeb.AllUsers[@"SHAREPOINT\SYSTEM"].UserToken;
using (SPSite elevatedSite = new SPSite(oWeb.Site.ID, token))
    using (SPWeb elevatedweb = site.OpenWeb())
      // Perform administrative actions       

You can also use SPSecurity.RunWithElevatedPrivileges block for the same.

For in-depth information on the impersonation techniques, Check this out :

Impersonation in SharePoint : An Extreme Overview

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.