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

Sathish Nadarajan
 
Solution Architect
September 26, 2017
 
Rate this article
 
Views
3671

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.

clip_image002

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)
         {
             UpdateTaskHierarchy();
         }
 
         public static void UpdateTaskHierarchy()
         {
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
             
             //Site URL
             string siteUrl = "https://*********.sharepoint.com/sites/communitysite";
             // UserName 
             string userName = "sathish@**********.onmicrosoft.com";
             //Password
             string password = "*******";
 
             
             using (var clientContext = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
             {
                 try
                 {
                     Site site = clientContext.Site;
                     Web web = clientContext.Web;
                     
                     clientContext.Load(web);
                     clientContext.Load(web.Lists);
                     clientContext.ExecuteQuery();
                     //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);
                     clientContext.Load(listItem);
                     clientContext.ExecuteQuery();
 
                     // 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;
 
                     listItem.Update();
                     clientContext.ExecuteQuery();
 
                 }
                 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("Completed....");
             System.Console.WriteLine("Press Any Key to Exit ....");
             System.Console.ReadLine();
         }
          
 
          
     }
 }
 

The output of the method will be as below.

clip_image004

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

Happy Coding,

Sathish Nadarajan.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment