File Unggah ke folder yang salah di Sharepoint Online

Saya telah mencoba mengunggah file dan folder ke SharePoint Online menggunakan PowerShell. Saya hampir sampai tetapi ada yang salah dengan kode dalam metode rekursif. Itu mengunggah file ke folder di bawah yang seharusnya. Misalnya jika saya memiliki struktur folder dan setiap folder memiliki 3 file:

Folder1\Folder2\Folder3

File di Folder2 dan Folder3 akan diunggah ke folder 3.

Saya tahu bahwa folder tersebut diunggah terlebih dahulu sehingga jalur $UploadSubFolder diperbarui ke folder bawah tetapi saya tidak tahu bagaimana cara memperbaikinya! Bisakah Anda menulis sedikit kode agar file diunggah terlebih dahulu?

Adakah yang bisa membantu saya mengatasi masalah ini? Terima kasih!

    Function ImportFiles()
{
   ForEach($File in Get-ChildItem $CurrentFolder)
   {    
        If($File.PSIsContainer -eq $false)
        {  
              #Upload File to Folder in Sharepoint
              $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
              $Upload = $UploadSubFolder.Files.Add($FileCreationInfo)
              $Context.Load($Upload)
              $Context.ExecuteQuery() 
        }
        ElseIf($File.PSIsContainer -eq $True)
        { 
              #Upload SubFolder

              $CurrentFolder = $CurrentFolder + "\" + $File.Name
              $NewFolder1 = Split-Path $File -leaf
              $UploadSubFolder = $UploadSubFolder.Folders.Add($NewFolder1) 
              $Context.Load($UploadSubFolder)
              $Context.ExecuteQuery()

              ImportFiles     
        }
        Else
        {
            Write-Host "Upload Complete"
        }
    }
}
#Get name of Root folder
$NewFolder = Split-Path $Folder -Leaf
#upload Root folder to sharepoint 
$UploadFolder = $List.RootFolder.Folders.Add($NewFolder)
$Context.Load($UploadFolder)
$Context.ExecuteQuery()

ForEach($File in (dir $Folder))
{
    if($File.PSIsContainer -eq $false)
    {
          #Upload File to Folder in Sharepoint
          $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
          $Upload = $UploadFolder.Files.Add($FileCreationInfo)
          $Context.Load($Upload)
          $Context.ExecuteQuery()
    }
    else
    {
            $CurrentFolder = $Folder + "\" + $File.Name
            #upload folder
            $NewFolder = Split-Path $File -leaf
            $UploadSubFolder = $UploadFolder.Folders.Add($NewFolder) 
            $Context.Load($UploadSubFolder)
            $Context.ExecuteQuery()
            ImportFiles 
        }
}

person DaBradley    schedule 28.07.2014    source sumber


Jawaban (1)


Skrip PS berikut ini dapat digunakan untuk mengunggah file ke Pustaka Dokumen di SharePoint Online.

Cara mengunggah file ke Pustaka Dokumen di SharePoint Online

Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"



Function Ensure-Folder()
{
Param(
  [Parameter(Mandatory=$True)]
  [Microsoft.SharePoint.Client.Web]$Web,

  [Parameter(Mandatory=$True)]
  [Microsoft.SharePoint.Client.Folder]$ParentFolder, 

  [Parameter(Mandatory=$True)]
  [String]$FolderUrl

)
    $folderUrls = $FolderUrl.Trim().Split("/",[System.StringSplitOptions]::RemoveEmptyEntries)
    $folderUrl = $folderUrls[0]
    $curFolder = $ParentFolder.Folders.Add($folderUrl)
    $Web.Context.Load($curFolder)
    $web.Context.ExecuteQuery()

    if ($folderUrls.Length -gt 1)
    {
        $curFolderUrl = [System.String]::Join("/", $folderUrls, 1, $folderUrls.Length - 1)
        Ensure-Folder -Web $Web -ParentFolder $curFolder -FolderUrl $curFolderUrl
    }
}



Function Upload-File() 
{
Param(
  [Parameter(Mandatory=$True)]
  [Microsoft.SharePoint.Client.Web]$Web,

  [Parameter(Mandatory=$True)]
  [String]$FolderRelativeUrl, 

  [Parameter(Mandatory=$True)]
  [System.IO.FileInfo]$LocalFile

)

    try {
       $fileUrl = $FolderRelativeUrl + "/" + $LocalFile.Name
       [Microsoft.SharePoint.Client.File]::SaveBinaryDirect($Web.Context, $fileUrl, $LocalFile.OpenRead(), $true)
    }
    finally {
       #$fileStream.Close()
    }
}




function Upload-Files()
{

Param(
  [Parameter(Mandatory=$True)]
  [String]$Url,

  [Parameter(Mandatory=$True)]
  [String]$UserName,

  [Parameter(Mandatory=$False)]
  [String]$Password, 

  [Parameter(Mandatory=$True)]
  [String]$TargetListTitle,

  [Parameter(Mandatory=$True)]
  [String]$SourceFolderPath

)

    if($Password) {
       $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
    }
    else {
      $SecurePassword = Read-Host -Prompt "Enter the password" -AsSecureString
    }
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,$SecurePassword)
    $Context.Credentials = $Credentials


    $web = $Context.Web 
    $Context.Load($web)
    $list = $web.Lists.GetByTitle($TargetListTitle);
    $Context.Load($list.RootFolder)
    $Context.ExecuteQuery()


    Get-ChildItem $SourceFolderPath -Recurse | % {
       if ($_.PSIsContainer -eq $True) {
          $folderUrl = $_.FullName.Replace($SourceFolderPath,"").Replace("\","/")   
          if($folderUrl) {
             Ensure-Folder -Web $web -ParentFolder $list.RootFolder -FolderUrl $folderUrl
          }  
       }
       else{
          $folderRelativeUrl = $list.RootFolder.ServerRelativeUrl + $_.DirectoryName.Replace($SourceFolderPath,"").Replace("\","/")  
          Upload-File -Web $web -FolderRelativeUrl $folderRelativeUrl -LocalFile $_ 
       }
    }
}

Sumber: Inti

Penggunaan

$Url = "https://contoso.sharepoint.com"
$UserName = "[email protected]"
$Password = "password"
$TargetListTitle = "Documents"   #Target Library
$SourceFolderPath = "C:\Users\me\Upload"  #Source Physical Path 


#Upload files
Upload-Files -Url $Url -UserName $UserName -Password $Password -TargetListTitle $TargetListTitle -SourceFolderPath $SourceFolderPath

Poin-poin penting:

  • Pertahankan struktur folder
person Vadim Gremyachev    schedule 28.07.2014