Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm looking for a powershell script that allows me add metadata column in SharePoint list. Thanks

share|improve this question
    
Do you already have site columns created of type managed metadata? or you want to create that also? –  Ashish Patel Jan 27 '12 at 17:01
add comment

3 Answers 3

There is a neat blog post where this is mentioned with detailed steps and snapshots. Please refer - http://get-spscripts.com/2011/03/configuring-metadata-navigation.html

EDIT: There is also a blog post which shows how managed metadata column is added using Client Object model in SharePoint 2010. Replace them with Powershell scripts and you shall get your result - http://pholpar.wordpress.com/2010/02/11/adding-a-managed-metadata-column-to-a-list-via-sharepoint-server-2010-object-model/

share|improve this answer
    
Hi Deepu Nair Thanks for the reply but i'm looking for adding simple metadat columns to a list.The above link is about metadta navigation settings.Please Kindly help me out. –  vikram06 Jan 27 '12 at 16:53
    
Thank you Nair.I will try that out.Thanks alot... –  vikram06 Jan 27 '12 at 18:22
    
Hi Nair tried to do with the code but having some issue my list is creating the columns except the metadata column which is needed..here is the script i used –  vikram06 Jan 27 '12 at 19:10
add comment

Sorry for the self promotion, but I have a post that shows you how to create a Managed Metadata column & add it to an existing list. Downloadable project as well: http://www.andrewconnell.com/blog/archive/2011/07/25/sharepoint-2010-managed-metadata-creating-managed-metadata-columns.aspx

share|improve this answer
add comment

The code below is a mix of some PS code i had laying around and stuff translated from C# on the fly (so there might be some errors in there :-D)

Also, this code creates a fullblown site column in the SPWeb and adds that to the list

To start, you need a reference to the termstore, i've written 2 functions that use CA and the metadata service app's type to get a standard installation's Termstore:

function Get-TaxonomySessionDefault()
{
  $centralAdmin = Get-SPWebApplication -IncludeCentralAdministration | Where {$_.IsAdministrationWebApplication} | Get-SPSite
  $session = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($centralAdmin)
  return $session
}

function Get-TermStoreDefault()
{
  $session = Get-TaxonomySessionDefault
  $serviceApp = Get-SPServiceApplication | Where {$_.TypeName -like "*Metadata*"}
  $termStore = $session.TermStores[$serviceApp.Name]    
  return $termStore;
}

now you can use these methods to access the termstore and get a reference to a TermSet to which to bind your new field

function Get-TermSet([string]$groupName, [string]$termSetName)
{
  $termStore = Get-TermStoreDefault
  return $termStore.Groups[$groupName].TermSets[$termSetName]
}

and using that function, we can create a function that creates a managed metadata site column in an SPWeb

function Create-TaxonomyField(
  [Microsoft.SharePoint.SPWeb]$web, 
  [string]$staticName, 
  [string]$displayName, 
  [string]$fieldGroup, 
  [string]$termStoreGroupName,
  [string]$termSetName
)
{
  $termSet = Get-TermSet $termStoreGroupName $termSetName

  $taxonomyField = $web.Fields.CreateNewField("TaxonomyFieldType", $displayName)

  $taxonomyField.SspId = $termSet.TermStore.Id
  $taxonomyField.TermSetId = $termSet.Id
  $taxonomyField.AllowMultipleValues = $false
  $taxonomyField.Group = $fieldGroup
  $taxonomyField.StaticName = $staticName
  $taxonomyField.ShowInEditForm = $true
  $taxonomyField.ShowInNewForm = $true
  $taxonomyField.Hidden = $false
  $taxonomyField.Required = $false

  $web.Fields.Add($taxonomyField);

  $web.Update();

  return $taxonomyField
}

Now all we need is a function to add the field to a list

function Add-FieldToList(
  [Microsoft.SharePoint.SPWeb]$web, 
  [string]$fieldName, 
  [string]$listTitle, 
)
{
  $list = $web.Lists[$listTitle]
  $list.Fields.Add($web.Fields[$fieldName])
  $list.Update()
}

EDIT: I am a big fan of PowerGUI, a great (free!) tool to write, test and debug(!!) your powershell scripts!

EDIT: I have modified some lines to make it work

share|improve this answer
    
Thank you colin it helped me and i have done it..:-) –  vikram06 Jan 30 '12 at 15:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.