Monday, July 23, 2012

ECM(Enterprise Content Management) Create Taxonomy Programatically in SharePoint 2010




//Application Management -> Service Applications -> Manage service applications -> Managed Metadata Service
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;
using Microsoft.SharePoint.WebControls;
namespace ECM_CreateTaxonomy.Layouts.ECM_CreateTaxonomy
{
    public partial class CreateTaxonomy : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void createTaxonomyButton_Click(object sender, EventArgs e)
        {
            SPSite currentSite = SPContext.Current.Site;
            TaxonomySession session = new TaxonomySession(currentSite);
            TermStore termstore = session.TermStores["Managed Metadata Service"];
            Group plantsGroup = termstore.CreateGroup("Plants");
            TermSet flowersTermSet = plantsGroup.CreateTermSet("Flowers");
            Term tulipsTerm = flowersTermSet.CreateTerm("Tulips", 1033);
            Term orchidsTerm = flowersTermSet.CreateTerm("Orchids", 1033);
            Term daffodilsTerm = flowersTermSet.CreateTerm("Daffodils", 1033);
            Term vanillaTerm = orchidsTerm.CreateTerm("Vanilla", 1033);
            vanillaTerm.SetDescription("A common orchid whose pods are used in desserts", 1033);
            vanillaTerm.CreateLabel("Vanilla planifolia", 1033, false);
            vanillaTerm.CreateLabel("Flat-leaved vanilla", 1033, false);
            try
            {
                termstore.CommitAll();
                resultsLabel.Text = "Taxonomy created successfully";
            }
            catch (Exception ex)
            {
                resultsLabel.Text = "There was an error: " + ex.Message;
            }
        }
    }
}
-------------------------------------------------------------------------------------------------------------------------

3 comments:

Featured Post

Create SharePoint Folder Structure in Destination (Only If Not Exists)

Why This Script Is Safe You can run it multiple times It will not create duplicate folders It will only create missing folders S...

Popular posts