Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Could anyone suggest a way to use Javascript to check whether or not the logged in user to a SharePoint site is Admin?

We need to hide some of the SharePoint's MasterPage ContentPlaceHolders if the logged in user is not an administrator.

share|improve this question
add comment

3 Answers 3

If you are using SP 2010 or SP 2013, you can use SharePoint Javascript client object model code to check if user has admin permissions or not. This is done by using Web.DoesUserHavePermissions. If a user is admin he/she will have manageWeb permissions. Here is a sample code:

var context;
var web;
var isAdmin = false;
function CheckUserPermissions() {
    context = new SP.ClientContext.get_current();
    web = context.get_web();
    context.load(web, 'EffectiveBasePermissions');
    context.executeQueryAsync(onSuccess, onFailure);
}
function onSuccess() {
    if (web.get_effectiveBasePermissions().has(SP.PermissionKind.manageWeb)) {
        isAdmin = true;
    }
}
function onFailure(sender, args) {
    alert('request failed ' + args.get_message() + 'n' + args.get_stackTrace());
}
share|improve this answer
add comment

If you can edit the page layouts or the master page you could make use of the SPSecurityTrimmedControl, a guide is found here -> http://www.infowisesolutions.com/blog/Comments.aspx?ArticleId=28

The main idea is the wrap the placeholders or other elements with this tag, and in the tag set the minimum permissions for the content to be rendered

share|improve this answer
add comment

How to determine if User is Site Admin via CSOM

User.IsSiteAdmin property specifies whether the user is a site collection administrator.

Limitations: this property is not available in SharePoint 2010 CSOM

function isCurrentUserSiteAdmin(OnSuccess,OnError)
{
    var context = SP.ClientContext.get_current();
    var user = context.get_web().get_currentUser();
    context.load(user);

    context.executeQueryAsync(
        function() {
          var isSiteAdmin = user.get_isSiteAdmin();  
          OnSuccess(isSiteAdmin);           
        },
        OnError
    );
}

//Usage
isCurrentUserSiteAdmin(function(isAdmin){
        console.log(isAdmin);
    },function(sender,args){
        console.log('An error occured: ' + args.get_message());
    }
);

For SharePoint 2010

function isCurrentUserSiteAdmin(OnSuccess,OnError)
{
    var context = SP.ClientContext.get_current();
    var list = context.get_web().get_siteUserInfoList();
    var userItem = list.getItemById(_spPageContextInfo.userId);

    context.load(userItem);
    context.executeQueryAsync(
        function() {
          var isSiteAdmin = userItem.get_item('IsSiteAdmin');  
          OnSuccess(isSiteAdmin);           
        },
        OnError
    );


}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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