Forum Discussion

farhan faiz's avatar
farhan faiz
Copper Contributor
Mar 02, 2017

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

  • 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
    • farhan faiz's avatar
      farhan faiz
      Copper Contributor

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

       

Resources