A Brief Introduction to Enterprise Metadata Management for Microsoft SharePoint Server 2010 Developers

Summary:  Enterprise Metadata Management (EMM) in Microsoft SharePoint Server 2010 enables users to create and manage metadata and taxonomies across the enterprise. This article introduces some basic developer tasks that you can use as basic building blocks for more powerful and complex metadata solutions.

Applies to: Business Connectivity Services | Office 2010 | Open XML | SharePoint Designer 2010 | SharePoint Foundation 2010 | SharePoint Online | SharePoint Server 2010 | Visual Studio

Provided by:  Ken Milne, Microsoft Corporation | Paula Wing, Microsoft Corporation

Contents

  • Introduction to Enterprise Metadata Management

  • Who Is This Article For?

  • The Taxonomy APIs

  • TaxonomySession Class

  • Groups, Term Sets, and Terms

  • CommitAll Method

  • SetDescription Method

  • CreateLabel Method

  • Code Sample: Create and Commit a Taxonomy, Add Labels, and Delete a Term

  • Conclusion

  • Additional Resources

Introduction to Enterprise Metadata Management

Enterprise metadata management (EMM) is a set of features introduced in Microsoft SharePoint Server 2010 that enable taxonomists, librarians, and administrators to create and manage terms and sets of terms across the enterprise. Key features include:

  • A new Managed Metadata Service that provides consistent metadata and shared taxonomies across multiple SharePoint Server sites in the enterprise.

  • The Managed Metadata Service is a single term store. Term stores are databases that contain one or more taxonomies. Multiple managed metadata services (term stores) can be associated with a single Web application.

  • Taxonomies are hierarchical groupings of metadata, hierarchy, and other elements that provide meaning (such as descriptions, synonyms, and translations).

  • The Term Management tool, which provides:

    • Service management: Access all available term store databases from one location.

    • Security groups: Control how users can create, edit, or delete by using SharePoint Server users and groups.

    • Term sets: Containers that you can use to organize, group, and share hierarchies of terms.

    • The ability to apply metadata in the Web browser, through Microsoft Office 2010 client applications and through third-party custom applications, by using the Taxonomy object model. The Taxonomy object model is described later in this article.

The Managed Metadata Service and Term Management tool capabilities that are available through the user interface (UI) are powerful and flexible, and enable users to manage diverse, large, and complex metadata and taxonomies. The Taxonomy APIs that are available through the object model are powerful and extensive, and enable you to do everything that you can do with the Managed Metadata Service and Term Management tool capabilities by using the UI.

This article provides a very simple and brief introduction to the Taxonomy object model. It does not cover the full breadth and depth of the APIs. Instead, this article explains basic concepts and syntax that you can use as building blocks to get started writing your own custom enterprise metadata applications. The article provides a complete code sample that demonstrates basic programming tasks, and steps through the concepts and syntax of each task in detail.

Who Is This Article For?

This article is written for developers who are new to EMM in SharePoint Server 2010, who may be new to SharePoint development, and who are interested in being introduced to some fundamental building blocks of the expansive Taxonomy object model. This article requires a basic working knowledge of Microsoft Visual C#, Microsoft .NET, and Web development, including ASP.NET.

Prerequisites

Before you can run the sample described in this article:

  1. Set up your SharePoint Server 2010 development environment as described in Setting Up the Development Environment for SharePoint 2010 on Windows Vista, Windows 7, and Windows Server 2008 in the Microsoft SharePoint Server 2010 SDK. This includes installing prerequisites for SharePoint Server 2010 as specified for your operating system, configuring your system to run SharePoint Server 2010, installing SharePoint Server 2010, and installing Microsoft Visual Studio 2010 Beta 1.

  2. Provision the term store through the UI or by using Windows PowerShell.

  3. Read the following topics in the Microsoft SharePoint Server 2010 SDK:

The Taxonomy APIs

Most of the APIs used to develop EMM solutions are in the Microsoft.SharePoint.Taxonomy namespace. This article focuses on some of the most commonly used classes, methods, and properties:

  • TaxonomySession class

  • TermStore class

  • Group class

  • TermSet class

  • Term class

  • CommitAll method

  • IsAvailable property

  • Name property

  • CreateLabel method

  • SetDescription method

TaxonomySession Class

The TaxonomySession class creates a new session in which to instantiate objects and commit changes transactionally to the TermStore object. A TaxonomySession object can have zero or more TermStore objects associated with it. TermStore objects are associated with the Web application of the parent SPSite object.

The following code snippet introduces the first section of the code sample that is included in its entirety at the end of this article. The snippet declares a new instance of the SPSite object at https://localhost, instantiates a new TaxonomySession object for the site, and instantiates a new managed metadata service connection to the TermStore object for the session.

using (SPSite site = new SPSite("https://localhost"))
{
//Instantiates a new TaxonomySession for the current site.
TaxonomySession session = new TaxonomySession(site);
//Instantiates the named "Managed Metadata Service Connection" for the current session.
TermStore termStore = session.TermStores["Managed Metadata Service Connection"];

Groups, Term Sets, and Terms

EMM provides rules and containers of metadata that make it easier to reinforce hierarchical relationships. After a term store is provisioned, users can add groups, term sets, and terms (in that order) to the term store and create relationships between them.

The items included in a taxonomy are based on the TaxonomyItem base class. The Group class, TermSet class, Term class, and certain other classes in the Microsoft.SharePoint.Taxonomy namespace are implementations of the TaxonomyItem class. Container objects based on the TaxonomyItem class, including Group objects, TermSet objects, and Term objects, are created by following these rules:

  • After a Group object is created, the first TermSet object can be created. A TermSet object must be the child of a single parent Group object.

  • After a TermSet object is created, the first Term object can be created. A Term object can be the child of a TermSet object, or of another Term object.

  • After a Term object is created, another Term object can be created and added as a child Term object.

The example in Figure 1 shows a Group object, Group1, that has one child: a TermSet object named termSet1. termSet1 is the parent of three child Term objects: Term1, Term2, and Term3. Finally, Term1 is a parent to two child terms, Term1a and Term1b.

Figure 1. Example of a taxonomy hierarchy

Example of a taxonomy hierarchy

CommitAll Method

The Taxonomy API uses the CommitAll method of the TermStore object to commit transactions to the TermStore object. Examples of transacations include creating, deleting, and modifying Group objects, TermSet objects, and Term objects.

The CommitAll method is transactional, meaning that all items to be committed in the current transaction are either successfully committed or none of the items are committed. The Taxonomy API always commits zero or all transactions. This helps to manage changes to the TermStore object systematically and helps to ensure that the term store always includes the latest intended set of metadata or the last known good set of metadata.

The Taxonomy object model also includes a change API, with types such as ChangedItem and ChangedGroup, that are used to record the history of CommitAll transactions.

termStore.CommitAll();

SetDescription Method

The Term object includes a SetDescription method. This method sets a string that describes the object and sets the LCID as a 32-bit integer.

term1.SetDescription("This is term1", 1033);

CreateLabel Method

EMM uses labels to manage synonyms and other different names for a source Term object. The CreateLabel method instantiates a new synonym or label for a Term object. For example, if the Name property for a Term object is set to "Dog", but others in your organization use the word canine to refer to the same Term object, use the CreateLabel method to assign a string and an LCID to the Term object. Additionally, you can set whether the label is the default to true or false.

term1.CreateLabel("TermOne", 1033, false);

term1.CreateLabel("FirstTerm", 1033, false);

The default Name property for a Term object is an example of a CreateLabel method that sets the default to true.

Code Sample: Create and Commit a Taxonomy, Add Labels, and Delete a Term

In previous sections, we introduced common classes and methods used to develop custom metadata solutions with the Taxonomy APIs. The following code sample presents the concepts and code discussed in previous sections as a unified whole. Use this sample as a building block for your own custom metadata applications.

The sample:

  1. Creates a new TaxonomySession object.

  2. Creates taxonomy items in the session such as a Group object, a TermSet object, and several Term objects. It also commits all changes to the TermStore object.

  3. Uses the SetDescription method to create a descriptive string for a given LCID to a Term object.

  4. Uses the CreateLabel method to create synonyms and alternate labels, then assigns them to a Term object, and commits the changes.

  5. Demonstrates how to delete a Term object from the TermStore and commit the change. While not shown in this sample explicitly, Group objects and TermSet objects can be deleted in a similar way.

using (SPSite site = new SPSite("https://localhost"))
{
//Instantiates a new TaxonomySession for the current site.
TaxonomySession session = new TaxonomySession(site);
//Instantiates the connection named "Managed Metadata Service Connection" for the current session.
TermStore termStore = session.TermStores["Managed Metadata Service Connection"];

// Creates and commits a Group object named Group1, a TermSet object named 
// termSet1, and several Term objects. Term1, Term2, and Term3 are members of 
// termSet1. Term1a and Term1b are children of Term1.
// 
Group group1 = termStore.CreateGroup("Group1");
TermSet termSet1 = group1.CreateTermSet("TermSet1");
Term term1 = termSet1.CreateTerm("Term1", 1033);
Term term2 = termSet1.CreateTerm("Term2", 1033);
Term term3 = termSet1.CreateTerm("Term3", 1033);
Term term1a = term1.CreateTerm("Term1a", 1033);
Term term1b = term1.CreateTerm("Term1b", 1033);
termStore.CommitAll();

// Sets a description and some alternate labels for term1 and commits
// the changes to termStore.
term1.SetDescription("This is term1", 1033);
term1.CreateLabel("TermOne", 1033, false);
term1.CreateLabel("FirstTerm", 1033, false);\
termStore.CommitAll();

// Deletes an unnecessary term, term3, from termStore and commits the change.
term3.Delete();
termStore.CommitAll();

Conclusion

SharePoint Server 2010 includes Taxonomy APIs that you can use to write custom applications that create hierarchies of metadata. The object model extends beyond metadata management capabilities and tools available through the UI, and provides powerful capabilities such as enabling metadata-driven navigation and search and providing values for custom properties. This article provides a basic introduction to some common development tasks, such as creating a taxonomy session, committing new taxonomy items to the term store, updating and modifying taxonomy items such as Group objects, TermSet objects, and Term objects, and creating new labels for Term objects.

Additional Resources

For more information, see the following resources: