Wednesday 28 November 2012

Check the user in a particular group and check it with the current logged in user to hide/disable controls

Below is the script to check the user in a particular group and check it with the current logged in user using JavaScript and Jquery.

If current user is present in desired group than you can implement any of the logic.
I have implemented disabling controls and hiding tables.

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(can be checked in group URL)
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=col1'); //Name of the columns that needs to be disabled
var i=$('input[title=col2]‘); //Name of the columns that needs to be disabled
$($(p)[0]).attr(‘disabled’,false);
$($(i)[0]).attr(‘disabled’,false);
document.getElementById("Table1").style.display = 'none';
}
else
{
var p=$(‘input[title=col1');
var i=$('input[title=col2');
$($(p)[0]).attr(‘disabled’,true);
$($(i)[0]).attr(‘disabled’,true);
document.getElementById("Table1").style.display = 'inline';
}
}
function onQueryFailed(sender, args)
{
var p=$(‘input[title=col1');
var i=$('input[title=col2]‘);
$($(p)[0]).attr(‘disabled’,true);
$($(i)[0]).attr(‘disabled’,true);
document.getElementById("Table1").style.display = 'inline';
}

No comments:

Post a Comment