简体   繁体   中英

How to upload a file to a hub site in SharePoint Online using PowerShell

I've created a new app, generated app id and secret, assigned full permissions for https://mydomain.sharepoint.com but I can't upload a file to the Shared Documents folder using the following PowerShell commands:

$SharePointURL = "https://mydomain.sharepoint.com"
$SharePointDestPath = "/Shared Documents"
$file = "some-file"

Connect-PnPOnline -Url $SharePointURL -ClientId "asddsf-9999-4a88-8c79-ab8cabc67889" -ClientSecret "123343sdsfsdgfg2343434345656=" -WarningAction Ignore
Write-Host "done"
Write-Host "Uploading output file ... " -NoNewline
Add-PnPFile -Path $file -Folder $SharePointDestPath
Disconnect-PnPOnline

I am getting 'access denied':

PS E:\sharepoint_online> .\script.ps1
Connecting to SharePoint online ... done
Uploading output file ... Add-PnPFile : Access denied.
At E:\sharepoint_online\script.ps1:26 char:1
+ Add-PnPFile -Path $file -Folder $SharePointDestPath -Verbose
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (:) [Add-PnPFile], ServerUnauthorizedAccessException
+ FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.AddFile

I'm probably doing something wrong or didn't make the proper settings. Can you please help me? Thanks in advance.

looks like you connect fine, but not allowed to upload, so looks like permission issues on your app creation, make sure to go to https://mydomain.sharepoint.com/_layouts/15/appinv.aspx (if you want to upload to main site, normally you would want to upload to sub sites, but just using your example) On that page type the “Client ID” that you got when you created the app into the “App ID” field and then click the “Lookup” button Add the following into Permission Request XML: 应用程序 Then click create at the bottom Click "Trust It" to set permissions and try again

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM