Second Life of a Hungarian SharePoint Geek

October 22, 2014

How to set the value of a Project Server Enterprise Custom Field via the Project Server Managed Client Object Model

Filed under: Managed Client OM, Project Server — Tags: , — Peter Holpar @ 21:58

Recently I had to set the value of a Project Server Enterprise Custom Field from a .NET client application. The samples I found on the web (like this one) used the PSI , and I found them rather developer-unfriendly. For example, with PSI we should work with DataSet objects, check if the custom field already exists or should be first created, and should set various properties depending of the data type of the custom field.

So I’ve decided to create my own implementation based on the Managed Client Object Model. In my case I had to set a project-related custom field. In the first step I assumed, that the project ID and the internal name of the custom field are known. The internal name of the custom field consists of the prefix Custom_ and the GUID of the field without the separator dashes.

The result is the following code:

  1. var url = @"http://YourProjectServer/pwa";
  2. var projectContext = new ProjectContext(url);
  3.  
  4. var projId = new Guid("98138ffd-d0fa-e311-83c6-005056b45654");
  5. var cfInternalName = "Custom_b278fdf35d16e4119568005056b45654";
  6. object cfValue = "Some value";
  7.  
  8. var proj = projectContext.Projects.GetByGuid(projId);
  9. var draftProj = proj.CheckOut();
  10. draftProj.SetCustomFieldValue(cfInternalName, cfValue);
  11. var cfsX = proj.CustomFields;
  12. draftProj.Publish(true);
  13.  
  14. projectContext.ExecuteQuery();

If the project ID and the internal name of the custom field are not known, it is easy to get them from the names like this:

  1. projectContext.Load(projectContext.Projects, ps => ps.Include(p => p.Id, p => p.Name));
  2. projectContext.Load(projectContext.CustomFields, cfs => cfs.Include(cf => cf.InternalName, cf => cf.Name));
  3. projectContext.ExecuteQuery();
  4.  
  5. var projId = projectContext.Projects.First(p => p.Name == "Your Project Name").Id;
  6. var cfInternalName = projectContext.CustomFields.First(cf => cf.Name == "NameOfTheField").InternalName;

Note, how much this solution is simpler than the PSI-alternative.

2 Comments »

  1. Your code runs without error however when setting a field for the fist time it does seem to set field value. Only if the field has been set previously by MS Project or PDP does new value appear. Any thoughts?

    Comment by Greg Winterhalter — March 11, 2016 @ 01:22

    • Hi Greg! Thanks for your comment. I’ve tested my code again, and in my environment (Project Server 2013, June 2015 CU) it works even if the field was not set before. I’ve tested with a custom field of type “Text”, and the value is displayed in Project Center and is available via client code (like proj.FieldValues[cfInternalName]) as well. What field type do you have? How do you test for the value (I mean, on the UI or via code)?

      Comment by Peter Holpar — March 14, 2016 @ 09:13


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: