Recently I came across a requirement where I need the deleted event receiver on managed metadata service. I initially thought there may be definitely some direct solution exist for this kind of requirement but unfortunately no synchronous event receiver available which trigger immediately after term got deleted from termset.
After some research I came across a method ‘GetChanges’ which is available at TermStore and TermSet both.
This solution worked well and I have created a PowerShell script which checks TermStore for any deleted terms after specific interval which is configured in task schedular.
What a wanted to achieve is to set the term ‘Uncategorized’ to the list item whose original term got deleted.
Method documentation at TermSet.GetChanges Method
Solution for SharePoint 2013:
$dt = Get-Date;
$dt = $dt.AddDays(-1);
$url = $webAppURL;
$tax = Get-SPTaxonomySession -Site $url;
$termStore = $tax.TermStores;
$deletedTerms = $termStore.GetChanges($dt, [Microsoft.SharePoint.Taxonomy.ChangedItemType]::Term,[Microsoft.SharePoint.Taxonomy.ChangedOperationType]::Delete);
In above example I was looking for deleted term in previous number of days (past 1 day in above example).
There are four overloads available for GetChanges method, the one I used have 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.
operationType of type ChangedOperationType (Indicates the types of operations to return)
Following are the possible values of Enum.
Method GetChanges returns us 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 –
- Find which term got deleted using GetChanges method we will use the term IDs we get in next step.
- Fire a SPQuery to get the items associated items from taxonomy hidden list.
- The items found in step 2 can be used to find actual list item using relation between taxonomy hidden list and actual list.
- We can update ‘Uncategorized’ term in the respective list item.
Office 365/ SharePoint online :
This method will work same way for Sharepoint online as well –
static void Main(string args)
AuthenticationManager authenticationManager = new AuthenticationManager();
ClientContext clientContext = authenticationManager.GetAzureADCredentialsContext("My tenant URL", "email", "pwd");
TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);
TermStore termStore = taxonomySession.GetDefaultSiteCollectionTermStore();
ChangeInformation changeInfoObj = new ChangeInformation(clientContext);
changeInfoObj.ItemType = ChangedItemType.Term;
changeInfoObj.StartTime = DateTime.Now.AddDays(-20);
changeInfoObj.OperationType = ChangedOperationType.Add;
ChangedItemCollection changedTerms = termStore.GetChanges(changeInfoObj);
if(changedTerms != null)
foreach(ChangedItem oneChangedTerm in changedTerms)
You can run above code in a task scheduled EXE, PowerShell script or a Azure Job also work. Also you can decide the frequency according to your requirement, in my case I was running this daily once.
Hope this will help. Happy coding.