首页 > 代码库 > SharePoint: Create managed metadata field programatically

SharePoint: Create managed metadata field programatically

The following piece of code creates a managed metadata column with internal name "BusinessLocation" and display name "Business Location"

using (SPSite site = new SPSite("http://intranet.contoso.com/sites/contracts"))
{
    using (SPWeb web = site.OpenWeb())
    {                 
        TaxonomySession taxonomySession = new TaxonomySession(site);
        TermStore termStore = taxonomySession.TermStores["Managed Metadata Service"];
        Group group = termStore.Groups["Corporate Taxonomy"];
        TermSet termset = group.TermSets["Geography"];
        TaxonomyField taxonomyField = web.Fields.CreateNewField("TaxonomyFieldType", "BusinessLocation") as TaxonomyField;
        taxonomyField.Description = "Loction ofbusiness house.";
        taxonomyField.SspId = termStore.Id;
        taxonomyField.TermSetId = termset.Id;
        taxonomyField.AllowMultipleValues = false;
        taxonomyField.Group = "My ContentTypes";
        web.Fields.Add(taxonomyField);
        TaxonomyField field = site.RootWeb.Fields["BusinessLocation"] as TaxonomyField;
        field.Title = "BusinessLocation";
        field.Update(true);
    }
}

 

来自 <http://www.sharepointnadeem.com/2012/02/create-managed-metadata-field.html> 

SharePoint: Create managed metadata field programatically