Managed Metadata, one of my most widely used feature in SharePoint 2010 comes with many exciting things. I tend to use them very often depending on the business case. Recently I came across a scenario where I had to Insert almost 1000 items to the Term Store with their sub sets. In my case it was the list of manufacturers and their products. So I wrote a console application to do the same. In my case, I had a excel sheet of values which I read using my previously created ReadExcel.cs. Here is the code :
For any queries, please write to me.
using (SPSite site = new SPSite("")) { TaxonomySession _TaxonomySession = new TaxonomySession(site); //Get instance of the Term Store TermStore _TermStore = _TaxonomySession.TermStores[""]; //Instance of Term Store Group _Group = _TermStore.Groups[""]; //Instance of Term Group TermSet _TermSet = _Group.TermSets[""]; FileStream filename = File.Open(@"", FileMode.Open); for (int i = 1; i <= 1000; i++) { // ReadExcel.ReadxlswithID() is my function to read the row values in Excel if (ReadExcel.ReadxlswithID(filename, "A" + i.ToString(), "Sheet2") != null) { string manufacturer = ReadExcel.ReadxlswithID(filename, "A" + i.ToString(), "Sheet2"); string product = ReadExcel.ReadxlswithID(filename, "B" + i.ToString(), "Sheet2"); TermCollection terms = _TermSet.GetTerms(manufacturer, true); try { if (terms.Count > 0) { Term tExists = _TermSet.GetTerm(terms[0].Id); //Get the branch of terms within terms TermCollection subTermExists = tExists.GetTerms(50); bool termAlreadyExists = false; for (int j = 0; j < subTermExists.Count; j++) { if (product == subTermExists[j].Name) { termAlreadyExists = true; break; } } if (e == false) { Term subTerm = tExists.CreateTerm(product, 1033); _TermStore.CommitAll(); } } else { Term _term = _TermSet.CreateTerm(manufacturer, 1033); Term _sTerm = _term.CreateTerm(product, 1033); _TermStore.CommitAll(); } } catch (Exception ex) { Console.WriteLine(ex.ToString() + product); } } } }
For any queries, please write to me.
No comments:
Post a Comment