none
How to Edit Custom Fields of Project Server 2010 Programatically RRS feed

  • Question

  • Hello,

    I want to change the Custom field value of Project Server 2010. I am following the brain article. My problem is the code run successfully but the changes did not reflect to the Project server. Following is my code, Any Help??

    private void UpdateRecordInProjectServer(string newValue, string projGuid, string propertyToEdit)
            {
                ProjectSoapClient projectSvc = new ProjectSoapClient();
                CustomFieldsSoapClient customfieldSvc = new CustomFieldsSoapClient();
                CustomFieldDataSet fieldDefs = customfieldSvc.ReadCustomFields(string.Empty, false);
                LookupTableSoapClient loockuptableSvc = new LookupTableSoapClient();

                Guid projectId = new Guid(projGuid);
                ProjectDataSet projectDs = projectSvc.ReadProject(projectId, ListProjects.Project.DataStoreEnum.WorkingStore);

                foreach (ProjectDataSet.ProjectCustomFieldsRow cfRow in projectDs.ProjectCustomFields.Rows)
                {
                   // projectDs.ProjectCustomFields.RemoveProjectCustomFieldsRow(cfRow);
                    CustomFieldDataSet.CustomFieldsRow fieldDefinition = fieldDefs.CustomFields.Single(
                            cfd => cfd.MD_PROP_UID == cfRow.MD_PROP_UID);
                    if (fieldDefinition.MD_PROP_NAME == propertyToEdit)
                    {
                        cfRow.TEXT_VALUE = newValue;
                    }
                    //projectDs.ProjectCustomFields.AddProjectCustomFieldsRow(cfRow);
                    projectDs.ProjectCustomFields.AcceptChanges();
                }
                
                Guid sessionUid = Guid.NewGuid();
                Guid jobUid = Guid.NewGuid();
                if (!IsProjectCheckedOut(projectId))
                {
                    projectSvc.CheckOutProject(projectId, sessionUid, "Updating CF");
                    jobUid = Guid.NewGuid();
                    projectSvc.QueueUpdateProject(jobUid, sessionUid, projectDs, false);
                    System.Threading.Thread.Sleep(4000);
                }
                jobUid = Guid.NewGuid();
                projectSvc.QueueCheckInProject(jobUid, projectId, false, sessionUid, "Updating CF");
                System.Threading.Thread.Sleep(4000);
                jobUid = Guid.NewGuid();
                projectSvc.QueuePublish(jobUid, projectId, true, SPContext.Current.Site.Url);
                System.Threading.Thread.Sleep(4000);
            }

    Muhammad Ali

    Thursday, April 11, 2013 8:55 AM

All replies

  • Hi Muhammad,

    firstly, does your custom field contain any value before update?

    If it does not, then you can not update it like this, you're going to have to add it before updating because Project Server can't see field that has no value. You need to add it with the line that you've commented:

    //projectDs.ProjectCustomFields.AddProjectCustomFieldsRow(cfRow);

    Secondly, you could try get the sessionUid from project:

    Guid oldSessionGuid = projectDs.Project[0].PROJ_SESSION_UID;

    and try to check out and  update with this.

    Thursday, April 11, 2013 11:50 AM
  • Thank you for your response.

    yes custom fields have values and i can change it through Microsoft Project Professional but cannot through Visual Studio

    About the session uid i am facing the following error on the Guid oldSessionGuid = projectDs.Project[0].PROJ_SESSION_UID; statement.

    The value for column 'PROJ_SESSION_UID' in table 'Project' is DBNull.


    Muhammad Ali

    Thursday, April 11, 2013 12:42 PM
  • When you debug your code, can you get to line:

    cfRow.TEXT_VALUE = newValue;

    Thursday, April 11, 2013 2:55 PM
  • Yes Absolutely...

    All the code including the the above statement debugged successfully


    Muhammad Ali


    Thursday, April 11, 2013 3:05 PM
  • Try this instead of your last section(after last "if" statement):

    jobUid = Guid.NewGuid();
                projectSvc.CheckOutProject(projectId, sessionUid, "Updating CF");
                System.Threading.Thread.Sleep(4000);

                projectSvc.QueueUpdateProject(jobUid, sessionUid, projectDs, false); 

              System.Threading.Thread.Sleep(4000);
                projectSvc.QueuePublish(jobUid, projectId, true, SPContext.Current.Site.Url);
                System.Threading.Thread.Sleep(4000);

    Here is QueueUpdateProject inserted before Publish and checkIn replaced with checkOut

    Thursday, April 11, 2013 3:37 PM
  • Brother,

    it will give you an exception " ProjectServerError(s) LastError=CICOCheckedOutInOtherSession Instructions: Pass this into PSClientError constructor to access all error information"

    on 

    projectSvc.CheckOutProject(projectId, sessionUid, "Updating CF"); statement

    because the project has already been checked out when you traversing on projects. so that's why i put a check on it if the project is already checked out skips that checkout statement.. Got it??

    I am still investigating whats wrong and i have to deliver it today :)


    Muhammad Ali

    Thursday, April 11, 2013 3:56 PM
  • But project doesn't have to be checked out to the same user that is executing the PSI

    This is how it has to be:

                        Guid? oldSessionUid;
                        try
                        {
                            oldSessionUid = projectDs.Project[0].PROJ_SESSION_UID;
                        }
                        catch (Exception)
                        {
                            oldSessionUid = null;
                        }

                        Guid sessionUid = Guid.NewGuid();
                        Guid jobUid;

                        if (oldSessionUid != null)
                        {
                            sessionUid = oldSessionUid.Value;
                            jobUid = Guid.NewGuid();
                            projectSvc.QueueCheckInProject(jobUid, projectId, true, sessionUid, "Updating CF");

                            System.Threading.Thread.Sleep(4000);
                        }

                        jobUid = Guid.NewGuid();
                        projectSvc.CheckOutProject(projectId, sessionUid, "Updating CF");
                        projectSvc.QueueUpdateProject(jobUid, sessionUid, projectDs, false);
                        System.Threading.Thread.Sleep(4000);

                        jobUid = Guid.NewGuid();
                        projectSvc.QueuePublish(jobUid, projectId, true, null);
                        System.Threading.Thread.Sleep(4000);

                        jobUid = Guid.NewGuid();
                        projectSvc.QueueCheckInProject(jobUid, projectId, true, sessionUid, "Updating CF");
                        System.Threading.Thread.Sleep(4000);

                            
    Thursday, April 11, 2013 7:33 PM
  • Its is giving an exception on projectSvc.CheckOutProject(projectId, sessionUid, "Updating CF"); statement.

    ProjectServerError(s) LastError=CustomFieldInvalidUID Instructions: Pass this into PSClientError constructor to access all error information.


    Muhammad Ali

    Friday, April 12, 2013 9:20 AM
  • I managed to resolve this issue but it is still not updating the project server entry. Give me any reason for that

    Muhammad Ali

    Friday, April 12, 2013 10:37 AM
  • Have you got any solution for updating the custom field? If so please let me know... I'm also facing issue in updating the values
    Tuesday, November 25, 2014 9:41 AM