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

Advertisements

About Joseph Velliah
As a SharePoint Developer my professional interests tend to be technical and SharePoint focused. I run a blog at "SP RIDER" where you can expect to read HOW TOs and scenarios that I run into during my day to day job. I hope my posts will give back a little to the community that is helped me.

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

Leave a Reply

Please log in using one of these methods to post your comment:

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

%d bloggers like this: