Home

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

Highlighted
Occasional Contributor

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

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)
$Context.Load($List)
$Context.ExecuteQuery()

$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 ########## 



$listItem.Update()
$Context.Load($Upload)
$Context.ExecuteQuery()

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")
$Context.Load($fieldPDT)
$Context.ExecuteQuery()
        
$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 PowerShell / CSOM with meta data

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 PowerShell / CSOM with meta data

[ Edited ]

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)
        $Context.Load($List)
        $Context.ExecuteQuery()

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

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

 

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

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

 

You can use either Set-PnPListItem OR Set-PnPTaxonomyFieldValue 

 

Related Conversations
GA: Microsoft PowerApps and Flow
Chris McNulty  in  SharePoint  on
41 Replies
Online Interactive Guides
Maddie Egan  in  Online Interactive Guides  on
37 Replies
Announcing Power BI Web Part Preview for SharePoint
Chris McNulty  in  SharePoint  on
30 Replies
Script Editor Web Parts & Google Chrome 57 don't work
Robert Woods  in  SharePoint  on
10 Replies
Guest Users vs. External Users
Salvatore Biscari  in  SharePoint  on
10 Replies