I'm looking for a powershell script that allows me add metadata column in SharePoint list. Thanks
-
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
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
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/
-
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
-
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
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