1

I have been tasked to update metadata fields in a document library by reading inputs from a CSV file. Based on my little understanding of powershell, I have written the code below but it doesn't work. I hope someone can help me. Sorry if this is not an appropriate forum for this.

Add-PSSnapin Microsoft.SharePoint.PowerShell;

$siteobj = new-object Microsoft.SharePoint.SPSite("http://myServer:9600/sites/custdocstore1")
$web = Get-SPWeb "http://myServer:9600/sites/custdocstore1"

$session = New-Object Microsoft.SharePoint.Taxonomy.TaxonomySession($siteobj)

$CRMCols = Import-Csv "G:\Backup files\MetadataUpdateFromCRMToSharePointFew.csv";

#Getting the term (new value) to which we want to update the column 
$termstore = $session.TermStores["Managed Metadata Service"];  
$group = $termstore.Groups["MyCompany Customer Document Management"]; 
$termset = $group.TermSets["Document Category"];

#List/Library we want to update 
$list=$web.Lists["Documents"]; 
$taxfield = $list.Fields["Doc Category"] -as [Microsoft.SharePoint.Taxonomy.TaxonomyField];

#Assign CSV column values to variables 
foreach($CRMCol in $CRMCols) 
{ 
$CRMOrg = $CRMCol.Organisation;

$CRMDC = $CRMCol.Doc_Category;

$spFile = $web.GetFile($CRMOrg);

$termValue = $CRMDC;

$term = $termset.Terms[$termValue];

$itemValue = $spFile.items;

#SetFieldValue function is used to update taxonomy field 
$taxField.SetFieldValue($itemValue,$term);

$itemValue.SystemUpdate();

}

write-host -ForegroundColor Green "Disposing the web Object.."; 

$web.Dispose();
3
  • How do you know which items in the library to update or will all items get the same term? $itemValue really should not be $list.items as the latter is a collection. You will need to iterate through it.
    – Bunzab
    Commented Nov 28, 2016 at 11:55
  • Thank you, Bunzab. Actually, I had a GetFile function that called up the document but I wasn't very sure whether it was necessary. I will edit the code for you to see.
    – Emml
    Commented Nov 28, 2016 at 12:01
  • Hi Bunzab, I have added the text in bold but I'm afraid it doesn't work either. Hope you can help
    – Emml
    Commented Nov 28, 2016 at 12:19

1 Answer 1

1

Try something like:

$CRMOrg = $CRMCol.Organisation
$termValue = $CRMCol.Doc_Category
#Does this return a valid SPFile Object: 
$spFile = $web.GetFile($CRMOrg)
$term = $termset.Terms[$termValue]
$taxField.SetFieldValue($spFile.Item, $term)
$spFile.SystemUpdate()

Use PowerShell ISE to test this and add breakpoints (F9) at various stages to validate the process.

4
  • Hi Bunzab, Thank you very much for your help. I stepped through but had the error below on the SystemUpdate line. All the lines are OK =========================================== You cannot call a method on a null-valued expression. At G:\Backup files\Emml-Scritps&SiteColls\UpdatingManagedMetadatafromCSV_DRAFT.ps1:34 char:1 + $itemValue.SystemUpdate() + ~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
    – Emml
    Commented Nov 28, 2016 at 15:05
  • Sorry the item needs updated. Modified to $spFile.SystemUpdate().
    – Bunzab
    Commented Nov 28, 2016 at 15:25
  • Thank you Bunzab. This is the new error ============================ Method invocation failed because [Microsoft.SharePoint.SPFile] doesn't contain a method named 'SystemUpdate'. At G:\Backup files\Emml-Scritps&SiteColls\UpdatingManagedMetadatafromCSV_DRAFT.ps1:34 char:1 + $spFile.SystemUpdate() + ~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound
    – Emml
    Commented Nov 28, 2016 at 15:48
  • $spFile.item.SystemUpdate() works
    – Emml
    Commented Nov 28, 2016 at 15:58

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.