Though SharePoint handles security trimming throughout the site, sometimes when you need to create custom pages and controls, you would need to check the permissions of the logged in user to allow/block certain actions. For example, only if a user has permission to edit list items, show Edit button. So how do you check if the logged-in user has the required permission or not?
SharePoint provides a REST endpoint "effectiveBasePermissions". Using this endpoint, you can check if the user has required permission or not.
THE URL syntax is:
Interestingly, this endpoint returns data as JavaScript Object Model (JSOM) object. So to use this data, you would need to load two libraries sp.runtime.debug.js and sp.debug.js. These libraries are available at 15 hive folder.
Let me show you the complete code (jQuery should be loaded for this code). This code is tested in SharePoint Online and it should work in on-premises as well.
EffectiveBasePermissions will have the High and Low values (related to Permission) which is difficult to make anything out of it. So we load these two numbers to a BasePermissions object and use "has" method to check the permission. Do note the parameter passed to this method. It is an enumeration of SP.PermissionKind. This method returns boolean value. In this case we are checking if the user has permission to edit List items (editListItems). To get the list of Enumeration values, check this MSDN article.
If you observe the example above, we are checking the user permission for a specific List. Similarly, you can check the user permission at site level or item level.
SharePoint provides a REST endpoint "effectiveBasePermissions". Using this endpoint, you can check if the user has required permission or not.
THE URL syntax is:
[Domain]/[Site]/_api/web/lists/getByTitle('ListName')/effectiveBasePermissions
.Interestingly, this endpoint returns data as JavaScript Object Model (JSOM) object. So to use this data, you would need to load two libraries sp.runtime.debug.js and sp.debug.js. These libraries are available at 15 hive folder.
Let me show you the complete code (jQuery should be loaded for this code). This code is tested in SharePoint Online and it should work in on-premises as well.
$(document).ready(function(){
$.ajax({
type: "GET",
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('CustomList')/effectiveBasePermissions",
headers: {"accept": "application/json; odata=verbose"},
success: function(data){
var perm = new SP.BasePermissions();
perm.fromJson(data.d.EffectiveBasePermissions);
var hasPermission = perm.has(SP.PermissionKind.editListItems);
if(hasPermission) {
alert("User has permission to Edit the List");
}
else {
alert("User has no permission to Edit the List");
}
},
error: function(error){
alert("Error");
console.log(error);
}
})
});
EffectiveBasePermissions will have the High and Low values (related to Permission) which is difficult to make anything out of it. So we load these two numbers to a BasePermissions object and use "has" method to check the permission. Do note the parameter passed to this method. It is an enumeration of SP.PermissionKind. This method returns boolean value. In this case we are checking if the user has permission to edit List items (editListItems). To get the list of Enumeration values, check this MSDN article.
If you observe the example above, we are checking the user permission for a specific List. Similarly, you can check the user permission at site level or item level.
what is Domain and what is SITE anmd what is a LIST ? do you have a complete url i could see ? thanks.
ReplyDelete