Online Training On SharePoint

Monday, 1 March 2010

Creating Taxonomy Fields using SharePoint Object model in SharePoint 2010

To create the taxonmy fields using SharePoint Object Model we can use the following code snipet:
using (SPSite oSite = new SPSite("mylocalsite")) // pass the URL 
                using (SPWeb web = oSite.OpenWeb()) 
                    // Get the session 
                    TaxonomySession session = new TaxonomySession(oSite); 
                    TermStore termStore = session.TermStores["DemoTermStore"]; 
                    Group group = termStore.Groups["DemoTermGroup"]; 
                    TermSet termSet = group.TermSets["DemoTermSet"]; 

                   //To create a single valued field 
                    TaxonomyField taxField = web.Fields.CreateNewField("TaxonomyFieldType", "TaxonomyColumn") as TaxonomyField; 
                    taxField.SspId = termStore.Id; 
                    taxField.TermSetId = termSet.Id; 
                    taxField.AllowMultipleValues = false; 

If we need to create a multi valued Taxonomy field we need to use:
TaxonomyField taxField = web.Fields.CreateNewField("TaxonomyFieldTypeMulti", "TaxonomyColumn") as TaxonomyField; 
Post a Comment
Related Posts with Thumbnails