question

AmitRawat-3636 avatar image
0 Votes"
AmitRawat-3636 asked WendyLi-MSFT commented

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

Hi,
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-devoffice-sharepoint-server-development
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MichaelHan-MSFT avatar image
0 Votes"
MichaelHan-MSFT answered WendyLi-MSFT commented

Hi @AmitRawat-3636,

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:

https://docs.microsoft.com/en-us/SharePoint/sites/user-permissions-and-permission-levels.
------------Update-----------
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.





· 16
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @MichaelHan-MSFT ,
I provided full access at the WebApplication level to both users. To achieve this, I go to central admin-> then click on web application -> then user policy -> and here I give full read permission to both the users. Do we need to give any other permission?
I am traversing a web application for this I am using the below code. Working fine with the user provided by IT team. But for other user after giving required privileges it is not working.

SPSecurity.RunWithElevatedPrivileges(delegate ()
{
SPWebApplication webApplication = SPWebApplication.Lookup(new Uri(webApplicationUrl));
foreach (SPSite sps in webApplication.Sites)
{
if (sps != null)
{
string size = FormatSize(sps.Usage.Storage);

                     foreach (SPWeb subSite in sps.RootWeb.Webs)
                     {
                         if (subSite != null)
                         {
                             //Do Something
                         }
                     }
                        
                 }
             }
         });
0 Votes 0 ·

You could try to give full control permissions. Check if it works.

0 Votes 0 ·
sadomovalex avatar image
0 Votes"
sadomovalex answered AmitRawat-3636 commented

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 http://example.com/_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)

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @sadomovalex
Below is my code and I am also following the same approach. When I select this user as a farm service account everything works as expected. Otherwise it is throwing error. Farm Service Account is a big thing. It would be good if we can achieve this with limited privileges.

SPSecurity.RunWithElevatedPrivileges(delegate ()
{
SPWebApplication webApplication = SPWebApplication.Lookup(new Uri(webApplicationUrl));
foreach (SPSite sps in webApplication.Sites)
{
if (sps != null)
{
size = FormatSize(sps.Usage.Storage)

                     }
                 }
             });
0 Votes 0 ·