SharePoint Online:使用 PowerShell 创建文档集
要求:使用 PowerShell 在 SharePoint Online 中创建文档集。
SharePoint Online:使用 PowerShell 创建文档集
虽然我的另一篇文章解释了如何在 SharePoint Online 中创建文档集:在 SharePoint Online 中创建文档集 - 分步,但此博客文章将向您展示如何使用 PowerShell 在 SharePoint Online 中创建文档集。
简而言之,文档集是保存一个或多个相关文档的组织容器。这就像在桌面上为不同的项目创建文件夹,并在这些文件夹中为各个任务创建子文件夹。此脚本假定已启用文档集功能,已创建文档集的内容类型并将其添加到库中。以下是用于在 SharePoint Online 中创建文档集的 PowerShell 脚本:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.DocumentManagement.dll"
#Parameters
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$DocumentLibraryName = "Proposal Documents"
$DocumentSetName = "Crescent Inc SharePoint"
#Content Type ID from the List!
$ContentTypeID="0x0120D52000683659260B26AF42AF65E57B09B613C2005AE31D539D0FB244BAEE6EA8CB1F4D52"
#Get Credentials to connect
$Cred= Get-Credential
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Get the Artifacts
$DocumentLibrary=$Ctx.Web.Lists.GetByTitle($DocumentLibraryName)
$Ctx.Load($DocumentLibrary)
$Ctx.Load($DocumentLibrary.RootFolder)
#Get content type from library by ID
$ContentType = $DocumentLibrary.ContentTypes.GetById($ContentTypeID)
$Ctx.Load($ContentType)
$Ctx.ExecuteQuery()
If($ContentType -ne $Null)
{
# sharepoint online powershell create document set from content type
$DocumentSet = [Microsoft.SharePoint.Client.DocumentSet.DocumentSet]::Create($Ctx,$DocumentLibrary.RootFolder,$DocumentSetName,$ContentType.ID)
$Ctx.ExecuteQuery()
Write-host "Document Set '$DocumentSetName' Created Successfully!'" -f Green
}
else
{
Write-host "Content Type '$ContentTypeName' doesn't exist!'" -f Yellow
}
}
Catch {
write-host -f Red "Error:" $_.Exception.Message
}
此脚本在 SharePoint Online 中创建一个新文档集。
PnP PowerShell 在 SharePoint Online 中创建文档集
以下是如何使用 PnP PowerShell 在 SharePoint Online 中创建文档集:
#Parameters
$SiteURL= "https://Crescent.sharepoint.com/sites/Docs"
$ListName = "Documents"
$ContentTypeName = "Invoice Doc Set"
$DocumentSetName = "Crescent Invoices"
#Connect to the Site
Connect-PnPOnline -Url $SiteURL -Interactive
#Create a Content Type from "Document Set" Parent content type
$ParentCType = Get-PnPContentType -Identity "Document Set"
$ContentType = Add-PnPContentType -Name $ContentTypeName -Group "Crescent" -ParentContentType $ParentCType
#Add the Content Type to Library
Add-PnPContentTypeToList -List $ListName -ContentType $ContentType
#Add Document Set to Library
Add-PnPDocumentSet -List $ListName -ContentType $ContentTypeName -Name $DocumentSetName
总之,与手动创建相比,使用 PowerShell 在 SharePoint Online 中创建文档集是一种实现流程自动化并节省时间的强大方法,尤其是当您必须快速大量配置它们时。通过使用 CSOM 方法和 PnP PowerShell cmdlet 并按照本指南中概述的步骤进行操作,您可以快速轻松地创建文档集并配置它们以满足您的特定要求。
