SharePoint Online - Upload document using PowerShell / CSOM with meta data

farhan faiz
Occasional Contributor

SharePoint Online - Upload document using PowerShe...

I am trying to upload document to SharePoint Online along with meta data using PowerShell CSOM 



$Context = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password1)
$Context.Credentials = $Creds

#Retrieve list
$DocLibName = 'Documents'
$List = $Context.Web.Lists.GetByTitle($DocLibName)

$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.ContentStream = $FileStream 
$FileCreationInfo.URL = 'test123.pdf'
$Upload = $List.RootFolder.Files.Add($FileCreationInfo)

$listItem = $Upload.ListItemAllFields
$listItem["Title"] = 'Title'
$listItem["PNo"] = '1234'
$listItem["Desc"] = 'Some Text'

################ Set Value for Managed Metadata Column ########## 


I have issue when try to set value of Managed Metadata column column.  Tried following approaches and not working. Error - Value is not in expected range 




$listItem["ManagedMetaDataColumn"] = 'f31984fe-f02b-4354-ac6a-cdaa148c1357' 

and other approach for which error is - Method is not supported




$fieldPDT = $list.Fields.GetByInternalNameOrTitle("ManagedMetaDataColumn")
$taxField = [Microsoft.SharePoint.Client.ClientContext].GetMethod("CastTo").MakeGenericMethod([Microsoft.SharePoint.Client.Taxonomy.TaxonomyField]).Invoke($context, $fieldPDT)
$taxFieldValueCol = New-Object Microsoft.SharePoint.Client.Taxonomy.TaxonomyFieldValueCollection($context, $term , $taxField)
$taxField.SetFieldValueByValueCollection($listItem, $taxFieldValueCol)



My question is can we specify "Managed Metadata" column when uploading the document. If yes, any help?



3 Replies

Re: SharePoint Online - Upload document using Powe...

Your first script is working, please check if you have got the GUID from
Site Settings -> Term Store Management Tool -> select group ->term set --> select term --> Unique Identifier

Re: SharePoint Online - Upload document using Powe...

Verified GUID and tried but same issue.

If I try to get reference of list item and update mata data, others columns are populated but not managed matadata column.

This time, I don't have error


        $Context = CreateConnection
        #Retrieve list
        $DocLibName = 'Documents'
        $List = $Context.Web.Lists.GetByTitle($DocLibName)

        $qry = New-Object Microsoft.SharePoint.Client.CamlQuery 
        $items = $List.GetItems($qry)

        foreach($item in $items)
            $item["ManagedMetaDataColumn"] = "f31984fe-f02b-4354-ac6a-cdaa148c1357" 
            $item["New"] = '12344'
            catch [Exception]
                $strException = $_.Exception + $_.ErrorDetails + $_.ScriptStackTrace + $_.ErrorDetails + $_.Exception


Re: SharePoint Online - Upload document using Powe...

For other, I resolved this by using PnP - Powershell.


You can use either Set-PnPListItem OR Set-PnPTaxonomyFieldValue 


Related Conversations
Updated: Online Interactive Guides
Maddie Egan  in  Online Interactive Guides  on
40 Replies
Custom markup in SharePoint calculated columns
Chris McNulty  in  SharePoint  on
7 Replies
GA: Microsoft PowerApps and Flow
Chris McNulty  in  SharePoint  on
41 Replies
"Get a Link" changed to "Copy Link?" When?
Matt Coats  in  SharePoint  on
17 Replies
Script Editor Web Parts & Google Chrome 57 don't work
Robert Woods  in  SharePoint  on
10 Replies