none
CSOM 2016 to update Project Enterprise Custom Fields

    Pertanyaan

  • Dear all,

    I'm trying to set a simple text value to a Project's (PWA) Enterprise Custom Field in CSOM 2016 but fail miserably with each decision I choose to do so.

    The main problem is the 2016 CSOM documentation and the missing SetCustomField method which existed in project client 2013 but is unavailable in 2016 API without any notice in MSDN docs. To keep it brief:

    how to update a custom field with CSOM 2016 without SetCustomField method?

    To explain more: I tried to apply a text value to newly created Draft project's FieldValues Collection, but the value wasn't saved to project's custom field (PWA project details were empty) even though debugger showed it in the Collection after insert/update code, which does not return any error and should be successful. Here's the code snippet bellow-

    //create project
    
                                ProjectCreationInformation newProj = new ProjectCreationInformation();
    
                                newProj.Id = Guid.NewGuid();
                                newProj.Name = "some name"
                                newProj.Description = "";
                                newProj.Start = DateTime.Today.Date;
    
                               
                                projContext.Projects.Add(newProj);
                                QueueJob qJob = projContext.Projects.Update();
    
                                JobState jobState = projContext.WaitForQueue(qJob, AppConfigHelper.queueTimeout);
    
                                if (jobState == JobState.Success)
                                {
                                    //projectCreated = true;
                                    Console.WriteLine("Project created!");
    
                                    ////Update custom field
                                    var proj = projContext.Projects.GetByGuid(newProj.Id).IncludeCustomFields;
                                    //No method SetCustomFieldValue in version 16?
    
                                    //Dictionary<String, Microsoft.ProjectServer.Client.CustomField> pwaECF = new Dictionary<string, Microsoft.ProjectServer.Client.CustomField>();
                                    var allECFields = projContext.LoadQuery(projContext.CustomFields.Include(
                                            qp => qp.InternalName,
                                            qp => qp.Name,
                                            qp => qp.FieldType,
                                            qp => qp.LookupTable,
                                            qp => qp.EntityType.Name
                                        )
                                        .OrderBy(qp => qp.EntityType.Name));
    
                                    projContext.ExecuteQuery();
    
                                    foreach (Microsoft.ProjectServer.Client.CustomField ECF in allECFields)
                                    {
                                        if (ECF.Name == "Some custom field name")
                                        {
                                            DraftProject prjDraft = proj.CheckOut().IncludeCustomFields;
                                            projContext.ExecuteQuery();
    
                                            prjDraft.IncludeCustomFields.FieldValues.Add(ECF.InternalName, "123");
                                            prjDraft.IncludeCustomFields.FieldValues[ECF.InternalName] = "123";
    
    
                                            prjDraft.Update();
                                            //projContext.Load(prjDraft);
                                            prjDraft.Publish(true);
                                            //projContext.ExecuteQuery();
                                            //projContext.Projects.Update();
                                            //projContext.ExecuteQuery();
                                            //prjDraft.Publish(true);
                                            //JobState jobState2 = projContext.WaitForQueue(projContext.Projects.Update(), 10);
    
                                            //prjDraft.IncludeCustomFields.Update();
                                            //var job2 = prjDraft.Publish(true);
                                            //prjDraft.CheckIn(true);
                                            //projContext.ExecuteQuery();
                                        }
                                    }
                                }
                                else
                                {
                                    Console.WriteLine("\nThere is a problem in the queue. Timeout is {0} seconds.", AppConfigHelper.queueTimeout);
                                    Console.WriteLine("\tQueue JobState: {0}", jobState.ToString());
                                    Console.ResetColor();
                                }
                            }
                            //test one by one
                            break;

     








    • Diedit oleh lukapes Rabu, 18 Oktober 2017 22.52
    Rabu, 18 Oktober 2017 22.27

Jawaban

Semua Balasan

  • Kamis, 19 Oktober 2017 08.27
    Moderator
  • we have built different code helpers for project fields, check this out

     public bool UpdateProjectField(string veldNaam, string nieuweWaarde, bool isCustomfield)
            {
                if (isCustomfield)
                {
                    CustomField retVal = projContext.CustomFields.FirstOrDefault(p => p.Name == veldNaam);
    
                    if (retVal == null)
                    {
                        return false;
                    }
                    else
                    {
                        veldNaam = retVal.InternalName;
                    }
                }
    
                draftProj[veldNaam] = nieuweWaarde;
                return true;
            }
    
            public bool UpdateProjectField(string veldNaam, int nieuweWaarde, bool isCustomfield)
            {
                if (isCustomfield)
                {
                    CustomField retVal = projContext.CustomFields.FirstOrDefault(p => p.Name == veldNaam);
    
                    if (retVal == null)
                    {
                        return false;
                    }
                    else
                    {
                        veldNaam = retVal.InternalName;
                    }
                }
    
                draftProj[veldNaam] = nieuweWaarde;
                return true;
            }
    
            public bool UpdateProjectField(string veldNaam, DateTime nieuweWaarde, bool isCustomfield)
            {
                if (isCustomfield)
                {
                    CustomField retVal = projContext.CustomFields.FirstOrDefault(p => p.Name == veldNaam);
    
                    if (retVal == null)
                    {
                        return false;
                    }
                    else
                    {
                        veldNaam = retVal.InternalName;
                    }
                }
    
                draftProj[veldNaam] = nieuweWaarde;
                return true;
            }
    
            public bool UpdateProjectField(string veldNaam, string nieuweWaarde, bool isCustomfield, string lookuptablenaam)
            {
                LookupTable lookUpTable = projContext.LookupTables.FirstOrDefault(t => t.Name == lookuptablenaam);
    
                if (lookUpTable == null)
                {
                    return false;
                    //loggen, ook voor update service
                }
    
                LookupEntry entry = lookUpTable.Entries.FirstOrDefault(e => e.FullValue == nieuweWaarde);
    
                if (isCustomfield)
                {
                    CustomField retVal = projContext.CustomFields.FirstOrDefault(p => p.Name == veldNaam);
    
                    if (retVal == null) return false;
                    else veldNaam = retVal.InternalName;
                }
                if (entry != null)
                {
                    draftProj[veldNaam] = new[] { entry.InternalName }; ;
                    draftProj.Update();
                }
    
                return true;
            }

    and one more for the task custom fields

    public void CustomfieldVullen(string taakNaam, string customfield, string inhoud)
            {
                DraftTask task = draftProj.Tasks.FirstOrDefault(t => t.Name == taakNaam);
                if (task != null)
                {
                    CustomField cs = projContext.CustomFields.FirstOrDefault(c => c.Name == customfield);
                    //to do loggen in geval van fout
                    if (cs == null) return;
                    task[cs.InternalName] = inhoud;
                }
            }


    From Project Techcenter

    Rabu, 11 Juli 2018 12.05