Ads

Tuesday, 2 April 2013

Check Whether a User is a Member of a SharePoint Group or Not Using ECMA Script and hide some filed according to User

<script type="text/javascript" src="../../Scripts/jquery-1.7.2.min.js"></script>

<script type="text/javascript">

ExecuteOrDelayUntilScriptLoaded(getWebUserData, "sp.js");

var context = null;
var web = null;
var _currentUser = null;
var usersMgr=null;
var groupMgr =null;
var groupCollection=null;


function getWebUserData()
{

context = new SP.ClientContext.get_current();

web = context.get_web();

this._currentUser = web.get_currentUser();

context.load(this._currentUser);

this.groupCollection = web.get_siteGroups();

this.groupMgr = groupCollection.getById(13); // ID Of the Group

this.usersMgr= groupMgr.get_users();

context.load(this.groupCollection);

context.load(this.groupMgr);

context.load(this.usersMgr);

context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod), Function.createDelegate(this, this.onFailureMethod));

}


function onSuccessMethod(sender, args)
{

var isManager=false;

var listEnumerator = this.usersMgr.getEnumerator();

while (listEnumerator.moveNext())
{
var item = listEnumerator.get_current();

userName = item.get_loginName();

if(userName == this._currentUser.get_loginName())
{
isManager=true;
break;
}

}

if(isManager==true)
{
$("nobr:contains('Approval Status')").
parent('h3').parent('td').parent('tr').show();
$("nobr:contains('Approval Comments')").
parent('h3').parent('td').parent('tr').show();

}
else
{
$("nobr:contains('Approval Status')").
parent('h3').parent('td').parent('tr').hide();
$("nobr:contains('Approval Comments')").
parent('h3').parent('td').parent('tr').hide();
}


}


function onFailureMethod(sender, args)
{
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}



</Script>

No comments:

Post a Comment

Ads