Client Object Model to Check user belongs to a Group and Hide controls in New/Edit/Display Forms

Problem: Check user belongs to a SharePoint Group and Hide some controls in New/Edit/Display Forms on SharePoint  Custom List

The above problem can be solved with the below link only if you use infopath in document or form library

http://www.projectpoint.at/?p=97

If you are using SharePoint list, you have to use the below steps to meet this requirement

Solution 

The following Script which will help you in checking if the current logged in SharePoint user belongs to a SharePoint user group and based on it hide some controls in New/Edit/Display aspx forms on Custom List.

ExecuteOrDelayUntilScriptLoaded(hideControls, “sp.js”);

function hideControls()
{
clientContext = new SP.ClientContext();
groupCollection = clientContext.get_web().get_siteGroups();
group = groupCollection.getById(10); //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.onOperationSucceeded), Function.createDelegate(this,
this.onOperationFailed));
RefreshCommandUI();
}

function onOperationSucceeded()
{
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)
{
}
else
{
document.getElementById(“testid”).style.display=”none”;}
}

}

function onOperationFailed(sender, args)
{
document.getElementById(“testid”).style.display=”none”;
}

Thank you Anil for your contribution to fix this issue

One thought on “Client Object Model to Check user belongs to a Group and Hide controls in New/Edit/Display Forms

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.