Managed Metadata using object Model
I have created a small xml file with the hierarchial representation of the metadata and our task is to ensure that the metadata is now available as Managed Metadata. This kind of solution is very much required for large deployments as it might take a lot of time to create them manually through UI (Term Store Manager).
<?xml version=”1.0″ encoding=”utf-8″ ?>
<Group Id=”3DBE9867-933E-4DC9-8744-F3E028320D24″ Name=”Automobiles” LCID=”1033″>
<TermSet Id=”B9518766-5EA5-4605-8F4A-64EA9951A515″ Name=”Honda” LCID=”1033″>
<Term Id=”{B07BEA53-3E81-419A-ADF6-E899AA96E131}” Name=”Honda City” LCID=”1033″/>
<Term Id=”{1D0C2B9C-BDBA-4672-B41B-9BA07E2B7B9E}” Name=”Honda Jazz” LCID=”1033″/>
<Term Id=”{145CDD05-E5AE-4789-8AE6-71AD4EA141FC}” Name=”Honda Civic” LCID=”1033″/>
<Term Id=”{BC46242D-F0F8-4975-86EA-F42B5FE84C7A}” Name=”Honda Scooters” LCID=”1033″/>
<Term Id=”{3946ECE0-6CDD-4871-B4C8-FA94FABF9616}” Name=”Honda Activa” LCID=”1033″/>
<Term Id=”{275FEFB8-454D-40C0-A268-B3881C0E37F9}” Name=”Honda Unicorn” LCID=”1033″/>
</TermSet>
<TermSet Id=”{07A60410-BC54-4C6C-B98D-99E9D029EBD4}” Name=”Suzuki” LCID=”1033″>
<Term Id=”{4399BD4E-2C25-4474-A168-1349672A4CC5}” Name=”Suzuki Ritz” LCID=”1033″/>
<Term Id=”{8270A601-D457-47F8-B395-792DDC851270}” Name=”Suzuki 800″ LCID=”1033″/>
<Term Id=”{BD02E454-896C-4CA3-BA90-4CA1B4C17E0A}” Name=”Suzuki Alto” LCID=”1033″/>
<Term Id=”{2F5D1CC4-5856-43DB-87B0-E51A13E0D81C}” Name=”Suzuki WagonR” LCID=”1033″/>
<Term Id=”{5A068949-B454-41CE-B991-C81BB271B757}” Name=”Suzuki SX4″ LCID=”1033″/>
<Term Id=”{A92A7C2A-4E51-41B3-97BC-D61DA9EF4AEC}” Name=”Suzuki Swift” LCID=”1033″/>
<Term Id=”{331DFA04-A1D2-4588-AE15-C22DA9184383}” Name=”Suzuki Swift DZire” LCID=”1033″/>
</TermSet>
<TermSet Id=”{A674EB20-DFE0-431F-A894-A2CA342A9DCF}” Name=”Toyota” LCID=”1033″>
<Term Id=”{02FA5C74-45A7-4644-B920-05807DF8DEF1}” Name=”Toyota Innova” LCID=”1033″/>
<Term Id=”{9772975D-B048-44F7-88D9-486B9498D297}” Name=”Toyota Etios” LCID=”1033″/>
<Term Id=”{7A3D18C1-32E6-49B1-A6D8-57641D29EF5F}” Name=”Toyota Corolla” LCID=”1033″/>
</TermSet>
<TermSet Id=”{EBDECA66-2EB4-4C7A-A545-16A0820363D1}” Name=”Chevrolet” LCID=”1033″>
<Term Id=”{7730AD60-B485-41B9-BC44-D09C0205343F}” Name=”Chevrolet Spark” LCID=”1033″/>
<Term Id=”{73C8C0B0-AA8F-4BF9-9385-16BF7C5F4FD4}” Name=”Chevrolet UVA” LCID=”1033″/>
<Term Id=”{E63D4E7A-E2AD-4032-B6C7-27DAB1452815}” Name=”Chevrolet Beat” LCID=”1033″/>
<Term Id=”{4AC1457B-8B12-4766-94D3-36B9B2E23E41}” Name=”Chevrolet Qualis” LCID=”1033″/>
</TermSet>
<TermSet Id=”{CAE94605-EEE1-446F-9BB5-83C4EC6A52C7}” Name=”Mahindra” LCID=”1033″>
<Term Id=”{A87DCD5B-967C-46F5-B0A0-9C00BE4F7BE8}” Name=”Mahindra Scorpio” LCID=”1033″/>
<Term Id=”{EF2128DA-5E3A-45D7-A982-E77C0E4A587B}” Name=”Mahindra Logan” LCID=”1033″/>
<Term Id=”{8B858F3F-B293-4FC4-9A4E-8E41C385034F}” Name=”Mahindra Xylo” LCID=”1033″/>
</TermSet>
<TermSet Id=”{7F11A8AF-04C5-4684-A85E-0DFC8AC6C785}” Name=”Tata” LCID=”1033″>
<Term Id=”{AF44A8C4-CA1E-43BC-AC6F-B0F837F8BDE6}” Name=”Tata Indica” LCID=”1033″/>
<Term Id=”{27457143-42E5-4008-BEF3-88C841C9BB7A}” Name=”Tata Indigo” LCID=”1033″/>
<Term Id=”{E08E406D-5B80-485D-BEEA-F336DA963234}” Name=”Tata Sumo” LCID=”1033″/>
<Term Id=”{FB9CF8B5-47C9-46E8-9C46-2C9CC49D48C4}” Name=”Tata Nano” LCID=”1033″/>
</TermSet>
</Group>
So inorder to work with Taxonomy in SharePoint 2010 First add the namespace
Microsoft.SharePoint.Taxonomy;
It provides all the classes required for working with Managed Metadata. So we can create, modify and delete Terms, TermSets, Groups, Keywords with the classess and methods available in this namespace.
Taxonomy is always created for a given site collection. To do so we need to start a new session to retrieve all the Termstores associated with a given site collection.
TaxonomySession session = new TaxonomySession(site); |
Once we have the TaxonomySession object, we can easily work with TermStore, Groups, TermSets, Terms and Keywords.
Some of the commonly used methods are given below:
Getting the default TermStore
TermStore oTermstore = session.Termstores[0]; |
Creating a Group
Group oGroup=oTermstore.CreateGroup(GroupName); |
Here Group Name is a string.
Creating a Term Set
Termset oTermset = oGroup.CreateTermSet(Termset Name, TermSetID, Locale ID); |
Creating a Term
Term oTerm = oTermSet.CreateTerm(TermName, LocaleID); |
The complelte code of the feature activated event receiver is provided here.
using System; using System.Runtime.InteropServices; using System.Security.Permissions; using Microsoft.SharePoint; using Microsoft.SharePoint.Security; using System.IO; using Microsoft.SharePoint.Taxonomy; using System.Xml; namespace Taxonomy.Features.ImportMetadataFeature { /// <summary> /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade. /// </summary> /// <remarks> /// The GUID attached to this class may be used during packaging and should not be modified. /// </remarks> [Guid(“2cb91558-fc42-4527-a184-6408f0abcda4”)] public class Feature1EventReceiver : SPFeatureReceiver { // Uncomment the method below to handle the event raised after a feature has been activated. string[] strValues = new string[3]; private string ID = string.Empty; private string GroupName = string.Empty; Group oGroup; TermStore oTermstore = null; Term ParentTerm; Guid GroupID; Guid TermSetID; Guid TermID; Guid GlobalTermID; public override void FeatureActivated(SPFeatureReceiverProperties properties) { string configFileKeyname = properties.Feature.Properties[“Taxonomy”].Value; string xmlConfigFilePath = Path.Combine(properties.Definition.RootDirectory, configFileKeyname); // Connect to the site and use it to set up the TaxonomySession SPSite site = properties.Feature.Parent as SPSite; // Set up the session to work with Taxonomy TaxonomySession session = new TaxonomySession(site); // Check for Term Stores. if (session.TermStores.Count != 0) { XmlTextReader oReader = new XmlTextReader(Path.Combine(properties.Definition.RootDirectory, properties.Feature.Properties[“Taxonomy”].Value)); bool startTerm=false; Term oTerm; while (oReader.Read()) { switch (oReader.NodeType) { case XmlNodeType.Element: if (oReader.Name.Equals(“Group”)) { //Create a TermStore oTermstore = session.TermStores[0];//DefaultSiteCollectionTermStore; if (oReader.AttributeCount > 0) startTerm = false; //Private method to read the attributes of the xml node private void ReadAttributes(XmlTextReader oReader) public override void FeatureDeactivating(SPFeatureReceiverProperties properties) Thats it…. We have created and updated our a taxonomy in the default term store. |