簡體   English   中英

您如何設置多個自定義字段? -Microsoft Project Server

[英]How do you set multiple custom fields? - Microsoft Project Server

我是Project Server開發的新手,我想知道以下代碼需要進行哪些修改,我必須讓它一次更新多個自定義字段。 我一切都准備就緒,直到要開始更新多個自定義字段為止。 我已經閱讀了許多教程,但沒有找到適合該問題的解決方案。 我整理的當前程序只會導致更新第一個ForEach cfValueWOD自定義字段。 我可以獲取更新多個字段(如果它們已經具有值)的代碼,但是對於我的項目,這些自定義字段可以具有初始值,也可以沒有值開始。 在這兩種情況下,我都需要將值寫入這些字段。 我需要很快完成一個正在工作的項目的工作,我很茫然。 您的幫助將不勝感激。 我當前的代碼如下:

{
    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);

    }

據我了解,您正在更新項目自定義字段。 更新自定義字段只不過是更新項目。 為此,首先必須簽出一個項目,更新自定義字段,然后調用“隊列發布”方法來保存和發布它。

但是在您的代碼中,您僅簽出一個項目。 因此,您可以更新僅屬於該項目的自定義字段。

為了更新多個自定義字段,則您的代碼應更具動態性。

范例:

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.
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM