在Teamsite的DocLib中创建文件夹 - Office 365 - Sharepoint Online

时间:2016-06-30 08:59:47

标签: powershell directory office365 teamsite

我想使用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();

如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

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

下面演示了如何通过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 
}

如果您对保留文件夹结构时上传文件的方案感兴趣,请查看How to: Upload files into Office 365 via PowerShell帖子,它包含用于此目的的现成脚本。