Sunday, 9 June 2013

Create managed metadata column using feature

Create managed metadata column using feature

I have seen lots of blogs which talks about creating managed metadata column via feature but I haven’t got a single article which provided me a complete solution. In this blog, I am going to talk on creating managed metadata column using feature and assigning it a to proper term set.
Click here to download the sourecode.
Pre-requisites.
Create a Managed metadata column with the below settings.
  •  Group Name: Electronics
  • Term set: Computer
  • Term:  Dell, Samsung, HP
Below is the screen shot after creating the column.






 Creating Column via Feature.
1.       Open Visual Studio and create an Empty SharePoint Project(  I have named as ManagedMetadata_Column)
2.       Add a New Item. Select Empty Element.

 3. Paste the below section in between the elements tab. 
  <Field DisplayName="MMSingleSelect"
       ID="{D069F181-5674-4D87-BDA8-2AC3CE6F6E39}"
       Type="TaxonomyFieldType"
       DisplaceOnUpgrade="TRUE"
       Required="FALSE"
       Group="Simple Rule Site Columns"
       SourceID="http://schemas.microsoft.com/sharepoint/v3"
       StaticName="MMSingleSelect"
       Name="MMSingleSelect"
       Overwrite="TRUE">
  </Field>
  <Field DisplayName=" MMMultiSelect"
         ID="{B1D07B1A-60B9-40A9-A53C-48BAB6FABD23}"
         Type="TaxonomyFieldTypeMulti"
         DisplaceOnUpgrade="TRUE"
         Required="TRUE"
         Mult="TRUE"
         Group="Simple Rule Site Columns"
         SourceID="http://schemas.microsoft.com/sharepoint/v3"
         StaticName="MMMultiSelect"
         Name="MMMultiSelect"
         Overwrite="TRUE">
  </Field>

4. Above section adds 2 columns with Multi Select enabled and another one with Multi Select disabled.
Click on the feature and click on the Add event Receiver. This will add new cs class. Uncomment the FeatureActivated method and paste the below code. Basically, this code will read the field and will update the column values. This code is self-explanatory



public class Managed_Metadata_ColumnEventReceiver : SPFeatureReceiver
{
public const string METADATA_SERVICE = "Managed Metadata Service";
public const string MMSingleSelect = "MMSingleSelect";
public const string MMMultiSelect = "MMMultiSelect";
public const string ATTRIBUTE_GROUP = "Electronics";
public const string TERMSET = "Computer";
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb currentWeb = null;
XmlDocument fieldDefinition = null;
XPathNavigator fieldNavigator = null;
XPathNodeIterator selectedNodes = null;
try
{
// feature is scoped at Site, so the parent is type SPSite rather than SPWeb..
using (SPSite site = properties.Feature.Parent as SPSite)
{
if (site != null)
{
currentWeb = site.RootWeb;
}
using (currentWeb)
{
string listName = string.Empty;
string columnName = string.Empty;
string groupName = string.Empty;
string finalCaml = string.Empty;
string hiddenListId = string.Empty;
string webId = string.Empty;
fieldDefinition = new XmlDocument();
fieldDefinition.LoadXml(currentWeb.Fields.SchemaXml);
fieldNavigator = fieldDefinition.CreateNavigator();
fieldNavigator.MoveToRoot();
selectedNodes = fieldNavigator.Select("//Field[@Type='TaxonomyField'] | //Field[@Type='TaxonomyFieldType'] | //Field[@Type='FilteredLookupTextField']| //Field[@Type='TaxonomyFieldTypeMulti']");
while (selectedNodes.MoveNext())
{
groupName = selectedNodes.Current.GetAttribute("Group"string.Empty);
if (groupName == "Simple Rule Site Columns")
{
TaxonomySession session = new Microsoft.SharePoint.Taxonomy.TaxonomySession(site);
TermStore termstore = session.TermStores[METADATA_SERVICE];
columnName = selectedNodes.Current.GetAttribute("Name"string.Empty);
TaxonomyField taxonomyColumn = currentWeb.Fields.GetFieldByInternalName(columnName) as TaxonomyField;
currentWeb.AllowUnsafeUpdates = true;
switch (columnName)
{
case MMSingleSelect:
SetTaxonomyTermSet(termstore, taxonomyColumn, session, ATTRIBUTE_GROUP, TERMSET);
break;
case MMMultiSelect:
SetTaxonomyTermSet(termstore, taxonomyColumn, session, ATTRIBUTE_GROUP, TERMSET);
break;
}
}
currentWeb.AllowUnsafeUpdates = false;
}
}
}
}
catch (Exception ex)
{
//
}
}
private void SetTaxonomyTermSet(TermStore termstore, TaxonomyField taxonomyColumn, TaxonomySession session,string termStoreGroup, string termSet)
{
Group group = termstore.Groups[termStoreGroup];
TermSet termset = group.TermSets[termSet];
taxonomyColumn.SspId = termstore.Id;
taxonomyColumn.TermSetId = termset.Id;
taxonomyColumn.Update();
}
}

 
5. Once the WSP is deployed and go activate the feature ManagedMetadata_Column Feature. You should see 2 column created under "Simple Rule Site Columns".
Click here to download the sourecode.

No comments:

Post a Comment