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 of business house.";
        taxonomyField.SspId = termStore.Id;
        taxonomyField.TermSetId = termset.Id;
        taxonomyField.AllowMultipleValues = false;
        taxonomyField.Group = "My Content Types";
        web.Fields.Add(taxonomyField);
        TaxonomyField field = site.RootWeb.Fields["BusinessLocation"] as TaxonomyField;
        field.Title = "Business Location";
        field.Update(true);
    }
}

 

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

posted @ 2014-12-10 22:05  SP 新手  阅读(211)  评论(0编辑  收藏  举报