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.

  • This is what I am looking for. Please provide code. Thanks in advance. – user4649 Sep 4 '11 at 14:10

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.

  • 1
    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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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