Friday, 25 November 2016

remove a term from managed metadata column in Sharepoint




Hi All,

 Today I will share you the code for removing a single term from a Managed Metadata (- multivalue) column in a list.

Name of the List - "Todo List"
Name of the Managed metadata column - "TodoColumn"
Values it is holding at present - "Lesson 1" , "Lesson 2" , "Lesson 3" , "Lesson 4"
Value to be removed - "Lesson 1"

Now we will see the code :


   SPList list = oweb.Lists["Todo List"];
                    SPQuery oQuery = new SPQuery();
                    oQuery.Query = " <Where><Eq><FieldRef Name='TodoColumn' /><Value Type='TaxonomyFieldTypeMulti'>Lesson 1</Value></Eq></Where>";
                    SPListItemCollection collListItems = list.GetItems(oQuery);
                    foreach (SPListItem oListItem in collListItems)
                    {
                        TaxonomyFieldValueCollection tfc = oListItem["TodoColumn"] as TaxonomyFieldValueCollection;
                        tfc.Remove(tfc.Where(i => i.Label.Equals("Lesson 1")).FirstOrDefault());
                        oListItem["TodoColumn"] = tfc;
                        oListItem.Update();
                    }
                    list.Update()


Thats's it :)

Have a great day... Happy coding :))))

No comments:

Post a Comment