0

As I cannot use Pnp.PowerShell with a SharePoint-2019 on premise, AFAIK, I've been trying workd with OData instead, using curl (or PowerShell Invoke-RestMethod or falling back to JavaScript fetch if needed). Unfortunately it happens to be much more complicated than I thought.

I ended up building this command:

curl -vvv -n -X POST  "https://sharepoint.site/site/aaa/_api/web/GetFolderByServerRelativeUrl('/site/aaa/library/folder')/Files/add" -T a.xlsx -H "Accept: application/json;odata=nometadata" -H "X-RequestDigest: xxxxxx"

HTTP/1.1 400 Bad Request

or

curl -vvv -n -X POST  "https://sharepoint.site/site/aaa/_api/web/GetFolderByServerRelativeUrl('/site/aaa/library/folder')/Files/add(url=a.xlsx,overwrite=true)" -T a.xlsx -H "Accept: application/json;odata=nometadata" -H "X-RequestDigest: xxxxxx"

HTTP/1.1 403 FORBIDDEN

{"odata.error":{"code":"-2130575252, System.Runtime.InteropServices.COMException","message":{"lang":"fr-FR","value":"La validation de la sécurité de cette page a expiré. Cliquez sur Précédente dans votre navigateur web, actualisez la page et retentez l'opération."}}}* Connection #0 to host sharepoint.site left intact

(translation: The security validation of this page has expired. Click Back in your web browser, refresh the page and try again.)

I found this PowerShell function: Upload-FileToShp, but I cannot make it work in my case, so doesn't help for having a correct curl or Invoke-RestMethod!

What's missing? I think it's an SSO issue?

I will be using Add-PnPFile, which works (even along SSO!), but I am afraid of using SharePointPnPPowerShell2019 as a shacky workaround.

1 Answer 1

0

if the script will be running on the same machine as the Sharepoint, you can use PS snapin Microsoft.SharePoint.PowerShell:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
  
#Function to Upload File
Function Upload-AllFilesFromDir($WebURL, $DocLibName, $FolderPath)
{
    #Get the Web and List to upload the file
    $web = Get-SPWeb $WebURL
       
    #Get the Target Document Library to upload
    $List = $Web.GetFolder($DocLibName)
  
    #Get the Files from Local Folder
    $Files = Get-ChildItem $FolderPath #You can filter files by: -filter “*.pdf”
 
    #upload the files
    Foreach ($File in $Files)
    {
        #Get the Contents of the file to FileStream
        $stream = (Get-Item $file.FullName).OpenRead()
     
        # Set Metadata Hashtable For the file - OPTIONAL
        $Metadata = @{"Country" = "United States"; "Domain" = "Sales"}
     
        #upload the file             
        $uploaded = $List.Files.Add($File.Name, $stream, $Metadata, $TRUE)
     
        #dispose FileStream Object
        $stream.Dispose()
    }
}
   
#call the upload function
Upload-AllFilesFromDir "https://SharePoint.crescent.com/sites/sitename/" "Shared Documents" "c:\Documents\"

https://learn.microsoft.com/en-us/answers/questions/1361849/auto-upload-file-share-documents-to-sharepoint

3
  • Thank you for your answer. I was focused on OData REST API (as PnP.PowerShell is not any more supported but for SP Online), but another solution like yours is welcome. Why do you say Ìf the script will be running on the same machine as the SharePoint? SharePoint is on a remote server, and behind a SSO authentication. Does Microsoft.SharePoint.PowerShell support SharePoint-2019 officially? Most of the time, only SharePoint Online is still supported, and documentation often confusing (at least for me). Where do I get the snap-in for SharePoint-2019 from ?
    – jgran
    Commented Feb 16 at 15:55
  • Sharepoint 2019 is supported, we are using it for automatied scripts. I think it is installed automatically with the sharepoint 2019 instalation. Maybe it is working from other machines, but we use it on the remote machine where the sharepoint is installed, thats why you do not have to authenticate, it is using your windows credentials.
    – Bálint
    Commented Feb 19 at 10:26
  • To get it work from other machines: sharepoint.stackexchange.com/questions/49967/…
    – Bálint
    Commented Feb 19 at 10:38

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.