I'm trying to learn about accessing/setting managed metadata fields using powershell (v2) in sharepoint 2010.
Based on various tutorials/forums I have the following minimal (not)working example:
$Main_Scope = Start-SPAssignment
$SiteAddress = "http://foo.com/sites/bar/"
$SPWeb = Get-SPWeb $SiteAddress
$docLib = $SPWeb.lists["Final Reporting"]
$taxonomySession = Get-SPTaxonomySession -Site $SPWeb.Site
$termStore = $taxonomySession.DefaultSiteCollectionTermStore
$taxonomyField = $docLib.Fields["Document Type"]
$termSet = $termStore.GetTermSet($taxonomyField.TermSetId)
Stop-SPAssignment $Main_Scope
The $taxonomyField object is null and hence $termStore.GetTermSet fails with the error
Cannot convert argument "0" with value "", for "GetTermSet" to type "System.Guid"
Probably related issue:
Minimal example:
$SPFile = $SPWeb.GetFile($path)
$SPItem = $SPFile.Item
$SPItem.fields
this displays all of the single line of text fields without a problem, but errors out when it reaches the managed metadata fields with the following error:
An error occurred while enumerating through a collection: Exception has been thrown by the target of an invocation..
Any ideas? I'm fairly new to both Sharepoint and Powershell so if there is any more info you need to help answer the question let me know.
Thanks in advance.
Edit: I've managed to find a different way to access the Term Store, so the first issue is solved, although the second problem still remains.