Project Online / Project Server: Project Detail Pages - Enhancements (Part 5)–Remove Time from Enterprise Custom Fields on Project Detail Pages

2019-01-31 | Barbara.Henhapl

This is the fifth 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

If there are Enterprise Custom Fields of type “Date”, these fields will always contain the time by default. In Project Center a user can toggle display of time.

image

On Project Detail Pages, there is no such option available. However, as an administrator, you can apply some JavaScript to show dates without time. The following script is not working with any standard date functions to reflect any setting the user has chosen as individual regional settings in his or her profile. Fortunately date and time are always separated by a “ “ in all regional settings I am aware of. So we will just look for a “ “ in all date fields to strip the time. Function GetProjectCustomFields will return all fields of type date, so there is not additional configuration required.

Please note:

This code will not work in delegate session in on premise environments since we will retrieve all ECF of type “Date” with REST. Within a delegate session, REST is not available per 2019-01-31 in Project Server 2013, 2016 and 2019. To test, open a delegate session and enter "http(s)://<SERVER>/PWA/_api/ProjectServer/CustomFields" as address in browser. You will get the following result:

image


<!-- 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">

    // This script will hide the time from Enterprise Custom Fields of type Date

    var RESTAppendix = "/_api/ProjectServer/CustomFields()?$Filter=FieldType eq 4" // 4 is date
    
    //A global variable is necessary
    var Delegate;  // Online necessary in on premise environments
    var FieldList = [];

    //Call main function
    $(document).ready(ExecuteOrDelayUntilScriptLoaded(MainFunction, "sp.js"));

    //MainFunction 
    function MainFunction() {

        //Use this part only 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;
            }
        }
        //Use this part only with On Premise - end

        //function to retrieve custom fields
        GetProjectCustomFields(RESTAppendix);

        if (state==true){   //state = Checked-Out
            for (i = 0; i < FieldList.length; i++) {
                FieldToFormat = document.getElementById("idCF_" + FieldList[i].Id);
                if(FieldToFormat != undefined){
                    //not using toLocale, since settings can be different
                    FieldToFormat.value = FieldToFormat.value.substring(0,FieldToFormat.value.indexOf(" "));
                }
            }
        }
        else {
            for (i = 0; i < FieldList.length; i++) {
                FormatElementsByGuid(FieldList[i].Id);
            }
        }
    }


    function CheckDelegate() {
        var data = $.ajax({
            // we need the result, therefore "async: false"
            async: false,
            url: _spPageContextInfo.siteAbsoluteUrl + "/_api/Projectserver()?$SELECT=IsDelegate",
            type: "GET",
            dataType: "json",
            headers: {
                Accept: "application/json;odata=verbose"
            }
        });

        data.done(function(data) {
            if (data.d.results == undefined) {
                Delegate = data.d.IsDelegate;
            }
        });
    }


    //Get ProjectSettings: 
    function GetProjectCustomFields(REST) {

        var data = $.ajax({
            async: false,
            url: _spPageContextInfo.siteAbsoluteUrl + REST,
            type: "GET",
            dataType: "json",
            headers: {
                Accept: "application/json;odata=verbose"
            }
        });
        data.done(function (data) { 
            if (true) {
                var ResultData = data.d.results;
                for (i = 0; i < ResultData .length; i++) {
                    var DateFields = {};
                    DateFields.Id = ResultData[i]["Id"];
                    DateFields.Name = ResultData[i]["Name"]; 
                    FieldList[i] = DateFields;
                }
            }
        })
        
    }

    //This function will return the value of Enterprise Custom Field if it is available on the page of a checked-in project
    function FormatElementsByGuid(guid) {
        var nodeList = document.getElementsByTagName('*');
        var nodeArray = [];
        var iterator = 0;
        var node = null;

        while (node = nodeList[iterator++]) {
            //Nothing will happen, if field not on page
            if (node.hasAttribute("guid") && node.outerHTML.startsWith('<div guid="' + guid + '"')) {
                node.innerText = node.innerText.substring(0,node.innerText.indexOf(" "));
            }
        }
    }

</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/01/31/Project-Online-Project-Server-Project-Detail-Pages-Enhancements-(Part-5)-Remove-Time-from-Enterprise-Custom-Fields-on-Project-Detail-Pages