Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Update Managed Metadata field in SharePoint Online

$
0
0

I'm struggling to update a managed metadata field with PowerShell.

I was able to code the script on prem but I cannot make it work in SharePoint Online.

Here are some chunks of my code on prem:

$taxonomySession = Get-SPTaxonomySession -site "http://---"
$termStore = $taxonomySession.TermStores["Managed Metadata Service"]
$termStoreGroup = $termStore.Groups["Test"]
$termSet = $termStoreGroup.TermSets["Colors"]
$t = $termset.GetAllTerms()
$term1 = $t["Blue"]

$taxField = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$docLibrary.Fields

["MetaColumn"]

$taxField.SetFieldValue($spFile.Item, $term1)

With SharePoint online, I am able to upload 10 files and assign them a value to their choice and lookup fields. This is the code I’m using.

$sourceDocumentPath = "D:\temp\Test.docx" 
$startNumDocs = 1
$numberDocsToCreate = 10
$User = "---"
$webUrl = "https://---"
$docLibraryName = "BigLibrary"

#Add references to SharePoint client assemblies and authenticate to Office 365 site - required for CSOM
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll"
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString

#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($webUrl)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds

#Retrieve list
$uploadfolder = $Context.Web.Lists.GetByTitle($docLibraryName)
$Context.Load($uploadfolder)
$Context.ExecuteQuery()

$numDocs = $numberDocsToCreate-$startNumDocs+1
for($i=$startNumDocs; $i -le $numberDocsToCreate; $i++)
{
$File = get-item $sourceDocumentPath
$FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open)
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.ContentStream = $FileStream
$FileCreationInfo.URL = "$i" + $File.Name
$Upload = $uploadfolder.RootFolder.Files.Add($FileCreationInfo)
$listItem = $Upload.ListItemAllFields
$listItem["ChoiceColumn"] = "Blue"
$listItem["LookupColumn"] = "5;#Blue"
$listItem.Update()
$Context.Load($Upload)
$Context.ExecuteQuery()
}
write-host "Completed"

I would really appreciate if you could help me out assign Blue to the MetaColumn. Thanks


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>