2

I’m fairly new to Project Server development and was wondering what modifications need to be made the following code I have to have it update multiple Custom Fields in one pass. I have everything working up until the point I want to start updating multiple Custom Fields. I’ve gone through numerous tutorials and haven’t found a solution that works for this issue. The current program I’ve put together only results in updating the first ForEach cfValueWOD Custom Field. I can get the code to update multiple fields if they already have a value but for my project these custom fields can either have an initial value or no value to start. In both cases I will need to write values to these fields. I need to complete this for a project at work very soon and I’m at a loss. Your help will be much appreciated. My current code is as follows:

{
    static void WriteCustomFields()
    {
        //Guids for custom fields to update - Test
        string cfNameWOD = "WO Description"; //Test WO Description custom field
        string cfValueWOD = "xxxx5WODes";
        Guid cfIdWOD = new Guid("{8071365c-1375-46a1-9424-cd79f3c2b0db}");

        string cfNameWG = "Work Group"; //Test Work Group custom field
        string cfValueWG = "xxxx5Group";
        Guid cfIdWG = new Guid("{f75c6cfb-b7cb-4d35-8b04-60efb12fcd39}");                

        //projects into a dataset
        ProjectDataSet projectList = projectSvc.ReadProjectList();

        //read project data
        Guid myProjectUid = new Guid("{c96bd7ea-e9d2-47ed-8819-02e4653e92a7}");
        ProjectDataSet myProject = projectSvc.ReadProject(myProjectUid, DataStoreEnum.WorkingStore);

        //indicate the custom field has been found
       bool customFieldFound = false;

        //iterate over fields and update them to the table for WO Status
        foreach (ProjectDataSet.ProjectCustomFieldsRow cfRow in myProject.ProjectCustomFields)
        {
            //if field exists update it
            if (cfRow.MD_PROP_UID == cfIdWOD)
            {
                //update the value
                cfRow.TEXT_VALUE = cfValueWOD;
                customFieldFound = true;
            }

        }
        //check if the custom field has been found
        if (!customFieldFound)
        {
            //create a new row
            ProjectDataSet.ProjectCustomFieldsRow cfRowWOD =
                myProject.ProjectCustomFields.NewProjectCustomFieldsRow();

            //Sets all values to NUll to begin
            cfRowWOD.SetDATE_VALUENull();
            cfRowWOD.SetTEXT_VALUENull();

            //General parameters
            cfRowWOD.MD_PROP_UID = cfIdWOD; //custom field ID
            cfRowWOD.CUSTOM_FIELD_UID = Guid.NewGuid();
            cfRowWOD.PROJ_UID = myProjectUid; //current project ID

            //add value
            cfRowWOD.FIELD_TYPE_ENUM = 21;
            cfRowWOD.TEXT_VALUE = Convert.ToString(cfValueWOD); //test value

            //add the row to the data set
            myProject.ProjectCustomFields.AddProjectCustomFieldsRow(cfRowWOD);
        }

        //iterate over fields and update them to the table for WO Status
        foreach (ProjectDataSet.ProjectCustomFieldsRow cfRow in myProject.ProjectCustomFields)
        {
            //if field exists update it
            if (cfRow.MD_PROP_UID == cfIdWG)
            {
                //update the value
                cfRow.TEXT_VALUE = cfValueWG;
                customFieldFound = true;
            }

        }
        //check if the custom field has been found
        if (!customFieldFound)
        {
            //create a new row
            ProjectDataSet.ProjectCustomFieldsRow cfRowWG =
                myProject.ProjectCustomFields.NewProjectCustomFieldsRow();

            //Sets all values to NUll to begin
            cfRowWG.SetDATE_VALUENull();
            cfRowWG.SetTEXT_VALUENull();

            //General parameters
            cfRowWG.MD_PROP_UID = cfIdWG; //custom field ID
            cfRowWG.CUSTOM_FIELD_UID = Guid.NewGuid();
            cfRowWG.PROJ_UID = myProjectUid; //current project ID

            //add value
            cfRowWG.FIELD_TYPE_ENUM = 21;
            cfRowWG.TEXT_VALUE = Convert.ToString(cfValueWG); //test value

            //add the row to the data set
            myProject.ProjectCustomFields.AddProjectCustomFieldsRow(cfRowWG);
        }

        //generate sessionId for tracking
        Guid sessionId = Guid.NewGuid(); //sessionId for updating process
        Guid jobId = Guid.NewGuid(); //ID for each job

        //check out project
        projectSvc.CheckOutProject(myProjectUid, sessionId,
            "update checkout");

        //update project database
        bool validateOnly = false;
        projectSvc.QueueUpdateProject(jobId, sessionId,
            myProject, validateOnly);

        //wait to finish
        WaitForJob(jobId);

        //new jobId to check in the project
        jobId = Guid.NewGuid();

        //check in the updated project
        bool force = false;
        string sessionDescription = "update custom fields";
        projectSvc.QueueCheckInProject(jobId, myProjectUid,
            force, sessionId, sessionDescription);

        //wait to finish
        WaitForJob(jobId);

        //new jobId to publish the project
        jobId = Guid.NewGuid();
        bool fullPublish = true;
        projectSvc.QueuePublish(jobId, myProjectUid, fullPublish, null);

        //wait to finish
        WaitForJob(jobId);

    }
bpmill
  • 21
  • 3
  • Please ignore the ReadCustomFields portion. I’m also working on a piece of the code that will read the fields by a lookup. – bpmill Apr 25 '14 at 23:20
  • Does anyone have any suggestions on how to get this looping correctly to set values to custom fields that either have no initial value or a current value that needs to be over written? – bpmill Apr 29 '14 at 21:52
  • For anyone that may wonder how to do this I was able to solve my problem by doing the following and just repeating this section tovarious fiels I need to update: – bpmill May 30 '14 at 21:37
  • For anyone that may wonder how to do this I was able to solve my problem by doing the following and just repeating this section to various fields I need to update. I moved the 'bool customFieldFound = false;' to inside each update/create logic above the 'foreach (ProjectDataSet.ProjectCustomFieldsRow cfRow in myProject.ProjectCustomFields)' – bpmill May 30 '14 at 21:46

1 Answers1

1

As per my understanding, you are updating a project custom field. Updating a custom field is nothing but up updating a project. In order to do that, first you have to check out a project,Update the custom field, and then call the Queue publish method to save and publish it.

But in your code, You are checking out only one project. So you can update the custom fields that belongs to only that project.

Inorder to update multiple custom fields then your code should be more dynamic.

Example :

read project Guid dynamically.
Loop in thru the project Guid's, then
{
Get Custom field Dataset.
Read custom field dataset.
compare custom fields guids, pick the custom field values based on Project Guid and Custom Field Guid.
Set the value and finally update it.
}
Sasidharan
  • 3,676
  • 3
  • 19
  • 37
  • is this code check-out then check-in automatically and then save those modifications ? and then publish it? or not? – saber tabatabaee yazdi Jul 24 '16 at 06:55
  • if project is checked in, then do checkout,update,publish. If a project is checked out to another user, then checkin, checkout with new sessionid,update and publish. – Sasidharan Aug 02 '16 at 09:19