Tìm kiếm trang web

SharePoint Online: Thay đổi thứ tự loại nội dung trong menu mới bằng PowerShell


Yêu cầu: Thay đổi thứ tự loại nội dung trong Danh sách SharePoint Online bằng PowerShell.

Làm cách nào để thay đổi thứ tự loại nội dung trong danh sách hoặc thư viện SharePoint Online?

Trong danh sách SharePoint Online có nhiều loại nội dung, bạn có thể muốn thay đổi thứ tự chúng xuất hiện trong menu “Mới” hoặc thậm chí muốn ẩn chúng khỏi nút mới. Menu mới được sử dụng để tạo các mục mới, chẳng hạn như tài liệu hoặc mục danh sách, trong thư viện hoặc danh sách và thứ tự của các loại nội dung sẽ xác định loại nào được chọn làm mặc định. Bằng cách thay đổi thứ tự của các loại nội dung trong menu mới, bạn có thể đảm bảo rằng loại nội dung được sử dụng thường xuyên nhất có thể truy cập dễ dàng.

  1. Dẫn hướng đến danh sách hoặc thư viện SharePoint Online nơi các kiểu nội dung cần được quản lý.
  2. Đi tới Cài đặt Danh sách hoặc Thư viện >> Trên trang Cài đặt, nhấp vào liên kết “Thay đổi thứ tự nút mới và loại nội dung mặc định” trong phần Loại nội dung.
  3. Trên trang Thay đổi thứ tự nút mới, Bạn có thể sắp xếp lại các loại nội dung có sẵn và bật hoặc tắt chúng xuất hiện trong menu mới bằng cách đặt hộp kiểm hiển thị. Loại nội dung xuất hiện ở vị trí đầu tiên sẽ trở thành loại nội dung mặc định cho danh sách.

  4. Nhấp vào nút OK để lưu các thay đổi.

PowerShell để thay đổi thứ tự loại nội dung trong SharePoint Online

Đây là tập lệnh PowerShell để thay đổi thứ tự của các loại nội dung trong danh sách hoặc thư viện 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"
    
#Set Variables
$SiteURL= "https://crescent.sharepoint.com/sites/Marketing"
$ListName = "Contacts"
$ContentTypesOrder = @("Contact", "Item", "Announcement")

#Setup 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 List
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    $Ctx.Load($List)
    $Ctx.Load($List.ContentTypes)
    $Ctx.ExecuteQuery()

    #Create a List for content types order
    $ContentTypeOrder = New-Object System.Collections.Generic.List[Microsoft.SharePoint.Client.ContentTypeId]

    #Arrange Content Types Order
    ForEach($ContentType in $ContentTypesOrder)
    {
        #Get the Content Type from List and Add to Content Type Order List
        $CType = $List.ContentTypes | Where {$_.Name -eq $ContentType}
        If($CType -ne $Null)
        {
            $ContentTypeOrder.Add($CType.Id)
        }
    }
    #Set Content type Order
    $List.RootFolder.UniqueContentTypeOrder = $ContentTypeOrder
    $List.RootFolder.Update()
    $Ctx.ExecuteQuery()
    Write-host -f Green "Content Type Order has been updated Successfully!"#>
}
Catch {
    write-host -f Red "Error: " $_.Exception.Message
}

Xin lưu ý, tập lệnh này không xóa hoặc thêm lại bất kỳ loại nội dung nào khỏi danh sách. Nó chỉ đơn giản là thay đổi thứ tự của chuỗi loại nội dung và loại nội dung trong chỉ mục đầu tiên sẽ trở thành loại nội dung mặc định.

Để ẩn một loại nội dung khỏi menu Mới, chỉ cần để lại tên của nó trong biến $ContentTypesOrder!

PnP PowerShell để đặt thứ tự loại nội dung trong menu mới

Đây là phiên bản PnP PowerShell của tập lệnh!


#Set Variables
$SiteURL= "https://crescent.SharePoint.com/sites/Retail"
$ListName = "Contacts"
$ContentTypesOrder = @("Contact", "Item", "Announcement")

#Connect to the site
Connect-PnPOnline -Url $SiteURL -Interactive

#Get the List
$List = Get-PnPList -Identity $ListName -Includes ContentTypes,RootFolder.UniqueContentTypeOrder

#Create a List for content types order
$ContentTypeOrder = New-Object System.Collections.Generic.List[Microsoft.SharePoint.Client.ContentTypeId]

#Arrange Content Types Order
ForEach($ContentType in $ContentTypesOrder)
{
    #Get the Content Type from List and Add to Content Type Order List
    $CType = $List.ContentTypes | Where {$_.Name -eq $ContentType}
    If($CType -ne $Null)
    {
        $ContentTypeOrder.Add($CType.Id)
    }
}

#Set Content type Order
$List.RootFolder.UniqueContentTypeOrder = $ContentTypeOrder
$List.RootFolder.Update()
Invoke-PnPQuery
Write-host -f Green "Content Type Order has been updated Successfully!"

Tóm lại, việc đặt thứ tự loại nội dung trong menu mới của thư viện hoặc danh sách trong SharePoint Online là một quy trình đơn giản có thể giúp nâng cao hiệu quả của quy trình làm việc. Bằng cách thực hiện các thay đổi đối với thứ tự, các nhóm có thể đảm bảo rằng các loại nội dung được sử dụng thường xuyên nhất đều có thể truy cập dễ dàng, giảm thời gian và công sức cần thiết để tạo các mục mới.

Bài viết liên quan