Monday, October 28, 2013

Adding Taxonomy Field to a Content Type or a SPList – SharePoint 2013

Managed metadata is a hierarchical collection of centrally managed terms that we can define, and then use as attributes for items. Taxonomy term stores are created through the SharePoint Central Administration and consist of managed metadata Groups, Term Sets, and Terms.

In here Terms can be divided into two types.

Managed terms: created by users and often organized into a hierarchy.
Enterprise keywords: words or phrases that are added to SharePoint items. All enterprise keywords are part of a single, non-hierarchical term set.

SharePoint Server 2013 includes a predefined column named Enterprise Keywords. We can add this column to content types or lists. When a user adds a value to the Enterprise Keywords column, the enterprise keyword control is displayed. Enterprise keywords control allows users to select enterprise keywords in addition to managed terms and by default it allows for multiple values.

Enterprise Keywords column can be added to a content type by using follow PowerShell function:

function AddTaxonomyFieldInContentTypes([string]$listname, [string]$weburl){
 $web=get-spweb $weburl
 $targetlist=$web.lists[$listname]
 if( $targetlist -ne $null){
   $entField=$targetlist.Fields | where {$_.title -eq "Enterprise Keywords"}
   if($entField -ne $null){
      $targetlist.Fields.Delete($entField);
   }
   $newField=$web.AvailableFields| where {$_.title -eq "Enterprise Keywords"}
   $targetlist.ContentTypes | foreach {
      if($_.name -ne "Folder"){
         $_.FieldLinks.Add($newField); $_.Update()
      }
   }          
   $targetlist.Update();
 } else {
    write-host "$listname list is empty"
 }
}

Enterprise Keywords column can be added to a list by using follow PowerShell function:


function AddTaxonomyFieldInLists([string]$listname, [string]$weburl){
 $web=get-spweb $weburl
 $targetlist=$web.lists[$listname]
 if( $targetlist -ne $null){
   $entField=$targetlist.Fields | where {$_.title -eq "Enterprise Keywords"}
   if($entField -ne $null){
      $targetlist.Fields.Delete($entField);
   }
   $newField=$web.AvailableFields| where {$_.title -eq "Enterprise Keywords"}
   $targetlist.Fields.Add($newField);
   $targetlist.Update();
   try{
     $view = $targetlist.DefaultView
     if(!$view.ViewFields.ToStringCollection().Contains($newField))
     {
       $view.ViewFields.add($newField)
       $view.Update()
     }
     write-host "$listname list and view Updated"
   } catch {
     write-host "$listname Failed to Update View field."
   }  
 } else {
   write-host "$listname list is empty"
 }
}

No comments: