当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:如何使用 PowerShell 在列表中启用评级?

作者:精品下载站 日期:2024-12-14 21:10:24 浏览:16 分类:玩电脑

SharePoint Online:如何使用 PowerShell 在列表中启用评级?


要求: 在 SharePoint Online 列表中启用评级。

如何在 SharePoint Online 中启用评级?

SharePoint Online 中的评级功能有助于突出显示文档和列表项。可以在 SharePoint Online 列表和库中启用评级,以便用户查看项目并对其进行评级。评级可以配置为星级评级或喜欢。

  1. 星级
    当为星级评级配置列表时,每次有人对项目或文档进行评级时,等级都会显示所有对其进行评级的人的平均评级。要对列表中的项目进行评分,请单击代表您要给予该项目的评分的星号。选择评级后,该项目将更新以包含您的评级。如果您再次对项目进行评分,则先前应用的评分将替换为新选择的评分。
  2. 点赞评级
    点赞与您在 Facebook、Twitter 和 LinkedIn 等上使用的类似。当评级类型设置为“点赞”时,项目会显示“点赞”按钮(小心形)图标),其中包括某个项目收到的点赞数。如果您还没有喜欢某个项目,您可以喜欢该项目;如果您已经喜欢该项目,您可以不喜欢该项目。您可以通过单击“喜欢”图标来记录您的喜欢/不喜欢。

要启用评级,我们必须在列表设置下激活它。要激活它们,请执行以下操作:

  1. 导航到要激活评级的 SharePoint Online 列表或库。>> 单击“列表设置”
  2. 在列表设置页面上,单击常规设置部分下的“评级设置”链接。
  3. 在“评级设置”上,将“允许对此列表中的项目进行评级”选项设置为“是”
  4. 对于“您希望为此列表启用哪种投票/评级体验”选项,请选择“星级”或“赞”。

    [玩转系统] SharePoint Online:如何使用 PowerShell 在列表中启用评级?

  5. 单击“确定”按钮保存更改。

将启用列表的评级。如果您在列表设置下没有看到评级设置,则必须激活评级功能!如何激活SharePoint Online中的评级功能?

[玩转系统] SharePoint Online:如何使用 PowerShell 在列表中启用评级?

PowerShell 在 SharePoint Online 中启用评级设置:

在 SharePoint Online 列表或库中配置评级设置有点棘手,因为没有直接的方法来设置评级。我们需要执行以下步骤:

  1. 将评级网站栏添加到列表中
  2. 将评级字段添加到默认视图
  3. 设置列表根文件夹的属性包值。

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Function to Add Fields to List 
Function Add-SPOSiteColumnToList([Microsoft.SharePoint.Client.List]$List, [GUID]$FieldID)
{
    #Get Fields from List
    $Ctx.Load($List.Fields)
    $Ctx.ExecuteQuery()
    
    #Get the Site Column from Web
    $SiteColumn = $List.ParentWeb.AvailableFields.GetById($FieldID)
    $Ctx.Load($SiteColumn)
    $Ctx.ExecuteQuery()

    #Check if the Field exist in list
    $ListField = $List.Fields | where {$_.ID -eq $SiteColumn.Id}
    if($ListField -eq $NULL)
    {
        #Add the site column to the list
        $NewColumn = $List.Fields.Add($SiteColumn)
        $ctx.ExecuteQuery()
        Write-host "Site Column '$($SiteColumn.Title)' Added to the List Successfully!" -f Green
    }
}

Function Set-SPOListRatingSettings([string]$SiteURL, [string]$ListName, [string]$RatingsType)
{
    #Setup Credentials to connect
    $Cred = Get-Credential

    #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.ExecuteQuery()

    #Rating Site Columns
    $AverageRatingFieldID = [guid]"5a14d1ab-1513-48c7-97b3-657a5ba6c742"
    $RatingCountFieldID = [guid]"b1996002-9167-45e5-a4df-b2c41c6723c7"
    $RatedByFieldID = [guid]"4D64B067-08C3-43DC-A87B-8B8E01673313"
    $RatingsFieldID = [guid]"434F51FB-FFD2-4A0E-A03B-CA3131AC67BA"
    $LikesCountFieldID = [guid]"6E4D832B-F610-41a8-B3E0-239608EFDA41"
    $LikedByFieldID = [guid]"2CDCD5EB-846D-4f4d-9AAF-73E8E73C7312"

    #Call the function to Add Rating Site columns to the List
    Add-SPOSiteColumnToList -List $List -FieldID $AverageRatingFieldID
    Add-SPOSiteColumnToList -List $List -FieldID $RatingCountFieldID
    Add-SPOSiteColumnToList -List $List -FieldID $RatedByFieldID
    Add-SPOSiteColumnToList -List $List -FieldID $RatingsFieldID
    Add-SPOSiteColumnToList -List $List -FieldID $LikesCountFieldID
    Add-SPOSiteColumnToList -List $List -FieldID $LikedByFieldID

    #Update Field to Default view
    $ListDefaltView = $List.DefaultView
    $ViewFields = $ListDefaltView.ViewFields
    $Ctx.Load($ListDefaltView)
    $Ctx.Load($ViewFields)
    $Ctx.ExecuteQuery()

    If($RatingsType -eq "Ratings")
    {
        #Add Ratings Fields to default view
        If($ViewFields -notcontains "AverageRating")
        {        
            $ListDefaltView.ViewFields.Add("AverageRating")
            $ListDefaltView.Update()
            $Ctx.ExecuteQuery()
            Write-host -f Green "Ratings Field Added to the Default View!"
        }
        #Remove Likes Field from Default View
        If($ViewFields -contains "LikesCount")
        {        
            $ListDefaltView.ViewFields.Remove("LikesCount")
            $ListDefaltView.Update()
            $Ctx.ExecuteQuery()
            Write-host -f Green "Likes Field Removed from the Default View!"
        }
    }
    ElseIf ($RatingsType -eq "Likes")
    {
        #Add Likes Fields to default view
        If($ViewFields -notcontains "LikesCount")
        {        
            $ListDefaltView.ViewFields.Add("LikesCount")
            $ListDefaltView.Update()
            $Ctx.ExecuteQuery()
            Write-host -f Green "Likes Field Added to the Default View!"
        }
        #Remove Ratings Field from Default View
        If($ViewFields -contains "AverageRating")
        {        
            $ListDefaltView.ViewFields.Remove("AverageRating")
            $ListDefaltView.Update()
            $Ctx.ExecuteQuery()
            Write-host -f Green "Rating Field Removed from the Default View!"
        }
    }
    Else #Disable Rating!
    {
        #Remove Ratings Field from Default View
        If($ViewFields -contains "AverageRating")
        {        
            $ListDefaltView.ViewFields.Remove("AverageRating")
            $ListDefaltView.Update()
            $Ctx.ExecuteQuery()
            Write-host -f Green "Rating Field Removed from the Default View!"
        }
        #Remove Likes Field from Default View
        If($ViewFields -contains "LikesCount")
        {        
            $ListDefaltView.ViewFields.Remove("LikesCount")
            $ListDefaltView.Update()
            $Ctx.ExecuteQuery()
            Write-host -f Green "Likes Field Removed from the Default View!"
        }
    }

    #Set Rating Setting for the List
    $RootFolder = $List.RootFolder
    $RootFolderProperties = $RootFolder.Properties
    $Ctx.Load($RootFolder)
    $Ctx.Load($RootFolderProperties)
    $RootFolderProperties["Ratings_VotingExperience"] = $RatingsType
    $RootFolder.Update()
    $Ctx.ExecuteQuery()
    Write-host -f Green "Rating Settings Updated for the List!"
}
#Set Variables
$SiteURL= "https://crecent.sharepoint.com/sites/Marketing"
$ListName="Team Documents"
$RatingsType = "Ratings" #Ratings or Likes or "" (To Disable!)

#Call the function to set rating settings in list
Set-SPOListRatingSettings -SiteURL $SiteURL -ListName $ListName -RatingsType $RatingsType 

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯