1

I am trying to access, in a project detail page, the project context via Javascript to retrieve the data of the custom fields.

But my problem is that, when I try to access all the data it only shows me three fields, and there are at least 20.

My code:

<script type="text/javascript">

var contexto;
var proyecto;   
var fields; 


    function init() {       

        contexto = PS.ProjectContext.get_current();
        proyecto = contexto.get_projects().getByGuid(PDP_projUid);
        fields = proyecto.get_customFields(); 
        contexto.load(fields);
        contexto.executeQueryAsync(Function.createDelegate(this, this.Exito), Function.createDelegate(this, this.Error));

    }

    init(); 

    function Exito() {

        console.log('Exito');

        console.log(fields);
        console.log(fields.get_count()); 

        for(i = 0; i < fields.get_count(); i++) 
        {
            console.log(fields.get_item(i));            
            console.log(fields.get_item(i).get_name());
            console.log(fields.get_item(i).get_description());
        }

    }

    function Error() {
        alert('Error'); 
    }

</script>

All the fields, are controlled by the workflow, even the three that are successfully shown.

Any idea why this happens?

3
  • Do you see these custom fields on the PWA UI (Project Details page)? And via REST (for example yourserver/PWA/_api/ProjectServer/… where the Guid is the Id of the project)? Is the project checked-in?
    – pholpar
    Mar 3, 2015 at 10:16
  • Hi @pholpar, the fields are not shown in the PDP. The PDP only has the embbed code webpart I'm using. Via REST without selecting any field the XML only shows those three fields. The project was checked out but I checked it in and nothing changed.
    – ADPX
    Mar 3, 2015 at 10:30
  • And when I try to access any of the custom fields (those three or the others) via REST as you've shown me, I always get the same error -1, Microsoft.SharePoint.Client.InvalidClientQueryException The expression (custom field name) is not valid
    – ADPX
    Mar 3, 2015 at 10:40

2 Answers 2

2

Fields by default are not loaded in CSOM, you need to specify what field you would like to load. Here is an example on how to do it.

projectContext.Load(projectContext.Projects,
    c =>
        c.Where(p => p.Id == new Guid(id))
            .IncludeWithDefaultProperties(f => f.Description,
                f => f.FinishDate, f => f.PercentComplete, f => f.StartDate, f => f.Id, f => f.Name,
                f => f.CreatedDate, f => f.Owner.UserId, f => f.CustomFields));

projectContext.ExecuteQuery();
1

From your replies I assume you have defined about 20 enterprise custom fields for the project entity, but to the project you are working with only 3 of these fields were applied explicitly. Custom fields not set on an entity (project, task or resource) will be not included in the result as you request the custom fields for the given entity (either via REST via the client OM, or via PSI).

You can verify, if the fields were assigned to the project and saved with a value via the following database query as well (should be executed on the project database):

SELECT F.MD_PROP_Name,
      FV.TEXT_VALUE      
  FROM pub.MSP_PROJ_CUSTOM_FIELD_VALUES FV
  INNER JOIN pub.MSP_CUSTOM_FIELDS F ON FV.MD_PROP_ID = F.MD_PROP_ID
  WHERE PROJ_UID = 'd152c2ba-047a-e411-9568-005056b45654'

In the query I assume the custom field is a text field (see FV.TEXT_VALUE) and the Guid in the WHERE clause is the id of your project.

You can set the custom fields on the project using the following code (as long as you know the ID of the project as well the internal name of your custom field):

var url = @"http://YourProjectServer/pwa"
var projectContext = new ProjectContext(url);

var projId = new Guid("98138ffd-d0fa-e311-83c6-005056b45654");
var cfInternalName = "Custom_b278fdf35d16e4119568005056b45654";
object cfValue = "Some value";

var proj = projectContext.Projects.GetByGuid(projId);
var draftProj = proj.CheckOut();
draftProj.SetCustomFieldValue(cfInternalName, cfValue);
var cfsX = proj.CustomFields;
draftProj.Publish(true);

projectContext.ExecuteQuery();

You can find the complete code (including resolving the project ID and internal name for the field) here.

3
  • Well, not exactly, I've got a PDP in which I try to access the fields and in that one I don't show any custom field. But, in the same stage, I do have another PDP in which I do show an let the user modify some of those custom fields that I cannot access. So I guess they are set on an entity, aren't the?
    – ADPX
    Mar 3, 2015 at 13:42
  • That was one of my questions earlier, if you (or a user) can see the values of the "missing" custom fields on a PDP. Just because you let the users to set the fields does not mean that the field has a value. If you can see the custom fields with values on a PDP (or via the database query, see my updated answer above), then the fields should be returned by REST / client OM as well.
    – pholpar
    Mar 3, 2015 at 14:13
  • Yes, I missunderstood you, sorry. Anyway I ran the query and it only returns those three custom fields. Do you know how should I set the rest of the custom fields to the project?
    – ADPX
    Mar 4, 2015 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.