1

I am working on a console application for project server. Atm I can read the name and work percent of each task assined to the project. Each task has a custom field with an unique id. It looks like this.

How do I get the value of the unique id? e.g. the 84

Here is my code to list the task name and work percent:

var projColl = projContext.LoadQuery(projContext.Projects
                .Where(p => p.Name == projectName)
                .Include(
                    p => p.Name,
                    p => p.Tasks,
                    p => p.Tasks.Include(
                        t => t.Name,
                        t => t.PercentComplete,
                        t => t.CustomFields
                      )
                    )
                 );



        projContext.ExecuteQuery();
        PublishedProject theProj = projColl.First();
        PublishedTaskCollection taskColl = theProj.Tasks;
        PublishedTask theTask = taskColl.First();
        CustomFieldCollection LCFColl = theTask.CustomFields;
        Dictionary<string, object> taskCF_Dict = theTask.FieldValues;

        int k = 1;    //Task counter.
        foreach (PublishedTask t in taskColl)
        {


            Console.WriteLine("\t{0}.  {1, -15}       {2,-30}{3}", k++, t.Name, t.PercentComplete);

        }

I tried to useConsole.WriteLine("\t{0}. {1, -15} {2,-30}{3}", k++, t.Name, t.PercentComplete,t.CustomFields);

but I only get

Microsoft.ProjectServer.Client.CustomFieldCollection

I also know the InternalName of the customfield if that helps

EDIT: I added this exsample but I only get the values of the first row. Any idea how to loop for every row?

T.Steph
  • 17
  • 7
  • You are only getting values from the first row because `LCFColl` is defined as the custom fields of `theTask` not the variable `t` that you use inside your loop. Start with moving the declaration of `LCFColl` inside your task loop: `CustomFieldCollection LCFColl = t.CustomFields;`. – Rachel Hettinger Mar 12 '19 at 21:17
  • Thanks that helped. Now I just have to somehow get the one particular custom field – T.Steph Mar 13 '19 at 06:30
  • Got it to work as I want. Thank you! – T.Steph Mar 13 '19 at 08:54
  • Great. I moved the comment to an answer so you can accept/mark it as the best answer. This way, it can be useful to other people who search for answered questions. – Rachel Hettinger Mar 14 '19 at 16:25

1 Answers1

0

You are only getting values from the first row because LCFColl is defined as the custom fields of the object variable theTask not the variable t that you use inside your loop. Move the declaration of LCFColl inside your task loop:

   foreach (PublishedTask t in taskColl)
   {

      CustomFieldCollection LCFColl = t.CustomFields;
      foreach (CustomField cf in LCFColl)
      {
          // do something with the custom fields
      }

      Console.WriteLine("\t{0}.  {1, -15}       {2,-30}{3}", k++, t.Name, t.PercentComplete);

   }
Rachel Hettinger
  • 7,927
  • 2
  • 21
  • 31