Project Server 2013 Make fields Read Only in PDP depending on SharePoint Groups the logged user belong to

I had a requirement depending on the SharePoint group the user belong some fields need to be read only.

The script below disable the field  “Project Code” on the PDP (Project Detail Page) if the user is not in the SharePoint group “Project Code”.

Copy the code snippet to a script editor web part in a PDP page.

jQuery.noConflict();

function IsCurrentUserMemberOfGroup(groupName, OnComplete) {
var currentContext = new SP.ClientContext.get_current();
var currentWeb = currentContext.get_web();
var currentUser = currentContext.get_web().get_currentUser();
currentContext.load(currentUser);

var allGroups = currentWeb.get_siteGroups();
currentContext.load(allGroups);

var group = allGroups.getByName(groupName);
currentContext.load(group);

var groupUsers = group.get_users();
currentContext.load(groupUsers);

currentContext.executeQueryAsync(OnSuccess,OnFailure);

function OnSuccess(sender, args) {
var userInGroup = false;
var groupUserEnumerator = groupUsers.getEnumerator();
while (groupUserEnumerator.moveNext()) {
var groupUser = groupUserEnumerator.get_current();
if (groupUser.get_id() == currentUser.get_id()) {
userInGroup = true;
break;
}
}
OnComplete(userInGroup);
}

function OnFailure(sender, args) {
OnComplete(false);
}
}

function IsCurrentUserHasContribPerms()
{
if (!jQuery(‘input[title=”Project Code”]’).prop(‘disabled’) )
{
console.log(jQuery(‘input[title=”Project Code”]’).prop(‘disabled’));
IsCurrentUserMemberOfGroup(“Project Code”, function (isCurrentUserInGroup) {
if(isCurrentUserInGroup)
{
jQuery(‘input[title=”Project Code”]’).prop(‘disabled’,false);
}
else
{
// alert(“user not in group”);
jQuery(‘input[title=”Project Code”]’).prop(‘disabled’,true);
}
});
}

}
ExecuteOrDelayUntilScriptLoaded(function(){setInterval(IsCurrentUserHasContribPerms, 250)}, ‘SP.js’);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

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