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

[玩转系统] SharePoint Online:使用 PowerShell 添加索引列

作者:精品下载站 日期:2024-12-14 14:39:34 浏览:15 分类:玩电脑

SharePoint Online:使用 PowerShell 添加索引列


要求:在 SharePoint Online 中创建索引列。

如何在 SharePoint Online 中添加索引列?

当您频繁筛选和查询列时,索引列会在大型列表中增加性能优势。 SharePoint Online 中的索引列是一种特殊类型的列,用于存储可用作搜索键的值。索引列使用户能够使用搜索和过滤功能快速查找和检索信息。

要在 SharePoint Online 中创建索引列,请执行以下步骤:

  1. 导航到列表或库 >> 单击设置 >> 选择列表设置。
  2. 向下滚动到“列”部分 >> 单击“索引列”。

    [玩转系统] SharePoint Online:使用 PowerShell 添加索引列

  3. 在“索引列”页面上,单击“创建新索引”。

    [玩转系统] SharePoint Online:使用 PowerShell 添加索引列

  4. 从主列部分的下拉列表中选择一列。单击“创建”。这会在列表上创建一个索引。您可以在任何 SharePoint 列表或库中创建最多 20 个索引列。

    [玩转系统] SharePoint Online:使用 PowerShell 添加索引列

请注意,并非所有列类型都可以建立索引例如多行文本、多值选择、查找、超链接或图片、计算列、自定义列、多值个人或组,以及外部数据。

当您的列表接近 5,000 个项目时,SharePoint 中的自动索引功能会自动创建适当的索引!

SharePoint Online:使用 PowerShell 创建索引列

下面是 PowerShell 创建索引列,以提高列表或库上的搜索查询的性能。


#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"
   
#Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/"
$ListName="Projects"
$ColumnName="Project Name" #Display name of the field

Try { 
    #Get Credentials to connect
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Set up the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Ctx.Credentials = $credentials
   
    #Get the List and column to add index
    $List = $Ctx.web.Lists.GetByTitle($ListName)
    $Field = $List.Fields.GetByTitle($ColumnName)
    $Field.Indexed = $True
    $Field.Update()
    $Ctx.ExecuteQuery()

    Write-Host -f Green "Column Index Added Successfully!"
}
Catch {
    write-host -f Red "Error Adding Indexed to Column!" $_.Exception.Message
}

执行此 PowerShell 脚本后,您应该在索引列中看到“项目名称”字段。

使用 PowerShell 从 SharePoint Online 列表中删除索引列

要从列中删除索引,请将“Indexed”属性设置为“False”。


$Field.Indexed = $False

PnP PowerShell 在 SharePoint Online 中创建索引列

让我们看看如何使用 PowerShell 脚本在 SharePoint Online 列表或库中创建索引列。


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName = "Projects"
$ColumnName = "ProjectName" #Internal Name

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

#Get the Context
$Context = Get-PnPContext

#Get the Field from List
$Field = Get-PnPField -List $ListName -Identity $ColumnName

#Set the Indexed Property of the Field
$Field.Indexed = $True
$Field.Update() 
$Context.ExecuteQuery() 

在 SharePoint Online 中创建索引列是一个简单明了的过程,可以极大地提高信息管理的效率。通过使用索引列,用户可以快速轻松地搜索和过滤数据。

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

取消回复欢迎 发表评论:

关灯