About two years ago I posted a code about how to set the value of a Project Server Enterprise Field via the managed client OM. Again and again I get the question how to get the value, once it is set already.
In the first case I assume, you already know the ID of your project and the internal name of the field you would like to query. In this case, you need only send a single request to the server, as shown in this code:
- var url = @"http://YourProjectServer/pwa";
- var projectContext = new ProjectContext(url);
- var projId = new Guid("98138ffd-d0fa-e311-83c6-005056b45654");
- var cfInternalName = "Custom_b278fdf35d16e4119568005056b45654";
- var proj = projectContext.Projects.GetByGuid(projId);
- projectContext.Load(proj, p => p[cfInternalName], p => p.Name);
- projectContext.ExecuteQuery();
- Console.WriteLine(proj.Name, proj.FieldValues[cfInternalName]);
If either the ID of your project or the internal name of the field is unknown, you need an extra round-trip before the query shown in the previous code to determine their value. In the code below I assume you know none of these values:
- var url = @"http://YourProjectServer/pwa";
- var projectContext = new ProjectContext(url);
- var projName = "Your Project Name";
- var fieldName = "NameOfTheField";
- projectContext.Load(projectContext.Projects, ps => ps.Include(p => p.Id, p => p.Name));
- projectContext.Load(projectContext.CustomFields, cfs => cfs.Include(cf => cf.InternalName, cf => cf.Name));
- projectContext.ExecuteQuery();
- var projId = projectContext.Projects.First(p => p.Name == projName).Id;
- var cfInternalName = projectContext.CustomFields.First(cf => cf.Name == fieldName).InternalName;
- var proj = projectContext.Projects.GetByGuid(projId);
- projectContext.Load(proj, p => p[cfInternalName], p => p.Name);
- projectContext.ExecuteQuery();
- Console.WriteLine(proj.Name, proj.FieldValues[cfInternalName]);
I hope it helps to read the custom field values, for example the values set by the code in the former post.
