Click here to Skip to main content
14,634,094 members
Articles » Web Development » Web Services » ATL Web Services

How to Check User Permission for the Web, List, or SharePoint Item in SharePoint JavaScript Object Model

Rate this:
5.00 (3 votes)
Please Sign up or sign in to vote.
5.00 (3 votes)
5 Nov 2013CPOL
How to check user permission for the web, list, or SharePoint Item in SharePoint JavaScript Object Model.

Normally, we need to perform tasks such as:

  • Is current user an admin on the site
  • Does current user have a list of edit permissions
  • etc.

SharePoint provides a method called doesUserHavePermissions to perform that. First of all, we need to know how SharePoint defines User roles by assigning permission levels such as Full Control, Contributor, design, etc.

As an example, site admin is assigned by Full Control which is a composite of few permission items we called as permission kind.

Full control - http://office.microsoft.com/en-001/windows-sharepoint-services-help/permission-levels-and-permissions-HA010100149.aspx.

You can get all kinds of permissions by http://msdn.microsoft.com/en-us/library/ee556747(v=office.14).aspx.

Example One

Assume that we want to check whether current user is an admin of the site. For that, we need to check whether user has manageWeb permission kind. (Actually, we need to check other kinds of permission assigned to full control as well but if user has managed web, it is more likely user can perform admin tasks. In my other example, I will show how to check multiple kinds of permission.)

var ctx = new SP.ClientContext.get_current();
var web = context.get_web();
 
var ob = new SP.BasePermissions();
ob.set(SP.PermissionKind.manageWeb)
 
var per = web.doesUserHavePermissions(ob)
ctx.executeQueryAsync(
     function(){ 
          alert(per.get_value()); // If this is true, user has permission, if not, no 
        },
     function(a,b){
         alert ("Something wrong");
 }
);

Example Two – Check Multiple Permission Kinds

In here, I'm going to check manageweb and managePermissions.

var ctx = new SP.ClientContext.get_current();
var web = context.get_web();
 
var ob = new SP.BasePermissions();
ob.set(SP.PermissionKind.manageWeb)
ob.set(SP.PermissionKind.managePermissions)
 
var per = web.doesUserHavePermissions(ob)
ctx.executeQueryAsync(
     function(){ 
          alert(per.get_value()); // If this is true, user has permission, if not, no 
        },
     function(a,b){
         alert ("Something wrong");
 }
);

You can find REST interface here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Melick
Sri Lanka Sri Lanka
No Biography provided

Comments and Discussions

 
QuestionYou copy-pated code from stackexchange Pin
Rockie(aka Collapse Troll)1-Nov-19 5:48
MemberRockie(aka Collapse Troll)1-Nov-19 5:48 
QuestionThanks For Sharing Pin
alan_usa4-Feb-14 7:47
Memberalan_usa4-Feb-14 7:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Technical Blog
Posted 5 Nov 2013

Tagged as

Stats

58.4K views
2 bookmarked