0

I am using Powershell to (bulk) Add-SPOFile to a Sharepoint Online Doclib. I need to Set-SPOListItem properties for the new items in the process, but don't know how to get the Item ID for the newly added file. It would be great if Add-SPOFile returned an ID, or to be able to pipe from Add-SPOFile to Set-SPOListItem? What is the prescribed solution?

I am reading a CSV file, looping through the 288K entries, uploading a file for each entry, then trying to update the item properties. My code looks like this:

Connect-SPOnline -Url https://XXX.sharepoint.com/sites/sedatest -Credentials 'YYY'

$CSVPath = "ZZZ\SODA_CSVData.10.txt"
$SrcPath = "\\MDWeb01\DC\pdf\Released"
$DstFolder = "./SEDA"

$SODAData = import-csv $CSVPath -Delimiter "|"

Foreach ($r in $SODAData)
{
    $UploadFile = "$SrcPath\$($r.Series.Substring(0,1))\$($r.Filename)"
    Write-Host "Uploading $UploadFile"
    Add-SPOFile -Path $UploadFIle -Folder $DstFolder
    $Item = Get-SPOListItem -List SEDA | where {$($_.FileLeafRef) -eq $($r.Filename)}
    Set-SPOListItem -List SEDA -Identity $($Item.Id) -Values @{"LegacyID" = $r.LegacyID}
}

View Source on the doc library suggests that "FileLeafRef" is the right property name:

"ID": "51",
...
"FileLeafRef": "pf2363010800.pdf",
...

Maybe I am not referencing it properly?

3 Answers 3

0

If you use Get-SPListItems using where as your filter, you will be able to find the item ID. For instance:

Get-SPOListItems -ListTitle TestList | where {$_.Title -eq "Test Item"}

Do let me know if this helps

6
  • Sounds good @George, though I am unable to -match the uploaded filename with either $_.Name or $_.FileLeafRef... is there another variable I should be looking for? Commented Apr 19, 2016 at 15:26
  • Depends, is the output giving you a full file path (in which case it would be $_.FullName), or file name e.g. (test.txt), in which case I would have thought it would be $_.Name. Commented Apr 19, 2016 at 15:31
  • I am trying to match the filename and extension, i.e. "pf2363010100.pdf". Name, FullName and FileLeafRef all show no values when stepping into the Get-SPOListItem line above... Any idea where to find the actual property names? Commented Apr 19, 2016 at 16:04
  • I've confirmed that "FileLeafRef" is the property that contains the filename I'm looking for, but I am not getting any values for this property when doing the comparison. Am I referencing it improperly? And can I assign the results to a variable as shown above? Commented Apr 19, 2016 at 17:01
  • Try adding the following after your "where" clause: | % { $_.FileLeafRef = $FileNameVariable} Commented Apr 19, 2016 at 18:51
0

This actually made me switch to the following code instead of add-spofile:

$Context = Get-SPOContext

#Retrieve list
$List = $Context.Web.Lists.GetByTitle("DocLib")
$Context.Load($List)
$Context.ExecuteQuery()

#Upload file
Foreach ($File in (dir $folder))
{
$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 = $File.Name
$Upload = $List.RootFolder.Files.Add($FileCreationInfo)
$Context.Load($Upload)
$Context.Load($Upload.ListItemAllFields)
$Context.ExecuteQuery()
}

This will give you the ID via $Upload.ListItemAllFields.Id

0

Turns out there are a couple ways to do this; The first one I found looks like this:

    $Item = Get-SPOListItem -List SEDA -Fields "FileLeafRef" | 
      %{new-object psobject -property  @{Id = $_.Id; Filename = $_["FileLeafRef"]}} | 
      select Id, Filename | where {$_.Filename -eq $($r.Filename)}

    Set-SPOListItem -List SEDA -Identity $($Item.Id) -Values @{"LegacyID" = $r.LegacyID}

But the easier way, and the one I deployed (including the FileCreationInfo approach above), looks like this:

    $FileStream = New-Object IO.FileStream($SrcFilePath,[System.IO.FileMode]::Open)
    $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
    $FileCreationInfo.Overwrite = $true
    $FileCreationInfo.ContentStream = $FileStream
    $FileCreationInfo.URL = $_.SedaID
    $Upload = $Files.Add($FileCreationInfo)
    $context.Load($Upload)
    $context.Load($Upload.ListItemAllFields)
    $context.ExecuteQuery()

    Set-SPOListItem -List $listTitle -Identity $($Upload.ListItemAllFields.Id) -ContentType "SEDA Document" -Values $Metadata

Now I just need to figure out how to add a Check-in Comment during this upload/update without multiplying the version count?

Any ideas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.