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; 
                    web.Fields.Add(taxField); 
                    web.Update(); 

                } 
           } 
If we need to create a multi valued Taxonomy field we need to use:
TaxonomyField taxField = web.Fields.CreateNewField("TaxonomyFieldTypeMulti", "TaxonomyColumn") as TaxonomyField; 
                   

1 comment:

Anonymous said...

thanks, nice to see how to do multiple values, not sure if i can get it to work though, you find that this model is a little more squirly then other models in sharpeoint?

Related Posts with Thumbnails