How to update values in a custom multi lookup field Field (lookup Table) of MS Project using JavaScript/jQuery?

 Updating values in the custom multi lookup field Field of MS Project using JavaScript/jQuery:

First of all we need to know the steps of Project Online for its update/add

Step 1: CheckOut a Project
Step 2: Update detals in same as in Step 1 Project
Step 3: Publish a Project
Step 4: CheckIn a Project

We always in need to follow the above steps to update any Project to MS Project/Project Server.
Same we need to follow in code too.



   //Step 1: First, we need to define all the required functions, the below Functions are callback functions
            window.checkOutPWAProject = function (projectId, callBack) {
                var checkOutURL = _spPageContextInfo.webAbsoluteUrl + "/_api/ProjectServer/Projects('" + projectId + "')/checkOut()";
                $.ajax({
                    url: checkOutURL,
                    type: "POST",
                    async: false,
                    headers: {
                        "accept": "application/json;odata=verbose",
                        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                        "content-Type": "application/json;odata=verbose",
                        "X-HTTP-Method": "POST",
                        "If-Match": "*"
                    },
                    success: function (res) {
                        callBack(true, 'Successfully checkout Project');
                    },
                    error: function (error) {
                        console.log(error.responseJSON.error.message.value);
                        callBack(false, error.responseJSON.error.message.value);
                    }
                });
            }

            

            function updateProjectLookupField(projectId, data, callBack) {
                // 
                $.ajax({
                    // url:_spPageContextInfo.webAbsoluteUrl+"/_api/ProjectServer/CustomFields('"+fieldID+"')",
                    url: _spPageContextInfo.webAbsoluteUrl + "/_api/ProjectServer/Projects('" + projectId + "')/Draft/UpdateCustomFields",
                    method: "POST",
                    async: false,
                    headers: {
                        "accept": "application/json;odata=verbose",
                        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                        "content-Type": "application/json;odata=verbose",
                        "X-HTTP-Method": "POST",
                        "If-Match": "*"
                    },
                    data: JSON.stringify(data),
                    success: function (response) {
                        console.log('Success-Update completed');
                        callBack(true, 'Data updated successfully');
                    },
                    error: function (e) {
                        var message = error.responseJSON.error.message.value;
                        console.log(message);
                        callBack(false, 'Something went wrong to update the data.');
                    }

                });
            }
            window.publishPWAProject = function (projectId, callBack) {
                var publishURL = _spPageContextInfo.webAbsoluteUrl + "/_api/ProjectServer/Projects('" + projectId + "')/Draft/publish(true)";
                $.ajax({
                    url: publishURL,
                    type: "POST",
                    async: false,
                    headers: {
                        "accept": "application/json;odata=verbose",
                        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                        "content-Type": "application/json;odata=verbose",
                        "X-HTTP-Method": "POST",
                        "If-Match": "*"
                    },
                    success: function (res) {
                        callBack(true, 'Successfully Publish Project');
                    },
                    error: function (error) {
                        console.log(error.responseJSON.error.message.value);
                        callBack(false, error.responseJSON.error.message.value);
                    }
                });
            }

            window.checkInPWAProject = function (projectId, callBack) {
                var checkInURL = _spPageContextInfo.webAbsoluteUrl + "/_api/ProjectServer/Projects('" + projectId + "')/Draft/checkIn(true)";
                $.ajax({
                    url: checkInURL,
                    type: "POST",
                    async: false,
                    headers: {
                        "accept": "application/json;odata=verbose",
                        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                        "content-Type": "application/json;odata=verbose",
                        "X-HTTP-Method": "POST",
                        "If-Match": "*"
                    },
                    success: function (res) {
                        callBack(true, 'Successfully CheckIn Project');
                    },
                    error: function (error) {
                        console.log(error.responseJSON.error.message.value);
                        callBack(false, error.responseJSON.error.message.value);
                    }
                });
            }
            window.getUpdatedProjectData = function (projectId) {
                var url1 = _spPageContextInfo.webAbsoluteUrl + "/_api/projectdata/Projects(guid'" + projectId + "')?$select=TestMultiLookup,LookupM,ProjectName,ProjectId";
                var url2 = _spPageContextInfo.webAbsoluteUrl + "/_api/projectdata/Projects?$select=TestMultiLookup,LookupM,ProjectName,ProjectId";
                $.ajax({
                    url: projectId != undefined ? url1 : url2,
                    method: "GET",
                    headers: {
                        "accept": "application/json;odata=verbose", //It defines the Data format 
                        "content-type": "application/json;odata=verbose" //It defines the content type as JSON
                    },
                    success: function (res) {
                        console.log(res);
                        // if (projectId == undefined) {
                        //     response.d.results.forEach(function(d){
                        //         console.log(d['TestMultiLookup'])
                        //     }); 
                        // }

                    },
                    error: function (e) {
                        console.log(e.responseJSON);
                    }

                });
            }

            //Finally We need ProjectID & Internal Name of custom multi value lookup field
            // I am taking Project Id
            var projectID = '7648a7e4-70lc-ea11-9c8a-d4bed9a8kh0p';//My Test 1 ;
            var customFieldInternalName = "Custom_i85e13f501c2eb11bb9700155dac5120";//multi value Lookup field
            checkOutPWAProject(projectID, function (state, message) {
                if (state) {
                    //for data we took key= Internal Name of Custom field, Value= Internal Name of lookup Entries
                    //like Entry_868ccc17d2a0ea11b07d00155da8573f for In Progress,
                    // Entry_518ccc17d2a0ea11b07d00575da8573f for Pending &
                    //Entry_548ccc48d2a0ea11b07d00155da8573f for Failed
                    // Note: After first value we need to add the semi colon and after first value add hash(#) before each value
                    //ValueType = Edm.String  i.e, type of Custom field
                    var dataToUpdate = {
                        "customFieldDictionary": [{
                            "Key": customFieldInternalName, // Test Text
                            // "Value": "Entry_518ccc17d2a0ea11b07d00575da8573f;#Entry_868ccc17d2a0ea11b07d00155da8573f;#Entry_548ccc48d2a0ea11b07d00155da8573f",
                            "Value": "Entry_868ccc17d2a0ea11b07d00155da8573f;#Entry_548ccc48d2a0ea11b07d00155da8573f", 
                            // Note: After first value we need to add the semi colon and after first value add hash(#) before each value
                            "ValueType": 'Edm.String'
                        }]
                    };
                    updateProjectLookupField(projectID, dataToUpdate, function (status, res) {
                        if (status) {
                            console.log(status);
                        } else {
                            console.log(res);
                        }
                    });
                    publishPWAProject(projectID, function (result, response) {
                        if (result) {
                            console.log(result);
                        } else {
                            console.log(response);
                        }
                    });
                    checkInPWAProject(projectID, function (result, response) {
                        if (result) {
                            console.log(result);
                        } else {
                            console.log(response);
                        }
                    });
                    setTimeout(() => {
                        getUpdatedProjectData(projectID);
                    }, 2000);

                } else {
                    console.log(message);
                }
            });

//Note: to get the Cutom fields of a Project from Project Server we need to follow below URL


//to get entries of a lookup fields or values to update a lookup field, follow below URL 

Output:

In output, we will update the Custom Field ralted to Project which contains the above Project Id.


References:
1. URL :

https://docs.microsoft.com/en-us/office/client-developer/project/bulk-update-custom-fields-and-create-project-sites-from-workflow-in-project

2. URL :

https://pwmather.wordpress.com/2018/05/21/using-rest-in-javascript-to-update-projectonline-project-custom-fields-ppm-pmot-jquery-office365/

Comments

Popular posts from this blog

Get list item entity type full name of a SharePoint list using REST API

Get SharePoint list name by GUID

Get Current Web Logged In User of SharePoint Site in jQuery