Skip to main content
edited title
Link
jgran
  • 13
  • 3

how to upload a file to a document library using OData in PowerShell with curl or Invoke-RestMehthodRestMethod

added 303 characters in body
Source Link
jgran
  • 13
  • 3

As I cannot use Pnp.PowerShellPnp.PowerShell with a SharePoint-2019 on premise, AFAIK, I've been trying to do itworkd 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'sWhat'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.

As I cannot use Pnp.PowerShell with a SharePoint-2019 on premise, AFAIK, I've been trying to do it with OData using curl. 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

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?

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.

Source Link
jgran
  • 13
  • 3

how to upload a file to a document library using OData in PowerShell with curl or Invoke-RestMehthod

As I cannot use Pnp.PowerShell with a SharePoint-2019 on premise, AFAIK, I've been trying to do it with OData using curl. 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

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?