locked
Can I populate a Sharepoint list field with data from PWA, ie %complete or duration? RRS feed

  • Question

  • Hi,

    Can I populate a Sharepoint list field with data from PWA, ie %complete or duration?

    Thnxs

    Tuesday, February 5, 2019 7:11 PM

All replies

  • Hello Matium,

    Yes. You can use jQuery with REST to pull data from PWA in to SharePoint list.

    Regards


    Tanzim Akhtar

    Wednesday, February 6, 2019 9:14 AM
  • Thank you, is there somewhere I can see samples of this as I am not a coder.


    • Edited by Matium Friday, May 3, 2019 11:25 AM
    Friday, May 3, 2019 11:21 AM
  • The following script takes the custom users from PWA and update the SharePoint list. This code should be in the Script Editor in the SharePoint list.
    <script type="text/javascript" src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <script language="javascript">
    $(function(){
        var restURLProjSite = _spPageContextInfo.webAbsoluteUrl + "/_api/web/AllProperties?$select=mspwaprojuid"
        getProjectUID(restURLProjSite)
    });
    
        function getProjectUID(restURLProjSite)
        {
            $.ajax({
                url: restURLProjSite,
                type: "GET",
                headers: {
                    "accept": "application/json;odata=verbose",
                },
                success: function(data)
                {
                    var projUID = data.d["MSPWAPROJUID"];
                    var restURLProjectData = _spPageContextInfo.siteAbsoluteUrl + "/_api/projectdata/Projects(guid'" + projUID + "')";
                    getProjectDataFields(restURLProjectData);
                },
                error: function(error)
                {
                    alert(JSON.stringify(error));
                }
            })
        }
        
        function getProjectDataFields(restURLProjectData)
        {
            $.ajax({
                url: restURLProjectData,
                type: "GET",
                headers: {
                    "accept": "application/json;odata=verbose",
                },
                success: function(data)
                {
                    var supervisorName = data.d["Supervisor"];
                    var managingDirectorName = data.d["ManagingDirector"];
                    var projectLeadName = data.d["ProjectOwnerName"];
                    var projectTitle = data.d["ProjectTitle"];
                    var userURL = _spPageContextInfo.siteAbsoluteUrl + "/_api/web/siteusers?$filter=Title eq '" + supervisorName + "' or Title eq '" + managingDirectorName + "' or Title eq '" + projectLeadName + "'";
                    getProjStakeholdersUserId(userURL,supervisorName,managingDirectorName,projectLeadName,projectTitle);
                },
                error: function(error)
                {
                    alert(JSON.stringify(error));
                }
            })
        }
        
        function getProjStakeholdersUserId(userURL,supervisorName,managingDirectorName,projectLeadName,projectTitle)
        {
            $.ajax({
                url: userURL,
                type: "GET",
                headers: {
                    "accept": "application/json;odata=verbose",
                },
                success: function(data)
                {
                    var supervisorId = 0;
                    var managingDirectorId = 0;
                    var projectLeadId = 0;
                    $.each(data.d.results, function(index, item)
                           {
                                if (item.Title == supervisorName)
                                {
                                    supervisorId = item.Id;
                                }
                                if (item.Title == managingDirectorName)
                                {
                                    managingDirectorId = item.Id;
                                }
                                if (item.Title == projectLeadName)
                                {
                                    projectLeadId = item.Id;
                                }
                           });
                    getListItemId(supervisorId,managingDirectorId,projectLeadId,projectTitle);
                },
                error: function(error)
                {
                    alert(JSON.stringify(error));
                }
            })
        }
        
        function getListItemId(supervisorId,managingDirectorId,projectLeadId,projectTitle)
        {
            listURL = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('Project Data Fields')/items?$orderby=ID desc&$top 1";
            $.ajax({
                url: listURL,
                type: "GET",
                headers: {
                    "accept": "application/json;odata=verbose",
                },
                success: function(data)
                {
                    var itemId = 0;
                    $.each(data.d.results, function(index, item)
                           {
                                itemId = item.Id;
                           });
                    setProjectDataFields(itemId,supervisorId,managingDirectorId,projectLeadId,projectTitle);
                },
                error: function(error)
                {
                    alert(JSON.stringify(error));
                }
            })
        }
        
        function setProjectDataFields(itemId,supervisorId,managingDirectorId,projectLeadId,projectTitle)
        {
            var listItemURL = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('Project Data Fields')/items(" + itemId + ")";
            var fieldData = {
                __metadata: { 'type': 'SP.Data.Project_x0020_Data_x0020_FieldsListItem' },
                SupervisorId: supervisorId,
                Managing_x0020_DirectorId: managingDirectorId,
                Project_x0020_LeadId: projectLeadId,
                Title: projectTitle
            };
            
            $.ajax({
                url: listItemURL,
                type: "MERGE",
                data: JSON.stringify(fieldData),
                headers: {
                    "accept": "application/json;odata=verbose",
                    "content-type": "application/json;odata=verbose",
                    "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                    "IF-MATCH": "*"
                },
                success: function(data)
                {
                    //alert("Project Fields Data updated successfully");
                },
                error: function(error)
                {
                    alert(JSON.stringify(error));
                }
            })
        }
    </script>
    Regards

    Tanzim Akhtar


    • Edited by Tanzim Tuesday, May 7, 2019 10:07 AM
    Tuesday, May 7, 2019 10:06 AM