SharePoint Online And On-Premise Event Receiver Job For Managed Metadata Service

Recently, I came across a requirement where I needed the deleted event receiver on managed metadata service. I initially thought there may definitely be some direct solution for this kind of requirement but unfortunately, no synchronous event receiver is available which triggers immediately after the term has got deleted from the termset.
 
After some research, I came across a method ‘GetChanges’ which is available at TermStore and TermSet both.
 
This solution worked well and I created a PowerShell script which checks TermStore for any deleted terms after a specific interval that can be configured in the task scheduler.
 
What I wanted to achieve is to set the term ‘Uncategorized’ to the list item whose original term had got deleted.
 
You can read the method documentation at TermSet.GetChanges Method
 
Solution for SharePoint 2013 
  1. $dt = Get-Date;  
  2. $dt = $dt.AddDays(-1);  
  3. $url = $webAppURL;  
  4. $tax = Get-SPTaxonomySession -Site $url;  
  5. $termStore = $tax.TermStores[0];  
  6. $deletedTerms = $termStore.GetChanges($dt, [Microsoft.SharePoint.Taxonomy.ChangedItemType]::Term,[Microsoft.SharePoint.Taxonomy.ChangedOperationType]::Delete);  
In the above example, I was looking for deleted terms in the previous number of days (past 1 day in above example).
 
There are four overloads available for the GetChanges method. The one I used has the following parameters,
  • startTime of type DateTime (A UTC time indicating the earliest change to be included in the result collection)
  • itemType of type ChangedItemType (Indicates the type of ChangedItem objects to return) Following are the possible values of Enum.

    SharePoint Online And On-Premise Event Receiver Job For Managed Metadata Service

  • operationType of type ChangedOperationType (Indicates the types of operations to return)
Following are the possible values of Enum.
 
SharePoint Online And On-Premise Event Receiver Job For Managed Metadata Service
 
Method GetChanges returns us a collection of ChangedItem objects that represent changes to this TermStore since a specified time, restricted by item type and operation type.
 
End to end approach
  1. Find which term got deleted using GetChanges method we will use the term IDs we get in next step.
  2. Fire an SPQuery to get the items associated items from taxonomy hidden list.
  3. The items found in step 2 can be used to find actual list item using the relation between taxonomy hidden list and actual list.
  4. We can update the ‘Uncategorized’ term in the respective list item.
Office 365/ SharePoint online
 
This method will work the same way for Sharepoint online as well.
  1. static void Main(string[] args)  
  2.     {  
  3.             AuthenticationManager authenticationManager = new AuthenticationManager();  
  4.             ClientContext clientContext = authenticationManager.GetAzureADCredentialsContext("<My tenant URL>""<email>""<pwd>");  
  5.    
  6.             TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);  
  7.             TermStore termStore = taxonomySession.GetDefaultSiteCollectionTermStore();  
  8.             ChangeInformation changeInfoObj = new ChangeInformation(clientContext);  
  9.             changeInfoObj.ItemType = ChangedItemType.Term;  
  10.             changeInfoObj.StartTime = DateTime.Now.AddDays(-20);  
  11.             changeInfoObj.OperationType = ChangedOperationType.Add;  
  12.             ChangedItemCollection changedTerms = termStore.GetChanges(changeInfoObj);  
  13.             clientContext.Load(changedTerms);  
  14.             clientContext.ExecuteQuery();  
  15.    
  16.             if(changedTerms != null)  
  17.             {  
  18.                 foreach(ChangedItem oneChangedTerm in changedTerms)  
  19.                 {  
  20.                     Console.WriteLine(oneChangedTerm.Id);  
  21.                 }  
  22.             }  
  23.             Console.ReadLine();  
  24.      }  
You can run the above code in a task scheduler EXE, PowerShell script, or an Azure Job. Also, you can decide the frequency according to your requirement; in my case, I was running this once daily.
 
Hope this will help. Happy coding.