Arrange the Activities (Tasks List) Hierarchy Using Patterns and Practice C# in SharePoint Office 365

Sathish Nadarajan
SharePoint MVP
Published On :   26 Sep 2017
Visit Count
Today :  2    Total :   1600

Recently in one of the Migration activity, we faced a strange issue. We are migrating the Task List using a Custom Migration tool. Which will pull the tasks from a legacy application (IBM Connect) and push to the SharePoint Office 365 Tasks List.

While doing this, the tool was not able to maintain the Task and Sub Task.

i.e., all the activities were migrated as Tasks. Not with a Parent Child relationship.


But we have a column which has the ID of the Parent Task. With that, we need to write a Small snippet, which should arrange the hierarchy. Thought of sharing to the community.

There is a Field called “ParentID” for all the Tasks item. That is a Lookup column of the same list. Hence, we need to generate a LookupValueCollection and assign that to this field. I am explaining here with a single item. But, we can implement the for loop with the Recursive method in the actual implementation.

 using Microsoft.SharePoint.Client;
 using System;
 namespace Office365.Console
     class Program
         static void Main(string[] args)
         public static void UpdateTaskHierarchy()
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
             //Site URL
             string siteUrl = "https://*********";
             // UserName 
             string userName = "sathish@**********";
             string password = "*******";
             using (var clientContext = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
                     Site site = clientContext.Site;
                     Web web = clientContext.Web;
                     //Get the Tasks List
                     List list = web.Lists.GetByTitle("TasksList");
                     //Get the Child Item - The IDs were Hard coded for the demo purpose
                     ListItem listItem = list.GetItemById(4);
                     // Get the Parent Item - The LookupID is hard coded for the demo purpose
                     var lookupValue = new FieldLookupValue();
                     lookupValue.LookupId = 3;
                     var lookupValueCollection = new FieldLookupValue[1];
                     lookupValueCollection.SetValue(lookupValue, 0);
                     // Set the LookupValueCollection
                     listItem["ParentID"] = lookupValueCollection;
                 catch (Exception ex)
                     System.Console.ForegroundColor = ConsoleColor.Red;
                     System.Console.WriteLine("Exception Occured : " + ex.Message);
                     System.IO.File.AppendAllText("C:\\Temp\\UpdateTaskHierarchy.txt", ex.Message + " - " + siteUrl + Environment.NewLine);
             System.Console.WriteLine("Press Any Key to Exit ....");

The output of the method will be as below.


Though it is very rare scenario, definitely will be useful in the migration projects.

Happy Coding,

Sathish Nadarajan.