Извлечение вложений из SharePoint онлайн с помощью PowerShell - PullRequest
0 голосов
/ 17 января 2019

В настоящее время я пытаюсь извлечь вложения из списка в SharePoint онлайн. Я нашел код онлайн, который должен это сделать, но я получаю ошибку. Код, который я нашел, выглядит следующим образом:

$webUrl = "https://mm.sharepoint.com/teams/pj-b0000"
$library = "Photos" 
#Local Folder to dump files
$tempLocation = "C:\Users\C\Documents\temp"    
$s = new-object Microsoft.SharePoint.SPSite($webUrl)    
$w = $s.OpenWeb()         
$l = $w.Lists[$library]    
foreach ($listItem in $l.Items)
{
    Write-Host "    Content: " $listItem.ID 
    $destinationfolder = $tempLocation + "\" + $listItem.ID          
    if (!(Test-Path -path $destinationfolder))        
    {            
        $dest = New-Item $destinationfolder -type directory          
    }
    foreach ($attachment in $listItem.Attachments)    
    {        
        $file = $w.GetFile($listItem.Attachments.UrlPrefix + $attachment)        
        $bytes = $file.OpenBinary()                
        $path = $destinationfolder + "\" + $attachment
        Write "Saving $path"
        $fs = new-object System.IO.FileStream($path, "OpenOrCreate") 
        $fs.Write($bytes, 0 , $bytes.Length)    
        $fs.Close()    
    }
}

но я получаю эту ошибку:

new-object : Cannot find type [Microsoft.SharePoint.SPSite]: verify that the assembly containing this type is loaded.
At C:\Users\C\Documents\SPListExtract.ps1:5 char:6
+ $s = new-object Microsoft.SharePoint.SPSite($webUrl)
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

You cannot call a method on a null-valued expression.
At C:\Users\C\Documents\SPListExtract.ps1:6 char:1
+ $w = $s.OpenWeb()
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Cannot index into a null array.
At C:\Users\C\Documents\SPListExtract.ps1:7 char:1
+ $l = $w.Lists[$library]
+ ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

поэтому я немного отредактировал этот код, но я получаю только элемент в списке, а не вложения, которые есть в элементах. Код, который я написал, выглядит следующим образом:

Connect-PnPOnline -Url 'https://mm.sharepoint.com/teams/pj-b0000' -UseWebLogin
$tempLocation = "C:\Users\C\Documents\temp"
$list = Get-PnPListItem -List 'Photos'

foreach ($listItem in $list)
{
    Write-Host "    Content: " $listItem.ID 
    $destinationfolder = $tempLocation + "\" + $listItem.ID          
    if (!(Test-Path -path $destinationfolder))        
    {            
        $dest = New-Item $destinationfolder -type directory          
    }
    foreach ($attachment in $listItem.Attachments)    
    {        
        $file = $w.GetFile($listItem.Attachments.UrlPrefix + $attachment)        
        $bytes = $file.OpenBinary()                
        $path = $destinationfolder + "\" + $attachment
        Write "Saving $path"
        $fs = new-object System.IO.FileStream($path, "OpenOrCreate") 
        $fs.Write($bytes, 0 , $bytes.Length)    
        $fs.Close()    
    }
}

Я вижу, что моя проблема в цикле foreach внутри переменной $ file, я думаю. Может ли кто-нибудь помочь мне с этим?

Большое спасибо заранее.

1 Ответ

0 голосов
/ 17 января 2019

Первая строка в ваших ошибках означает, что вы не загрузили сборку:

new-object : Cannot find type [Microsoft.SharePoint.SPSite]: verify that the assembly containing this type is loaded.

Эти сборки установлены только на сервере SharePoint:

https://social.technet.microsoft.com/Forums/en-US/4a78ed2c-efde-40fa-800c-c4ecfa68a7c4/cannot-find-type-microsoftsharepointspsite-when-running-sharepoint-powerscript-in-a-windows-10?forum=sharepointdevelopment

...