Project Server: Updating and Deleting Custom Fields using the PSI

Following on from my posting on adding custom fields http://blogs.msdn.com/b/brismith/archive/2010/08/25/project-server-adding-custom-fields-to-projects-and-tasks-using-the-psi.aspx I had a few questions on changing fields – or deleting them.  So here are a couple of short samples that follow the same code from the previous sample and show how to update a task level custom field and also to delete a custom field.  In my sample I have hard-coded the GUID for my project and for my custom field.  And my project just has one task, so in the real world you would also need to ensure you were acting on the right task – otherwise my code would update every occurrence of that custom field for every task – or delete every occurrence…  Also when I am creating these samples I usually start from the LoginDemo sample and just add buttons to do the stuff I am playing with.  So the previous sample created a project – these two just update the CF or delete it.  Obviously I am updating a Text custom field at the task level – if you are updating another custom field type you would need to change the TEXT_VALUE as appropriate.  Also be a good idea to see if the row exists before updating or deleting it – but I’ll leave that to you.  This was created with 2007 but the logic would be the same in 2010.

        private void btnUpdateCF_Click(object sender, EventArgs e)
        {
            Guid taskTextGuid = new Guid("30665299-bc21-4c51-b954-220d407ba47e");
            Guid projectId = new Guid("4c1cef33-4810-4159-b2e1-db15ea19a28b");
            WebSvcProject.ProjectDataSet projectDs = 
                project.ReadProject(projectId, WebSvcProject.DataStoreEnum.WorkingStore);

            foreach (WebSvcProject.ProjectDataSet.TaskCustomFieldsRow cfRow in 
                projectDs.TaskCustomFields)
            {
                if (cfRow.MD_PROP_UID == taskTextGuid)
                {
                    cfRow.TEXT_VALUE = "Updated Value";
                }
            }
            Guid sessionUid = Guid.NewGuid();


            project.CheckOutProject(projectId, sessionUid, "Updating CF");
            Guid jobUid = Guid.NewGuid();
            project.QueueUpdateProject(jobUid, sessionUid, projectDs, false);
            jobUid = Guid.NewGuid();
            project.QueueCheckInProject(jobUid, projectId, false, sessionUid, "Updating CF");

        }

private void btnDeleteCF_Click(object sender, EventArgs e)
        {
            Guid taskTextGuid = new Guid("30665299-bc21-4c51-b954-220d407ba47e");
            Guid projectId = new Guid("4c1cef33-4810-4159-b2e1-db15ea19a28b");
            WebSvcProject.ProjectDataSet projectDs = 
                project.ReadProject(projectId, WebSvcProject.DataStoreEnum.WorkingStore);
            
            foreach (WebSvcProject.ProjectDataSet.TaskCustomFieldsRow cfRow in 
                projectDs.TaskCustomFields)
            {
                if (cfRow.MD_PROP_UID == taskTextGuid)
                {
                    cfRow.Delete();
                }
            }
            Guid sessionUid = Guid.NewGuid();

            project.CheckOutProject(projectId, sessionUid, "Updating CF");
            Guid jobUid = Guid.NewGuid();
            project.QueueUpdateProject(jobUid, sessionUid, projectDs, false);
            jobUid = Guid.NewGuid();
            project.QueueCheckInProject(jobUid, projectId, false, sessionUid, "Updating CF");
        }

Technorati Tags: