SSOM C# UnAuthorize Exception while using SPSite.Usage.Storage

asked 2021-11-10T05:01:08.8+00:00
Amit Rawat 526 Reputation points

I am using SP2013 and trying to fetch SPSite.Usage.Storage while traversing web application. I have two users both under the farm admin group. But it is working for one user and not working for other.
I also go to central admin -> then click on web application -> then user policy. Here also both users have similar permission which is Full Read.
Still this command working for one user whereas not working for other one.

SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
1,665 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,347 questions
No comments
{count} votes

2 answers

Sort by: Most helpful
  1. answered 2021-11-10T07:24:58.927+00:00
    Michael 17,861 Reputation points

    Hi @Amit Rawat ,

    To get SPSite.Usage.Storage data, the user need to have full control permission for the site.

    Could it that one user is the site collection administrator or he has full control to the site?

    Please refer to this article for more:

    Also make sure the user is a managed account, to check if a user is managed account or not, you can refer to the following:

    1. Go to Central Administration.
    2. Click Security and then select Configure Managed Accounts.
    3. The list of existing managed accounts is displayed.

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

  2. answered 2021-11-16T15:23:37.737+00:00
    sadomovalex 3,586 Reputation points

    how exactly do you run provided code? Is it running with Sharepoint context (e.g. as SP web service or application layout page opened in context of some SP site like this\_layouts/15/MyPage.aspx) or you run it without SP context (e.g. as console application or SP job)?

    • If you run it with SP context then you need to modify your code like that: SPSecurity.RunWithElevatedPrivileges(() =>
      using (var elevatedSite = new SPSite(webApplicationUrl))
      var webApplication = elevatedSite.WebApplication;
      foreach (SPSite sps in webApplication.Sites)
      string size = FormatSize(sps.Usage.Storage);

    I.e. it is necessary to reopen SPSite under RunWithElevatedPrivileges - otherwise RunWithElevatedPrivileges won't have effect. After that code will run under SHAREPOINT\System account (=account of app pool used by Sharepoint web site in IIS) which should have access to sps.Usage.Storage.

    • If you run this code without SP context - then you don't need RunWithElevatedPrivileges since it has effect only when SP context presents. In this case code is always running under account of user which ran the code. If it fails and you already added Full control policy for this user on this web application, try to add this user to Site collection administrators of each site collection in web app including root site collection (go to Site settings > Site collection administrators > Add user under which you run this code. Repeat this step for all site collections in web app)