0

我想使用 Powershell 在 Sharepoint Online 上创建的 Teamsite 的文档库中创建一个示例文件夹,但遇到错误。

创建 Teamsite 后,我使用以下脚本:

#Retrieve list
$DocLibName = "Dokumente"
$FolderTitle = "Beispiel"
$List = $ctx.Web.Lists.GetByTitle($DocLibName)
$folder = $list.AddItem("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder)
$folder["Title"] = $FolderTitle
$folder.Update(); 

$ctx.Load($List)
$ctx.ExecuteQuery()

错误信息

未找到类型 [Microsoft.SharePoint.SPFileSystemObjectType]:确保已加载包含此类型的程序集。

 Line:79 Char:1
 + $ Folder = $ List.addItem ("" [Microsoft.SharePoint.SPFileSystemObjectType ] :: Folde ...

It is not possible to use an index to a null array.
Line:80 Char:1
+ $ Folder ["Title"] = $FolderTitle

It is not possible to call a method for an expression of the NULL .
Line:81 Char:1
+ $Folder.Update();

如何解决?

4

1 回答 1

1

您收到此错误,因为Microsoft.SharePoint.SPFileSystemObjectType类型属于与 Office 365兼容的SharePoint服务器端API 。

下面演示了如何通过 PowerShell 在 SharePoint Online 站点中创建文件夹(利用 SharePoint CSOM API)

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

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

    $folder = $ParentFolder.Folders.Add($folderName)
    $ParentFolder.Context.Load($folder)
    $ParentFolder.Context.ExecuteQuery()
    return $folder
}

Function Get-Context($Url,$Username,$Password){
   $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
   $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
   $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
   $ctx.Credentials = $credentials
   return $ctx
}

用法

$Url = "https://contoso.sharepoint.com/"
$UserName = "jdoe@contoso.onmicrosoft.com"
$Password = ""
$TargetFolderName = "Archive2016"   


$ctx = Get-Context -Url $Url -Username $Username -Password $Password
$parentFolder = $ctx.Web.Lists.GetByTitle("Documents").RootFolder
$folder = Create-Folder -ParentFolder $parentFolder -FolderName $TargetFolderName
Write-Host "Folder [$TargetFolderName] has been created succesfully. Url: $($folder.ServerRelativeUrl)"

要创建文件夹层次结构,可以使用以下脚本:

Function Create-FolderHierarchy()
{
Param(
  [Parameter(Mandatory=$True)]
  [Microsoft.SharePoint.Client.Folder]$ParentFolder, 

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

    $folderNames = $FolderUrl.Trim().Split("/",[System.StringSplitOptions]::RemoveEmptyEntries)
    $folderName = $folderNames[0]
    $curFolder = $ParentFolder.Folders.Add($folderName)
    $ParentFolder.Context.Load($curFolder)
    $ParentFolder.Context.ExecuteQuery()
    if ($folderNames.Length -gt 1)
    {
        $curFolderUrl = [System.String]::Join("/", $folderNames, 1, $folderNames.Length - 1)
        return Create-FolderHierarchy -ParentFolder $curFolder -FolderUrl $curFolderUrl
    }
    return $curFolder 
}

如果您对在保留文件夹结构的同时上传文件的方案感兴趣,请查看如何:通过 PowerShell帖子将文件上传到 Office 365,它包含为此目的的现成脚本。

于 2016-06-30T10:54:21.257 回答