Tìm kiếm trang web

SharePoint Online: Tạo tập tài liệu bằng PowerShell


Yêu cầu: Tạo Tập tài liệu trong SharePoint Online bằng PowerShell.

SharePoint Online: Tạo tập tài liệu bằng PowerShell

Trong khi bài đăng khác của tôi giải thích cách tạo bộ tài liệu trong SharePoint Online: Tạo bộ tài liệu trong SharePoint Online - Từng bước, bài đăng blog này sẽ chỉ cho bạn cách sử dụng PowerShell để tạo bộ tài liệu trong SharePoint Online.

Nói tóm lại, Bộ tài liệu là một thùng chứa tổ chức chứa một hoặc nhiều tài liệu liên quan. Nó giống như tạo các thư mục trên màn hình của bạn cho các dự án và thư mục con khác nhau trong các thư mục đó cho các tác vụ riêng lẻ. Tập lệnh này giả định rằng tính năng bộ tài liệu đã được bật, Loại nội dung cho bộ tài liệu đã được tạo và thêm vào thư viện. Đây là tập lệnh PowerShell để tạo bộ tài liệu trong SharePoint Online:


#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
}

Tập lệnh này tạo một bộ tài liệu mới trong SharePoint Online.

PnP PowerShell để tạo tập tài liệu trong SharePoint Online

Dưới đây là cách tạo bộ tài liệu trong SharePoint Online bằng PnP PowerShell:


#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

Tóm lại, tạo Tập tài liệu trong SharePoint Online bằng PowerShell là một cách mạnh mẽ để tự động hóa quy trình và tiết kiệm thời gian so với tạo thủ công, đặc biệt khi bạn phải cung cấp chúng nhanh chóng với số lượng lớn. Bằng cách sử dụng các phương pháp CSOM và lệnh ghép ngắn PnP PowerShell, đồng thời làm theo các bước được nêu trong hướng dẫn này, bạn có thể nhanh chóng và dễ dàng tạo Tập tài liệu và đặt cấu hình chúng để đáp ứng các yêu cầu cụ thể của mình.

Bài viết liên quan