none
how to know in c# if an instance of SPWeb is elevated or not RRS feed

  • Question

  • hi all how are you

    i wish anyone could help me in my problem

    i want to know how to check  if an instance of SPWeb in c# code is elevated or not is there a property in the SPWeb that tells us if the web is elevated or not

    thanks a lot

    Tuesday, May 4, 2010 7:58 AM

Answers

  • hello,

    there is no OTB property in SPWeb which says whether it is elevated or not. But I agree with Markus that you can check SPWeb.CurrentUser property. The only thing is that instead of using hardcoded literal "SHAREPOINT\system" you can use SPSite.SystemAccount property. Also you can create extension method for SPWeb class:

     

    public static class SPWebExtensions
    {
      public static bool IsElevated(this SPWeb web)
      {
        if (web == null)
        {
          throw new ArgumentNullException("web");
        }
    
        if (web.CurrentUser == null)
        {
          return false;
        }
    
        return string.Equals(web.CurrentUser.LoginName, web.Site.SystemAccount.LoginName,
          StringComparison.InvariantCultureIgnoreCase);
      }
    }

     

    With this method you can check is SPWeb elevated or not using the following code:

     

    if (web.IsElevated())
    {
      ...
    }

     


    Blog - http://sadomovalex.blogspot.com
    Codeplex - http://camlex.codeplex.com
    • Marked as answer by Chengyi Wu Tuesday, May 11, 2010 2:30 AM
    Tuesday, May 4, 2010 2:56 PM

All replies

  • get the SPWeb.CurrentUser property and compare it for "SHAREPOINT\system". If so either the web was retrieved within an elevated context or the app pool user is logged in. In both cases you have full access to the web instance.

     

     

    regards,

    Markus

    Tuesday, May 4, 2010 1:05 PM
  • hello,

    there is no OTB property in SPWeb which says whether it is elevated or not. But I agree with Markus that you can check SPWeb.CurrentUser property. The only thing is that instead of using hardcoded literal "SHAREPOINT\system" you can use SPSite.SystemAccount property. Also you can create extension method for SPWeb class:

     

    public static class SPWebExtensions
    {
      public static bool IsElevated(this SPWeb web)
      {
        if (web == null)
        {
          throw new ArgumentNullException("web");
        }
    
        if (web.CurrentUser == null)
        {
          return false;
        }
    
        return string.Equals(web.CurrentUser.LoginName, web.Site.SystemAccount.LoginName,
          StringComparison.InvariantCultureIgnoreCase);
      }
    }

     

    With this method you can check is SPWeb elevated or not using the following code:

     

    if (web.IsElevated())
    {
      ...
    }

     


    Blog - http://sadomovalex.blogspot.com
    Codeplex - http://camlex.codeplex.com
    • Marked as answer by Chengyi Wu Tuesday, May 11, 2010 2:30 AM
    Tuesday, May 4, 2010 2:56 PM