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);
}
7 Responses to "SharePoint Client Object Model – Check user belongs to a Group – Disable controls in editfrm.aspx"
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
nice article. really helpful. but how to get the id of the user group?
August 30, 2011 at 7:09 am
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.
August 30, 2011 at 7:52 am
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