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

%3CLINGO-SUB%20id%3D%22lingo-sub-49704%22%20slang%3D%22en-US%22%3ESharePoint%20Online%20-%20Upload%20document%20using%20PowerShell%20%2F%20CSOM%20with%20meta%20data%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-49704%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20trying%20to%20upload%20document%20to%20SharePoint%20Online%20along%20with%20meta%20data%20using%20PowerShell%20CSOM%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%24Context%20%3D%20New-Object%20Microsoft.SharePoint.Client.ClientContext(%24siteURL)%0A%24Creds%20%3D%20New-Object%20Microsoft.SharePoint.Client.SharePointOnlineCredentials(%24User%2C%24Password1)%0A%24Context.Credentials%20%3D%20%24Creds%0A%0A%23Retrieve%20list%0A%24DocLibName%20%3D%20'Documents'%0A%24List%20%3D%20%24Context.Web.Lists.GetByTitle(%24DocLibName)%0A%24Context.Load(%24List)%0A%24Context.ExecuteQuery()%0A%0A%24FileCreationInfo%20%3D%20New-Object%20Microsoft.SharePoint.Client.FileCreationInformation%0A%24FileCreationInfo.Overwrite%20%3D%20%24true%0A%24FileCreationInfo.ContentStream%20%3D%20%24FileStream%20%0A%24FileCreationInfo.URL%20%3D%20'test123.pdf'%0A%24Upload%20%3D%20%24List.RootFolder.Files.Add(%24FileCreationInfo)%0A%0A%24listItem%20%3D%20%24Upload.ListItemAllFields%0A%24listItem%5B%22Title%22%5D%20%3D%20'Title'%0A%24listItem%5B%22PNo%22%5D%20%3D%20'1234'%0A%24listItem%5B%22Desc%22%5D%20%3D%20'Some%20Text'%0A%0A%0A%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%20Set%20Value%20for%20Managed%20Metadata%20Column%20%23%23%23%23%23%23%23%23%23%23%20%0A%0A%0A%0A%24listItem.Update()%0A%24Context.Load(%24Upload)%0A%24Context.ExecuteQuery()%3C%2FPRE%3E%3CP%3EI%20have%20issue%20when%20try%20to%20set%20value%20of%26nbsp%3BManaged%20Metadata%20column%20column.%20%26nbsp%3BTried%20following%20approaches%20and%20not%20working.%20Error%20-%20Value%20is%20not%20in%20expected%20range%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%24listItem%5B%22ManagedMetaDataColumn%22%5D%20%3D%20'f31984fe-f02b-4354-ac6a-cdaa148c1357'%20%3C%2FPRE%3E%3CP%3Eand%20other%20approach%20for%20which%20error%20is%20-%20Method%20is%20not%20supported%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%24fieldPDT%20%3D%20%24list.Fields.GetByInternalNameOrTitle(%22ManagedMetaDataColumn%22)%0A%24Context.Load(%24fieldPDT)%0A%24Context.ExecuteQuery()%0A%20%20%20%20%20%20%20%20%0A%24taxField%20%3D%20%5BMicrosoft.SharePoint.Client.ClientContext%5D.GetMethod(%22CastTo%22).MakeGenericMethod(%5BMicrosoft.SharePoint.Client.Taxonomy.TaxonomyField%5D).Invoke(%24context%2C%20%24fieldPDT)%0A%24taxFieldValueCol%20%3D%20New-Object%20Microsoft.SharePoint.Client.Taxonomy.TaxonomyFieldValueCollection(%24context%2C%20%24term%20%2C%20%24taxField)%0A%24taxField.SetFieldValueByValueCollection(%24listItem%2C%20%24taxFieldValueCol)%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMy%20question%20is%20can%20we%26nbsp%3Bspecify%20%22Managed%20Metadata%22%20column%20when%20uploading%20the%20document.%20If%20yes%2C%20any%20help%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-54465%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20Online%20-%20Upload%20document%20using%20PowerShell%20%2F%20CSOM%20with%20meta%20data%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-54465%22%20slang%3D%22en-US%22%3E%3CP%3EFor%20other%2C%20I%20resolved%20this%20by%20using%20PnP%20-%20Powershell.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20can%20use%20either%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FSharePoint%2FPnP-PowerShell%2Fblob%2Fmaster%2FDocumentation%2FSetPnPListItem.md%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3ESet-PnPListItem%3C%2FA%3E%26nbsp%3BOR%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FSharePoint%2FPnP-PowerShell%2Fblob%2F2.12.1702.0%2FDocumentation%2FSetPnPTaxonomyFieldValue.md%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3ESet-PnPTaxonomyFieldValue%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-49951%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20Online%20-%20Upload%20document%20using%20PowerShell%20%2F%20CSOM%20with%20meta%20data%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-49951%22%20slang%3D%22en-US%22%3E%3CP%3EVerified%20GUID%20and%20tried%20but%20same%20issue.%3C%2FP%3E%3CP%3EIf%20I%20try%20to%20get%20reference%20of%20list%20item%20and%20update%20mata%20data%2C%20others%20columns%20are%20populated%20but%20not%20managed%20matadata%20column.%3C%2FP%3E%3CP%3EThis%20time%2C%20I%20don't%20have%20error%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%20%20%20%20%20%20%20%20%24Context%20%3D%20CreateConnection%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%23Retrieve%20list%0A%20%20%20%20%20%20%20%20%24DocLibName%20%3D%20'Documents'%0A%20%20%20%20%20%20%20%20%24List%20%3D%20%24Context.Web.Lists.GetByTitle(%24DocLibName)%0A%20%20%20%20%20%20%20%20%24Context.Load(%24List)%0A%20%20%20%20%20%20%20%20%24Context.ExecuteQuery()%0A%0A%20%20%20%20%20%20%20%20%24qry%20%3D%20New-Object%20Microsoft.SharePoint.Client.CamlQuery%20%0A%20%0A%20%20%20%20%20%20%20%20%24items%20%3D%20%24List.GetItems(%24qry)%0A%20%20%20%20%20%20%20%20%24Context.Load(%24items)%0A%20%20%20%20%20%20%20%20%24Context.ExecuteQuery()%0A%0A%20%20%20%20%20%20%20%20foreach(%24item%20in%20%24items)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20try%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%24item%5B%22ManagedMetaDataColumn%22%5D%20%3D%20%22f31984fe-f02b-4354-ac6a-cdaa148c1357%22%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%24item%5B%22New%22%5D%20%3D%20'12344'%0A%20%20%20%20%20%20%20%20%20%20%20%20%24item.Update()%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%24Context.ExecuteQuery()%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20catch%20%5BException%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24strException%20%3D%20%24_.Exception%20%2B%20%24_.ErrorDetails%20%2B%20%24_.ScriptStackTrace%20%2B%20%24_.ErrorDetails%20%2B%20%24_.Exception%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-49716%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20Online%20-%20Upload%20document%20using%20PowerShell%20%2F%20CSOM%20with%20meta%20data%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-49716%22%20slang%3D%22en-US%22%3EYour%20first%20script%20is%20working%2C%20please%20check%20if%20you%20have%20got%20the%20GUID%20from%3CBR%20%2F%3ESite%20Settings%20-%26gt%3B%20Term%20Store%20Management%20Tool%20-%26gt%3B%20select%20group%20-%26gt%3Bterm%20set%20--%26gt%3B%20select%20term%20--%26gt%3B%20Unique%20Identifier%3C%2FLINGO-BODY%3E
Highlighted
Occasional Contributor

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
Highlighted
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
Highlighted

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
            }
        }

 

Highlighted

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

 

You can use either Set-PnPListItem OR Set-PnPTaxonomyFieldValue