Project Online / Project Server: Project Detail Pages - Enhancements (Part 8)– Disable “Project Owner” Button on Project Detail Page

2019-05-05 | Barbara.Henhapl

This is the eighth part of articles discussing Project Detail Page enhancements:

  1. Show/hide a field depending on the value of an internal field on the same page
  2. Show/hide a field depending on the value of an Enterprise Custom field on the same page
  3. Show/Hide a field based on the value of an internal field using REST
  4. Show/Hide a field based on the value of an Enterprise Custom Field without Lookup Table using REST
  5. Remove Time from Enterprise Custom Fields on Project Detail Pages
  6. Remove Prefix from Enterprise Custom Fields on Project Detail Pages
  7. Hide Impact Ratings on Project Detail Page "Strategic Impact"
  8. Disable “Project Owner” Button on Project Detail Page

For a description for preparation of a Project Detail Page for JavaScript, see General Preparation

In common scenarios, project managers are allowed to edit some projects not owned by themselves. With allowing this, they are also able to change the owner of a project to any other project manager, what may not be the indented behavior. As an administrator, you can disable the Owner button to prevent them from changing. In the following sample, the button will be disabled for everyone, except the current project owner and members of the group “Web Administrators (Project Web App Synchronized)”. In this SharePoint group, you will find all members of default Project Server group “Administrators”.

If you are working in an on premise environment, you will need to catch active delegate sessions, see “This part is only necessary with On Premise”. In Project Online, you can remove this part.

<!-- Change path for jquery-2.1.1.min.js --> 
<script type="text/javascript" src="/sites/PWA/Scripts/jquery-2.1.1.min.js"></script>
<script type="text/javascript">

// add "Owner" in all used languages to this list
var buttonToHide = "Besitzer, Owner"


$(document).ready(ExecuteOrDelayUntilScriptLoaded(MainFunction, "sp.js"));

function MainFunction() {

         //This part is only necessary with On Premise - begin  
        if (WPSC.WebPartPage.WebURL.indexOf("sharepoint.com") == 0) {
            CheckDelegate();
            if (Delegate == true) {
                console.log("Delegate session active - REST not working on premise");
                return;
            }
        }
        //This part is only necessary with On Premise - end

		if(_spPageContextInfo.userId != GetProjectOwner(PDP_projUid) && _spPageContextInfo.userId != CheckAdmin()){
			//window.alert('User is not member of admin group or owner of the project');
			hideButton();
		};
}

//Get sharepoint user id of project owner
function GetProjectOwner(ProjectId) {
    var data = LoadRestSync(_spPageContextInfo.webAbsoluteUrl + "/_api/ProjectServer/Projects('" + ProjectId + "')/Owner");
    return data.d.Id;
}

//Check if user is member of group "Web Administrators (Project Web App Synchronized)" containing all members of default Project Server group "Administrators"
function CheckAdmin() {
    var data = LoadRestSync(_spPageContextInfo.webAbsoluteUrl + "/_api/Web/SiteGroups/getByName('Web%20Administrators%20(Project%20Web%20App%20Synchronized)')/Users()?$SELECT=Id&$Filter=Id eq " + _spPageContextInfo.userId);
    if(data.d.results[0] != undefined){
        return data.d.results[0].Id;
    }
    else{
        return 0;
    }
}
	
function hideButton() {
        var buttons = document.getElementsByTagName('button');

        for (var i = 0, len = buttons.length; i < len; ++i) {
            if (buttonToHide.indexOf(buttons[i].title) > -1) {
                buttons[i].style.display = 'none';
            }
        }
    }	
	
function LoadRestSync(url, methodType, parameters) {
    if (methodType === undefined || methodType == "") methodType = "GET";
    console.debug("LoadRestSync - Method:" + methodType);
    var result;
    console.log("LoadRestDataCrossSite: " + url);
    try {
        $.ajax({
            url: url,
            type: methodType,
            async: false,
            data: JSON.stringify(parameters),
            crossDomain: true,
            beforeSend: function (xhr) {
                xhr.withCredentials = true;
            },
            headers: {
                "accept": "application/json;odata=verbose",
                "Content-Type": "application/json;odata=verbose;charset=utf-8",
                "X-RequestDigest": _spPageContextInfo.formDigestValue
            },
            xhrFields: {
                withCredentials: true
            },
            success: function (data) {
                result = data;
            },

            error: function (data, errorCode, errorMessage) {
                console.error(data);
                console.error(errorCode);
                console.error(errorMessage);
            }

        });
    }
    catch (err) {
        console.error(err);
        console.error(url);
    }
    return result;
}	
</script>

Copy above code into a text editor, modify at least the marked sections, save with file extension "js" or "html", and upload to your script library.

Categories: Project

Source: https://blog.atwork.at/post/2019/05/05/Project-Online-Project-Server-Project-Detail-Pages-Enhancements-(Part-8)-Disable-Change-Project-Owner-Button-on-Project-Detail-Page