6

Is it possible to check if the current account is a SharePoint administrator? The only way I can think of achieving this is by attempting to login to the https://tenant-admin.sharepoint.com and check if I get a 403.

I thought there might be a cleaner way of doing this? This if for SharePoint Online/Office 365

3 Answers 3

3

Try this:

using (var ctx = new ClientContext(webUri))
{
    var currentUser = ctx.Web.CurrentUser;
    ctx.Load(currentUser);
    ctx.ExecuteQuery();

    Console.WriteLine(currentUser.IsSiteAdmin);
}
6
  • what would I use for the webUri, to use the clientcontext would I not neet to authenticate against -admin.sharepoint url which would throw a 403 erro?
    – Webfort
    Mar 7, 2016 at 10:41
  • use your https://<domainname>.sharepoint.com url as webUri Mar 7, 2016 at 10:57
  • Thanks, that looks like it works with the testing I have done.
    – Webfort
    Mar 7, 2016 at 11:19
  • do you know if there is a way of doing this for SharePoint On Premise? Is there a general url I can use?
    – Webfort
    Oct 20, 2016 at 9:28
  • Which type of general url? Oct 21, 2016 at 8:26
9

REST API:

Use the below rest api to determine the current loggin user is site collection admin or not

https://<sharepointsiteurl>/_api/web/currentUser/issiteadmin

This returns the true or false value in xml format.

JS Format: Use the below SharePoint ecmaScript to identify the current user admin status,

function checkUserisSiteAdmin() {
    var clientContext = new SP.ClientContext();
    oUser = clientContext.get_web().get_currentUser();

    clientContext.load(oUser);
    clientContext.executeQueryAsync(
        Function.createDelegate(this, function() {
            var userInfo = '';
            userInfo +='LoginName: '+ oUser.get_loginName() +', SiteAdmin: '+ oUser.get_isSiteAdmin();            
            alert(userInfo.toString());
        }),
        Function.createDelegate(this, function(sender, args) {           
            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        }));
}
ExecuteOrDelayUntilScriptLoaded(checkUserisSiteAdmin, "sp.js");
4

In CSOM, you can execute the following line of code:

_spPageContextInfo.isSiteAdmin;

Example:

alert(_spPageContextInfo.isSiteAdmin);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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