none
Use CSOM to update project's custom fields RRS feed

  • Question

  • Hi, I have a problem when updating a project using CSOM (on Project Server 2013). I cannot use PSI because I need to move to Project Online in the near future. My code looks like this:

                DraftProject draftProject = onlineProject.CheckOut();
                draftProject.SetCustomFieldValue("Project Number", "111");
                draftProject.Name = "123";
                draftProject.Description = "aab";
                //draftProject["ProjectNumber"] = "111";
                var job = draftProject.Publish(true);
    

    Note I'm updating the name, the description, and a custom field called Project Number. Now the name is successfully updated. But neither the description nor the custom field is updated, and the service doesn't return any error. Why does the update partially succeeded?

    Can someone help? Thanks.


    Sining Oh Blue Star

    Tuesday, July 16, 2013 6:59 AM

Answers

  • Hi 

    For Custom field you need to pass fieldvalue as "Custom_<customfieldUID>".

    For Example , for updating following project Number , 

    Replace draftProject.SetCustomFieldValue("Custom_<customfieldUID>", "111");

    Pls mark it as answer if u u got it correct

    • Proposed as answer by harry devu Thursday, July 18, 2013 10:42 AM
    • Marked as answer by SALX Friday, July 19, 2013 3:54 AM
    Wednesday, July 17, 2013 7:38 AM
  • Thanks. That returns all UIDs, but not friendly names, so I cannot map friendly names to UIDs. Anyway, I'm now using OData to return all custom field information and then I can map friendly names to UIDs. That's working fine now.

    Sining Oh Blue Star

    • Marked as answer by SALX Friday, July 19, 2013 3:54 AM
    Friday, July 19, 2013 3:54 AM

All replies

  • Hi 

    For Custom field you need to pass fieldvalue as "Custom_<customfieldUID>".

    For Example , for updating following project Number , 

    Replace draftProject.SetCustomFieldValue("Custom_<customfieldUID>", "111");

    Pls mark it as answer if u u got it correct

    • Proposed as answer by harry devu Thursday, July 18, 2013 10:42 AM
    • Marked as answer by SALX Friday, July 19, 2013 3:54 AM
    Wednesday, July 17, 2013 7:38 AM
  • Thanks for the information. How can I get the UID programmatically? I tried to hard code it (got it from OData feed), but it still doesn't work. The custom field's value is not set.

                project.SetCustomFieldValue("Custom_x005f_8e967425f2e9e2119402005056915377""1223");
    


    Sining Oh Blue Star

    Wednesday, July 17, 2013 9:24 AM
  • OK. Tried Custom_8e967425f2e9e2119402005056915377, and now it works. But still not sure how to obtain the UID programmatically...

    Sining Oh Blue Star

    Wednesday, July 17, 2013 12:04 PM
  • Looks like _api/ProjectServer/CustomFields/ returns all custom fields. So I think one solution is to use this OData service to obtain a list of custom fields (since the ProjectServer service does not offer a metadata, we have to build a custom class instead of adding a service reference), and then create a dictionary to map each field's name and UID. When calling SetCustomFieldValue, query the dictionary to get the UID. Is this the best way to set custom field values? I think it's a bit too overhead.


    Sining Oh Blue Star

    Wednesday, July 17, 2013 12:27 PM
  • U can get the fieldkey  of customfield by following code

     projDict = projCheckedOut.FieldValues;

      foreach (KeyValuePair<string, object> kvp in projDict)
                        {
                            try
                            {
                                Console.WriteLine("\tKey = {0}, Value = {1}", kvp.Key, kvp.Value.ToString());
                            }
                            catch { }
                        }


    Thursday, July 18, 2013 10:41 AM
  • Thanks. That returns all UIDs, but not friendly names, so I cannot map friendly names to UIDs. Anyway, I'm now using OData to return all custom field information and then I can map friendly names to UIDs. That's working fine now.

    Sining Oh Blue Star

    • Marked as answer by SALX Friday, July 19, 2013 3:54 AM
    Friday, July 19, 2013 3:54 AM
  • Hi

    I am trying to update task level custom field, and am fully tired.

    SALX,  do u know how to do this.

    actually i am tired of doing this.

    Thank u..

    Tuesday, July 23, 2013 5:08 AM
  •                     DraftProject projCheckedOut = proj2Edit.CheckOut();
                        projContext.Load(projCheckedOut.Tasks);
                        projContext.ExecuteQuery();
                        DraftTaskCollection tskcoll = projCheckedOut.Tasks;

                        foreach (DraftTask tsk in tskcoll)
                        {
                            if ((tsk.Name != null) && (tsk.Name == "Your task name"))
                            {
                                projContext.Load(tsk.CustomFields);
                                projContext.ExecuteQuery();
                                foreach (CustomField cus in tsk.CustomFields)
                                {
                                    if (cus.Name == "Your custom Field")
                                    {
                                        string intname = cus.InternalName.ToString();
                                        string cusvalue = tsk[intname].ToString();
                                        tsk[intname] = "Your new value";                                  
                                        msg = "customfield  - " + "original " + cusvalue + ": new " + tsk[intname].ToString();
                                    }
                                }
                            }
                        }
                        projCheckedOut.Publish(true);
                        QueueJob qJob = projContext.Projects.Update();
                        JobState jobState = projContext.WaitForQueue(qJob, 200);

    Wednesday, July 24, 2013 4:43 PM
  • Thnaks for the reply.

    its working..

    Do u have javascript which updates the task level custom field.?

    Thanks

    Harry

    Thursday, July 25, 2013 7:35 AM
  • no, I have been almost all C# SharePoint programming since I started. Up until this version, I couldn't accomplish the things I needed to do in JS so very little experience. Much of the reporting and auditing and functionality I needed has to do with evaluating 500 sites in a collection all from the same template (in total). So mostly server side and now CSOM in solutions.

    Maybe a year from now I'll now more about APP and JS. I have seen samples in JS so it shouldn't be too hard to figure out, as most will be a pure translation. I will be interested to see which objects are not available or need a wholesale different approach.

    I'm glad you got the sample code to run. I haven't programmed in about 3 months so I was a little unsure of the samples value when I posted, but the basic pattern seemed correct even if a line or two was in error. I've been stuck on administration duties for Project Server trying to get a stable rollout.

    Thursday, July 25, 2013 12:26 PM
  • Hi,

    you can use the following code (source: http://pholpar.wordpress.com/2014/10/22/how-to-set-the-value-of-a-project-server-enterprise-custom-field-via-the-project-server-managed-client-object-model/) to update the field, even if you know only its name, and no ID:

    var url = @"http://YourProjectServer/pwa";
    var projectContext = new ProjectContext(url);
    
    // if you know only the name of the project and the field use this code block...
    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 == "Your Project Name").Id;
    var cfInternalName = projectContext.CustomFields.First(cf => cf.Name == "NameOfTheField").InternalName;
    
    // ... or use the known IDs if you have them
    //var projId = new Guid("98138ffd-d0fa-e311-83c6-005056b45654");
    //var cfInternalName = "Custom_b278fdf35d16e4119568005056b45654";
    
    object cfValue = "Some value"; // the value can be 'null' as well
    
    var proj = projectContext.Projects.GetByGuid(projId);
    var draftProj = proj.CheckOut();
    draftProj.SetCustomFieldValue(cfInternalName, cfValue);
    var cfsX = proj.CustomFields;
    draftProj.Publish(true);

    Thursday, October 23, 2014 6:17 AM
  • Below is link to update custom field using csom.It's 100% working

    http://projectservercode.com/update-custom-field-value-using-csom/

    Saturday, May 28, 2016 7:12 PM