Subscribe: SharePoint SharePoint SharePoint
Showing posts with label Create a Managed Metadata column. Show all posts
Showing posts with label Create a Managed Metadata column. Show all posts

Thursday, February 6, 2014

How to provision Managed Metadata columns in SharePoint

Once i came across a requirement from my client involving creating of a branding package for his SharePoint site in visual studio. Now in this particular package i had to add custom master pages, page layouts, css files, js files etc.

For each page layout i had to create separate content types with some site columns. These site columns too were created in visual studio.

Now one of these site columns was a metadata column i.e. a "Taxonomy Field". I did create the whole package but when i deployed it, i found that metadata columns were not connected to Term Store.

Powershell


Since this was the first time i had worked on this kind of project so was not aware that MMS Term store needs to be connected to a Taxonomy column. I could have done the same using UI, but my client wanted it to be automated.

I came across two approaches to do so. So thought of sharing those:

1. Elements.xml : In elements.xml file of the site column, we can easily specify the termsetid so that the column can connect to term set:

*Here ShowField="Term1033" is necessary field property.
NOTE: You can find the SSPID, GroupId and TermSetID from Term Store Management link in Site Settings.
 <?xml version="1.0" encoding="utf-8"?>  
 <Elements xmlns="http://schemas.microsoft.com/sharepoint/">  
  <Field  
     ID="{99e8cfdc-3edb-4d0e-b22e-0bde0cfe8dfd}"  
     Type="TaxonomyFieldType"  
      DisplayName="LOB"  
      ShowField="Term1033"  
      EnforceUniqueValues="FALSE"  
      Group="Custom"  
      StaticName="LOB"  
      Name="LOB">  
   <Customization>  
    <ArrayOfProperty>  
     <Property>  
      <Name>SspId</Name>  
      <Value  
      xmlns:q1="http://www.w3.org/2001/XMLSchema"  
      p4:type="q4:string"  
      xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">  
       9a5c42de-abb1-489e-87df-c68c2a30c9fc  
      </Value>  
     </Property>  
     <Property>  
      <Name>GroupId</Name>  
      <Value  
      xmlns:q2="http://www.w3.org/2001/XMLSchema"  
      p4:type="q2:string"  
      xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">  
       d86f5d20-8878-4b0d-8ef2-b7a24272e5f3  
      </Value>  
     </Property>  
     <Property>  
      <Name>TermSetId</Name>  
      <Value  
      xmlns:q2="http://www.w3.org/2001/XMLSchema"  
      p4:type="q2:string"  
      xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">  
       3ff7c119-8ad1-4676-b4f0-6d5618840527  
      </Value>  
     </Property>  
     <Property>  
      <Name>AnchorId</Name>  
      <Value  
      xmlns:q3="http://www.w3.org/2001/XMLSchema"  
      p4:type="q3:string"  
      xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">  
       00000000-0000-0000-0000-000000000000  
      </Value>  
     </Property>  
    </ArrayOfProperty>  
   </Customization>  
  </Field>  
 </Elements>  

2. Powershell : If we have already deployed the solution without connecting the columns to term store and we don't not want to change anything in the package. Then we can utilize powershell to connect to the Term Store :

 $centralAdmin = Get-SPWebApplication -IncludeCentralAdministration | Where {$_.IsAdministrationWebApplication} | Get-SPSite  
 $session = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($centralAdmin)  
 $serviceApp = Get-SPServiceApplication | Where {$_.TypeName -like "*Metadata*"}  
 $termStore = $session.TermStores[$serviceApp.Name]  
 $termSet = $termStore.Groups["Department"].TermSets["IT"]  
 $site = Get-SPSite http://myWebApp/sites/mySite  
 $web = $site.RootWeb  
 $taxonomyField = $web.Fields | Where { $_.Id -eq "99e8cfdc-3edb-4d0e-b22e-0bde0cfe8dfd" }  
 $taxonomyField.SspId = $termSet.TermStore.Id  
 $taxonomyField.TermSetId = $termSet.Id  
 $taxonomyField.AllowMultipleValues = $false  
 $taxonomyField.Update();  

Make sure you specify the GUID of the site column correctly, so that it can connect to the term store.

After following any of the above approaches you can see that the column is connected to Term Store.:

Powershell