Client Object model Check user has Permission SharePoint 2010

A very interesting scenario came up last week when i wanted to restrict a user from opening a document when they don't have access to it. We were actually parsing a RSS feed from a document library and displaying docs in a custom webpart. Some of the users who do not have access to some of the doc where not only seeing the docs but were getting an Access denied page when they click on it. To avoid all that we decided to check for user's access on fly using the JavaScript Client object model and we would either hide it or give user a JavaScript alert popup for not having access to it.

Well the simple method that we were looking for was

currentItem.get_effectiveBasePermissions();

this method will give access for the current user on the selected list item

If you have to Check for a specific permission see the example below

To Check current users permissions using Ecmascript\Javascript client object model SharePoint 2010

function checkifUserHasEditPermissions()
{
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
}
}}


0 comments:

Post a Comment

Popular Posts