SharePoint 2010: Creating Term store, term groups, terms programatically using CSV
With SharePoint 2010 Metadata Managment Services, come few frequently used terminologies such as Termstore, Termgroup, Termset and Terms which normally you create them all in this manner.
Now without going into the detail of this jargon (because a lot has been written already) and touching the implementational aspect of it, the easiest way of creating a term store consitituing of respective terms, is by importing a CSV file. Wictor Wilen gives a very interesting account of how to create a CSV that can easily be imported to the Metadata management service.
Not only that but he also hosts a Macros Enabled Excel Sheet which generates the right CSV upon entering the right data.
Technically in SharePoint, whatever is doable using out of the box SharePoint features, can be achieved programatically using custom solution.
Following this principlem what I'm trying to achieve is to do the same thing programatically that we do out of the box i,e Creating a TermStore and a term group and term set and then creating or uploading the terms manually.
Now lets have a look at the code.
Hope this helps.
References:
Now without going into the detail of this jargon (because a lot has been written already) and touching the implementational aspect of it, the easiest way of creating a term store consitituing of respective terms, is by importing a CSV file. Wictor Wilen gives a very interesting account of how to create a CSV that can easily be imported to the Metadata management service.
Not only that but he also hosts a Macros Enabled Excel Sheet which generates the right CSV upon entering the right data.
Technically in SharePoint, whatever is doable using out of the box SharePoint features, can be achieved programatically using custom solution.
Following this principlem what I'm trying to achieve is to do the same thing programatically that we do out of the box i,e Creating a TermStore and a term group and term set and then creating or uploading the terms manually.
Now lets have a look at the code.
- It was actually used inside an event reciever of a SP feature. Upon Feature activation this code was called.
- In short, this code creates a termgroup first i.e. Customers in my case. and then reads a CSV generated from Wilen's excel sheet and creates all the terms.
- Via the code, which is commented at the moment, you can also manually create the terms if you dont want to do that via a CSV.
- Although this code works but there are some anomilies that I could not sort out. Such as in the commented code, you could see I am trying to create a TermSet und the term group. Now my desire was to create the terms inside this Termset but I could not achieve that. Rather all the terms are created under the parent group.
- May be I am missing the concept of local terms and the global terms
//Code for creating Metadata Termstore and private void AddFieldToTaxonomy(SPFeatureReceiverProperties properties) { TaxonomySession oTaxSession; TermStore oTermStore; Group oTermGroup; TermSet oTermSet; Term oTerm; SPSite oSite = properties.Feature.Parent as SPSite; oTaxSession = new TaxonomySession(oSite); oTermStore = oTaxSession.DefaultSiteCollectionTermStore; try { oTermGroup = oTermStore.Groups["Customers"]; } catch (ArgumentOutOfRangeException ex) { oTermGroup = oTermStore.CreateGroup("Customers"); oTermGroup.Description = "Our Customers"; } //try //{ // oTermSet = oTermGroup.TermSets["Enterprise Customers"]; //} //catch (ArgumentOutOfRangeException ex) //{ // //oTermGroup.TermSets. // oTermSet = oTermGroup.CreateTermSet("Enterprise Customers"); // oTermSet.Description = "Kunde"; //} //try //{ // oTerm = oTermSet.Terms["MyTerms"]; //} //catch (ArgumentOutOfRangeException ex) //{ // oTerm = oTermSet.CreateTerm("MyTerms", oTermStore.DefaultLanguage); // oTerm.SetDescription("MyTerm Description", oTermStore.DefaultLanguage); // oTerm.CreateLabel("MyTerm Label", oTermStore.DefaultLanguage, false); //} oTermStore.CommitAll(); try { string filePath = @"C:\CustomerTermStore.txt"; StreamReader reader = File.OpenText(filePath); ImportManager manager = oTermStore.GetImportManager(); bool allTermsAdded = false; string errorMessage = string.Empty; manager.ImportTermSet(oTermGroup, reader, out allTermsAdded, out errorMessage); if (errorMessage.Length > 0) throw new Exception(errorMessage); oTermStore.CommitAll(); oSite.Close(); } catch (Exception) { throw; } oSite.Close(); }
Hope this helps.
References:
- http://www.wictorwilen.se/Post/Create-SharePoint-2010-Managed-Metadata-with-Excel-2010.aspx
- http://bramnuyts.be/2011/04/04/add-a-term-to-the-managed-metadatas-term-store-programmatically/
- http://sharepointegg.blogspot.de/2010/10/creating-term-set-in-sharepoint-2010.html
- http://sharepointweblog.blogspot.de/2011/11/sharepoint-2010-programmatically-add.html
Comments
Post a Comment