0

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.

1 Answer 1

0

I managed to access my managed metadata using the techniques outlined here:

http://ramblingsofamadcomputerguy.blogspot.com.au/search/label/TaxonomyFieldValue.PopulateFromLabelGuidPair

and here

http://nickhobbs.wordpress.com/2012/02/21/sharepoint-2010-how-to-set-taxonomy-field-values-programmatically/

In terms of the specific problem outlined in my question, I needed to use a different method of accessing the list. Instead of:

$taxonomyField = $docLib.Fields["Document Type"]

I needed to use:

$taxonomyField = $SPItem.fields.getfield("Document Type")

So getting the fields from the item and not from the library.

I still don't know what was going on with the second problem, but it is irrelevant now that I can access the fields this way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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