I am creating a Document Set for a library using powershell and that document set contains some mandatory managed metadata fields. I am able to create document set but how can I insert data into those managed metadata fields at runtime?
You have to connect to the term store and then construct a TaxonomyFieldValue and then use the target field's SetFieldValue() method to assign the value.
regards, KS
-
The link which you have provided is applicable for List managed metadata column. But, I have to add value into Document Set managed metadata column. Still not able to figure out... – Lalit Kumar Mishra Oct 31 '12 at 3:44
-
Just for information -- the column in which i have to insert data is a site column which added into the Document Set content type. – Lalit Kumar Mishra Oct 31 '12 at 3:46
Hope this helps someone.
You have to get a reference to the Managed metadata item from term store as shown below
$someTerm = $term.Terms | where-object {$_.Name -eq "SomeTerm" }
Create the document set as shown below
[Hashtable]$docSetProperties = @{}
$newDocumentSet=Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet]::Create($list.RootFolder,"TestDocSet",$DocContentType.Id, $docSetProperties )
Get a reference to the inserted document set
$id = $newDocumentSet.Item.ID
$spItem = $list.GetItemById($id)
Get a reference to the taxonomy field from the inserted document set
$taxMMField = $spItem.Fields['YourManagedMetaDataField'] -as [Microsoft.SharePoint.Taxonomy.TaxonomyField]
$taxMMField.SetFieldValue($spItem, $someTerm)
Call the Update method on the item
$spItem.Update()
This will update the Managed metadata field in the Document Set.
Complete sample
$webUrl = "http://portaladdress.local"
$inputDir = "C:\sampledata"
$spWeb = Get-SPWeb $webUrl
$spsite = $spWeb.Site
$docLibrary = $spWeb.Lists["DocumentLibraryListName"]
$spFolder = $spWeb.GetFolder($docLibrary.rootFolder.URL + "/")
$taxonomySession = Get-SPTaxonomySession -Site $spsite
$taxonomyField = $docLibrary.Fields["TaxononomyFieldName"]
$termStoreID = $taxonomyField.SspId
$termStore = $taxonomySession.TermStores[$termStoreID]
$termsetID = $taxonomyField.TermSetId
$termset = $termStore.GetTermSet($termsetID)
#################
### Get Document Set Content Type from list
$cType = $docLibrary.ContentTypes["DocumentSetContentTypeName"]
### Create Document Set Properties Hashtable
[Hashtable]$docsetProperties = @{"DescriptionFieldName"="some text"}
$docsetProperties = @{"DatefiledName"="06/06/2006"}
### Create new Document Set
$newDocumentSet = [Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet]::Create($spFolder,"documentset title",$cType.Id,$docsetProperties)
$term = $termset.GetTerms( "TermLabel", $true)
$id = $newDocumentSet.Item.ID;
$spItem = $docLibrary.GetItemById($id);
#this is a critical point: get a reference to the field by the item instead of using that from the list we got before (ie: $taxonomyField)
$taxMMField = $spItem.Fields['TaxononomyFieldName'] -as [Microsoft.SharePoint.Taxonomy.TaxonomyField];
$taxMMField.SetFieldValue($spItem, $term);
$spItem.Update();
#upload a file into the document set
$FilePath = "$inputDir\FileToBeLoadIntoTheDocumentset.pdf"
$FileName = "FileToBeLoadIntoTheDocumentset.pdf"
$file= Get-Item $FilePath
$spFile = $newDocumentSet.Folder.Files.Add($newDocumentSet.Folder.Url + "/" + $FileName,$file.OpenRead(),$false)
$spFileItem = $spFile.Item
$spFileItem["Titolo"] = "File title";
$spFileItem.Update()
$spFileItem.File.CheckIn("checkin comments", [Microsoft.SharePoint.SPCheckinType]::MajorCheckIn)
$spWeb.Dispose()