Quantcast
Channel: Second Life of a Hungarian SharePoint Geek
Viewing all articles
Browse latest Browse all 206

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

$
0
0

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.



Viewing all articles
Browse latest Browse all 206

Trending Articles