1

My file being uploaded is initiated below, I call $spFile.Item which is a SPListItem to call setFieldValue($spFile.Item,$term)

[Microsoft.SharePoint.SPFile]$spFile = $folder.Files.Add($folder.URL + "/" + $File.Name, [System.IO.Stream]$fileStream, $true)
$spSite = Get-SPSite -Identity $siteURL
$taxonomySession = Get-SPTaxonomySession -Site $siteURL
[Microsoft.SharePoint.Taxonomy.TaxonomyField]$lobField = $spSite.RootWeb.Fields["Lines of Business"] 
$termStoreID = $lobField.SspId
$termStore = $taxonomySession.TermStores[$termStoreID]
$termStore.get_Groups() | foreach{if($_.Name -eq "Corporate Store"){$termGroup = $_}}
$termGroup.get_TermSets() | foreach{if($_.Name -eq "Lines of Business"){$lobTermSet = $_}}
$lobTermCollection = $lobTermSet.GetAllTerms()
$lobTermCollection.GetEnumerator() | foreach{if($_.Name -eq $lineofbusiness){[Microsoft.SharePoint.Taxonomy.Term]$lobTerm = $_}}
$lobField.SetFieldValue($spFile.item,$lobTerm)

It's a big mess, but my previous method of doing the same thing in 2013 did not work at all. I always get the error

SetFieldValue : Exception calling "SetFieldValue" with "2" argument(s): "The given guid does not exist in the term store"

At line:1 char:24
+ $lobField.SetFieldValue <<<< ($spFile.Item,$lobTerm)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

How can I set the managed metadata properly for this file I am uploading to a document library on 2010? I have only worked with 2013 before.

I did what this guy said and it didn't work either I should add. Even properly getting the field from the file I add doesn't work. There seems to be a disconnect between the taxonomysession/termstore I am retrieving and the real one since it does not recognize terms I know are in the termset by string value

1
  • I should also note that I used PowerGUI & Management Shell both in Admin Mode Jul 26, 2016 at 15:40

1 Answer 1

0

So I never got formally introduced to sharepoint, but the error seems trivial to me.

[Microsoft.SharePoint.SPFile]$spFile = $folder.Files.Add($folder.URL + "/" + $File.Name, [System.IO.Stream]$fileStream, $true)
$spSite = Get-SPSite -Identity $siteURL
$taxonomySession = Get-SPTaxonomySession -Site $siteURL

My two changes were

[Microsoft.SharePoint.Taxonomy.TaxonomyField]$lobField = $spFile.Item.Fields["Lines of Business"]

and

$termStore = $taxonomySession.TermStores["Managed Metadata Service"]

termStore.get_Groups() | foreach{if($_.Name -eq "Corporate Store"){$termGroup = $_}}
$termGroup.get_TermSets() | foreach{if($_.Name -eq "Lines of Business"){$lobTermSet = $_}}
$lobTermCollection = $lobTermSet.GetAllTerms()
$lobTermCollection.GetEnumerator() | foreach{if($_.Name -eq $lineofbusiness){[Microsoft.SharePoint.Taxonomy.Term]$lobTerm = $_}}
$lobField.SetFieldValue($spFile.item,$lobTerm)

But I checked the GUID of the termstore called by string/hash and called by $lobField.sspid and they were the same! Oh well. also the GUID of the $lobField TaxField was the same.

If someone can explain that i will mark it as correct

Your Answer

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

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