Tìm kiếm trang web

SharePoint Online: Nhận giá trị túi thuộc tính bằng PowerShell


Túi thuộc tính trong SharePoint Online là cách nhanh chóng và thuận tiện để lưu trữ và truy xuất cấu hình tùy chỉnh thay vì lưu trữ chúng ở nơi khác. Nó lưu trữ dữ liệu cấu hình ở định dạng Bảng băm (Khóa-Giá trị). SharePoint cho phép lưu trữ cài đặt cấu hình ở các cấp độ khác nhau, chẳng hạn như tuyển tập trang, trang, danh sách và các mục danh sách. Đây là bộ sưu tập tập lệnh PowerShell của tôi để hoạt động với túi Thuộc tính trong SharePoint Online:

PowerShell để nhận khóa-giá trị từ túi thuộc tính SharePoint Online

Để kiểm tra khóa túi thuộc tính, hãy sử dụng PowerShell này 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"
 
#Function to Get a particular key value in Property Bag
Function Get-SPOPropertyBagKey($SiteURL, $Key)
{
    #Setup Credentials to connect
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
     
    #Get All Properties of the web
    $Web = $Ctx.Web
    $Ctx.Load($Web)
    $Ctx.Load($Web.AllProperties)
    $Ctx.ExecuteQuery()
 
    #Get if the Key exists in Property bag
    If ($Web.AllProperties.FieldValues.ContainsKey($Key) -eq $True)
    {
        #Update the Key value in Property bag
        $Value = $Web.AllProperties[$Key]
 
        Write-Host -f Green "Value of the Property Bag Key '$Key':" $Value
    }
    Else
    {
        Write-Host -f Yellow "Could not find Property Bag Key:" $Key
    }
}
 
#Variables
$SiteURL="https://Crescent.sharepoint.com/"
$Key ="NoCrawl"
 
#Call the function to Get the Property bag Key value
Get-SPOPropertyBagKey -SiteURL $SiteURL -Key $Key

Nhận tất cả giá trị túi thuộc tính của trang web SharePoint Online bằng PowerShell:

Để kiểm tra tất cả các khóa túi thuộc tính trong SharePoint Online, hãy sử dụng tập lệnh PowerShell này:


#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"
  
#Variables
$SiteURL="https://Crescent.sharepoint.com"
 
Try {
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
 
    #Get the Web
    $Web = $Ctx.Web
    $Ctx.Load($Web)
    #Get Property bag of the web
    $SiteProperties = $Web.AllProperties
    $Ctx.Load($SiteProperties)
    $Ctx.ExecuteQuery()

    #$SiteProperties.FieldValues
    #Iterate through each Property
    $PropertyBagKeys = $SiteProperties.FieldValues.Keys
    Foreach($Key in $PropertyBagKeys)
    {
        Write-Host $Key : $SiteProperties[$Key]
    }
}
Catch {
    write-host -f Red "Error Getting Property Bag Values!" $_.Exception.Message
}

Cập nhật Giá trị Túi Thuộc tính bằng PowerShell trong SharePoint Online

Đây là SharePoint Online PowerShell để đặt giá trị túi thuộc tính:


#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"
 
#Function to Set Property Bag Field Value in SharePoint Online Site
Function Set-SPOPropertyBag($SiteURL, $Key, $Value)
{
    #Setup Credentials to connect
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
     
    #Get All Properties of the web
    $Web = $Ctx.Web
    $Ctx.Load($Web)
    $Ctx.Load($Web.AllProperties)
    $Ctx.ExecuteQuery()
 
    #Get if the Key exists in the Property bag
    If ($Web.AllProperties.FieldValues.ContainsKey($Key) -eq $True)
    {
        #Update the Key value in Property bag
        $Web.AllProperties[$Key] = $Value
        $web.Update()
        $Ctx.ExecuteQuery()
 
        Write-Host -f Green "Property Bag Key '$Key' Value Updated to: " $Value
    }
    Else
    {
        Write-Host -f Yellow "Could not find Property Bag Key:" $Key
    }
}
 
#variable
$SiteURL ="https://Crescent.sharepoint.com/"
$Key = "FollowLinkEnabled"
$Value="False"
 
#Call the function to Update Property bag Key value
Set-SPOPropertyBag -SiteURL $SiteURL -Key $Key -Value $Value
Xin lưu ý, nếu tập lệnh tùy chỉnh bị tắt, bạn không thể cập nhật giá trị túi thuộc tính! Bạn có thể nhận được thông báo “Quyền truy cập bị từ chối. Bạn không có quyền thực hiện hành động này hoặc truy cập tài nguyên này.” lỗi. Đã có bản sửa lỗi ở đây: Làm cách nào để bật Tập lệnh tùy chỉnh trong SharePoint Online?

Cuối cùng nhưng không kém phần quan trọng: Bạn có thể sử dụng SharePoint Designer để thao tác các mục trong túi thuộc tính! Chỉ cần mở trang web trong SharePoint Designer và nhấp vào nút “Tùy chọn trang web” trong dải băng để tải trình chỉnh sửa túi thuộc tính!

Đọc hoặc ghi giá trị túi thuộc tính bằng PnP PowerShell

Đây là cách chúng ta có thể đọc giá trị từ túi thuộc tính trong SharePoint Online bằng lệnh ghép ngắn Get-PnPPropertyBag.


#Variable
$SiteURL= "https://crescent.sharepoint.com/sites/marketing"

#Connect to PnP Online
Connect-PnPOnline $SiteURL -Credentials (Get-Credential)

#Get the Tenant Site Object
Get-PnPPropertyBag -Key "allowdesigner"

PnP PowerShell để cập nhật giá trị túi thuộc tính trong SharePoint Online

Đây là PnP PowerShell để đặt túi thuộc tính trong SharePoint Online:


Set-PnPPropertyBagValue -Key "allowdesigner" -Value "1"

Tóm lại, việc nhận hoặc thiết lập túi thuộc tính trong SharePoint Online bằng PowerShell thật dễ dàng với hướng dẫn này. Bằng cách làm theo các bước và cách thực hành tốt nhất được nêu trong bài viết này, bạn có thể dễ dàng truy xuất hoặc cập nhật giá trị túi thuộc tính và khai thác toàn bộ tiềm năng của SharePoint Online.

Bài viết liên quan