Microsoft Land

SharePoint Client Object Model – Check user belongs to a Group – Disable controls in editfrm.aspx

Posted on: August 17, 2011


Just wanted to Share a Script which will help in checking if the Logged in User belongs to a SharePoint user group and based on it disable some controls in editfrm.aspx on Custom List.  It uses JQUERY.

 ExecuteOrDelayUntilScriptLoaded(disableControls, “sp.js”);
function disableControls()
{
clientContext = new SP.ClientContext();
groupCollection = clientContext.get_web().get_siteGroups();
group = groupCollection.getById(21); //The ID of the SharePoint user group
users = group.get_users();
clientContext.load(group);
clientContext.load(users);
currentUser = clientContext.get_web().get_currentUser();
clientContext.load(currentUser);
clientContext.executeQueryAsync(Function.createDelegate(this,
this.onQuerySucceeded), Function.createDelegate(this,
this.onQueryFailed));
RefreshCommandUI();
}
function onQuerySucceeded()
{
if(users.get_count() >0)
{
UserExistInGroup = false;
for(var i=0; i < users.get_count(); i++)
{
if(users.itemAt(i).get_loginName() == this.currentUser.get_loginName())
{
UserExistInGroup = true;
break;
}
}
}
if (UserExistInGroup)
{
var p=$(‘input[title=Priority’); //Name of the columns that needs to be disabled
var i=$(‘input[title=Range]’); //Name of the columns that needs to be disabled
$($(p)[0]).attr(‘disabled’,false);
$($(i)[0]).attr(‘disabled’,false);
}
else
{
var p=$(‘input[title=Priority’);
var i=$(‘input[title=Range’);
$($(p)[0]).attr(‘disabled’,true);
$($(i)[0]).attr(‘disabled’,true);
}
}
function onQueryFailed(sender, args)
{
var p=$(‘input[title=Priority’);
var i=$(‘input[title=Range]’);
$($(p)[0]).attr(‘disabled’,true);
$($(i)[0]).attr(‘disabled’,true);
}

About these ads

8 Responses to "SharePoint Client Object Model – Check user belongs to a Group – Disable controls in editfrm.aspx"

Thanks a lot. Its really helpful..
After implementing its working for me but when i logged in with different group user then i am getting below error.
“Access denied. You do not have permission to perform this action or access this resource”
Throws an exception. How to resolve this please help, your help would be highly appreciated.

Hi Milind,

Is it throwing the error when you are editing the item? Does the logged in user has access to the list / JS file in which you have included this script? Send me the screenshot if possible, Will see if i can help you. sriram.kunapuli@live.com

Best Regards
Sriram

I have written this code in custom aspx page in sharepoint designer to update dropdown status on user role basis. User who have contribution rights on site come to this page that time he receives this error.

Hello sriram,

one strange thing happened in my sharepoint 2010 environment. After installing 2010 sp1, there were core groups like Home owners, Home members, Home visitors gone from top-most sharepoint site collection. Now situation is like, my old code for custom webpart is stopped working and throwing below error.

Request failed. Cannot invoke method or retrieve property from null object. Object returned by the following call stack is null. “AssociatedMemberGroup

Web

Microsoft.SharePoint.SPContext.Current

Do you have an idea what to do now?

Actually i am using client object model for my silverlight webpart. I want to check current user’s permission whether it’s contribute, read, full control. I want to achieve this only. Before i was achieving this simply by below code :
_MemberGroup = _Context.Web.AssociatedMemberGroup;
_MemberUserCollectionInGroup = _MemberGroup.Users;
_Context.Load(_MemberUserCollectionInGroup);

_VisitorGroup = _Context.Web.AssociatedVisitorGroup;
_VisitorUserCollectionInGroup = _VisitorGroup.Users;
_Context.Load(_VisitorUserCollectionInGroup);

I tried creating these groups manually with corresponding permission, but its not working.

thanks & regards,
palak bhansali

Hi,

I am not aware of any issues where in the SP1 patching is removing the OOB SP Groups from Top Level sites. Will update the post after doing some research.

But for now, as you said you have created the User Groups manually, it would be easier for you to get the list of users in that group using Group ID instead of Associated* Memebers of SPWeb as i described above.

Best Regards
Sriram

Hello sriram,

Thanks for quick response and for your time to look into my problem. actually that’s true that there is no in-built home members/owners/visitors group in my sharepoint portal top site-collection after installing sp1. Even i created all three groups with pre-defined permission-level. but it’s not working at all.

Actually i am not worried about this, my problem is now i am not be able to get users with specific permission-level with the use of RoleAssignmentCollection RoleDefinitionscollection.
Actually i want to check currentuser’s (_Context.Web.CurrentUser) permission-level like contribute/full control/read. depending on this,i’ll do my work.

I am using client object model for silverlight. very similar to above code(javascript object model). i want to achieve this only if you have an idea how can i achieve this.

thanks
palak

nice article. really helpful. but how to get the id of the user group?

Very nice. I’m on SharePoint Online 2013 and sp.js is available. Looking to test is a user has full control. Any suggestion?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

My Tweets

Follow

Get every new post delivered to your Inbox.

Join 121 other followers

%d bloggers like this: