Wednesday, January 3, 2018

Disable or hide project server custom fields in project detail pages based on group current user belong too

Leave a Comment


Business Case:

Only PMO coordination team should able to update/set information related to project status. Currently project managers are updating project status which are affecting business validations & process flow.

Solution:
1.       Create a SharePoint group “PMO_Users”. Add PMO coordination team members to this group.
2.       Whenever a project is edited or created, at project details page, our custom module will verify if currently logged in user is part of “PMO_Users” group.
3.       If point (2) is true then project status button is enable and PMO coordination team can only able to update project status.
4.       Project managers can’t able to update project status (as they are not part of PMO_Users group.

Understanding:
This module works on the basis that project status (custom field) can’t be updated from Microsoft Project (client application) and only updateable from PWA.

Cross-Browser compatibility:
I have verified this module on chrome & internet explorer. 

Script/Code:

<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script>
ExecuteOrDelayUntilScriptLoaded(DisableProjectStatus,'sp.js');
function DisableProjectStatus()
{
var clientContext = new SP.ClientContext.get_current();
    var oWeb = clientContext.get_web();
    var currentUser = oWeb.get_currentUser();
    var allGroups = currentUser.get_groups();
    clientContext.load(allGroups);

    clientContext.executeQueryAsync(OnSuccess, OnFailure);

    function OnSuccess(sender, args)
    {
        var grpsEnumerator = allGroups.getEnumerator();
        var isNotPartOfPMO_Users=false;
        while(grpsEnumerator.moveNext())
        {          
            var group = grpsEnumerator.get_current();                  
            var grpTitle = group.get_title();
            if(grpTitle=='PMO_Users')
            {
                            isNotPartOfPCOAdmin=true;              
            }          
        }
        if(!isNotPartOfPMO_Users)
        {
            console.log('im not admin  user');
            var btnProjectStatus = $("button[id*='pfp_Repeater_ctl12_idCF_Btn_']");
            btnProjectStatus.attr("disabled", "disabled"); //similarly we can hide button as well.
        }
    }

    function OnFailure(sender, args)
    {
        console.log(args.get_message());
    }  
}
If You Enjoyed This, Take 5 Seconds To Share It

0 comments: