6

As an example, the below jQuery displays current SharePoint user title. Excellent! :)

Is it possible to use the sp.js objects to test If the current user has Full Control on the current page or Site and send an alert if they do?

Many Thanks.

var context;
var web;
var user;

// This code runs when the DOM is ready. It ensures the SharePoint
// script file sp.js is loaded and then executes sharePointReady()
$(document).ready(function () {
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);
});

// This function creates a context object which is needed to use the SharePoint object model
function sharePointReady() {
    context = new SP.ClientContext.get_current();
    web = context.get_web();

    getUserName();
}

// This function prepares, loads, and then executes a SharePoint query to get the current users information
function getUserName() {
    user = web.get_currentUser();
    context.load(user);
    context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFail);
}

// This function is executed if the above call is successful
// It replaces the contents of the 'helloString' element with the user name
function onGetUserNameSuccess() {
    $('#message').text('Hello ' + user.get_title());
}

2 Answers 2

17

You can use Sharepoint Client Object Model for this. Here is how to do it on the current web:

function CheckPermissionOnWeb() 
{
    context = new SP.ClientContext.get_current();

    web = context.get_web();

    this._currentUser = web.get_currentUser();

    context.load(this._currentUser);

    context.load(web,'EffectiveBasePermissions');

    context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod), Function.createDelegate(this, this.onFailureMethod));        

    function onSuccessMethod(sender, args) 
    {
        if (web.get_effectiveBasePermissions().has(SP.PermissionKind.editListItems)) 
        {
            //User Has Edit Permissions
            alert('YEAH, edit list permissions!');
        }
    }
} 

from http://spdailytips.blogspot.se/2011/09/check-current-user-permission.html

Here you can find all the possible values for the Enum SP.PermissionKind.

This pattern will work on a List or a ListItem as well (like a page). Just make sure to load the object with context.load(theItem, 'EffectiveBasePermissions');

So in your case manageWeb should do great!

2
  • 2
    Is there a reason to get the currentUser object? can you do without this? Jul 27, 2015 at 6:46
  • 1
    Full control means FullMask or ManageWeb permsission ?
    – Tito
    Nov 4, 2015 at 11:53
0

Here is another way that worked for me:

https://www.codeproject.com/articles/678653/how-to-check-user-permission-for-the-web-list-or-s

var ctx = new SP.ClientContext.get_current();
var web = ctx.get_web();

var ob = new SP.BasePermissions();
ob.set(SP.PermissionKind.editListItems)

var per = web.doesUserHavePermissions(ob)
ctx.executeQueryAsync(
  function(){ 
      alert(per.get_value()); // true if user has permission 
    },
 function(a,b){
     alert ("Something wrong");
 }
);

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.