none
Updating Tasks via PSI in C# (2010) RRS feed

  • Question

  • Does anyone have a simple example interating through tasks, interating through the custom fields in that task and updating another field custom field in the current task

    pseudo code would be

    foreach (var task in tasks)
    {
    
    var cf1 = from s in task.customFields where s.name == field1 select s;
    var cf2 = from s in task.customFields where s.name == field2 select s;
    
      if (cf1.value == "foo")
      {
        cf2.value = "bar";
      }
    
    }





    Tuesday, October 20, 2015 2:41 PM

All replies

  • Please refer to 

    https://msdn.microsoft.com/en-us/library/office/ms487478(v=office.12).aspx#pj12_PDSPSIExamples_Task

    you can explore the functions of update task as well in the same class. 

    Hope this helps.

    Faizan.


    Regards, Syed Faizan ur Rehman, CBPM®,PRINCE2®, MCTS

    Wednesday, October 21, 2015 6:00 AM
  • Thanks, I've referred to the msdn articles. I admit that I am new to this interface but the task/custom field relationship does not seem as intuitive as it could be. Here is the skeleton of the code that I've written that is somewhat working. If this makes sense to someone and there is a much easier was to loop through tasks and update fields (as a generic service), let me know. The code below would obviously be decomposed and refactored to handle all different data types that custom fields contain. Thanks
    CheckOutProject(projectId, sessionId, "Updating custom fields");
                var dataSet = _client.ProjectClient.ReadProject(project.PROJ_UID, DataStoreEnum.WorkingStore);
                var tasks = dataSet.TaskCustomFields.GroupBy(t => t.TASK_UID);
    
                foreach (var task in tasks)
                {
                    var keyField = task.FirstOrDefault(c => c.MD_PROP_UID == keyModel.CustomFieldUid);
                    if (keyField != null)
                    {
                        if (keyField.TEXT_VALUE.Equals(keyModel.TextValue, StringComparison.OrdinalIgnoreCase))
                        {
                            var fieldToUpdate = task.FirstOrDefault(c => c.MD_PROP_UID == modelToUpdate.CustomFieldUid);
                            if (fieldToUpdate != null)
                            {
                                Log.Debug(string.Format("Updating {0} to {1}", fieldToUpdate.TEXT_VALUE, modelToUpdate.TextValue));
                                fieldToUpdate.TEXT_VALUE = modelToUpdate.TextValue; 
                            }
                        }
                    }
                }

    Wednesday, October 21, 2015 2:27 PM