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

[玩转系统] SharePoint Online:使用 PowerShell 更改列表字段设置

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

SharePoint Online:使用 PowerShell 更改列表字段设置


要求:使用 PowerShell 更新 SharePoint Online 列表字段设置。

如何在 SharePoint Online 中编辑列设置?

列表字段的属性(例如显示名称、类型或其他相关列设置)可以通过列表设置页面进行更新。要更改列表或库中的列设置,请执行以下步骤:

  1. 导航到要编辑列的 SharePoint Online 列表或库。
  2. 从功能区或“站点设置”菜单转到列表或库设置页面 >> 在“列”部分下,选择要更新的列。
  3. 在“编辑列”页面上,您可以进行任何必要的更改,然后单击“确定”按钮进行保存。

    [玩转系统] SharePoint Online:使用 PowerShell 更改列表字段设置

请注意,更改列类型仅限于基于当前列类型的特定列。此外,在列类型之间切换时可能会丢失数据。例如,当您从多行文本切换到单行文本时,它的字符数限制为 255 个。

PowerShell 更改 SharePoint Online 列表中的字段设置

我们还可以使用 PowerShell 更改 SharePoint Online 中列表的字段设置。如果您需要更改字段的属性(例如显示名称、必需标志、默认值等),这会很有用。


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

#Set Config Parameters
$SiteURL="https://crescent.sharepoint.com/sites/projects"
$ListName="Projects"
$FieldName="HeadCount" #Internal Name

#Get 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)

#Get the List Field
$Field=$List.Fields.GetByInternalNameOrTitle($FieldInternalName)
$Ctx.Load($Field)
$Ctx.ExecuteQuery()

#Set List Field Settings
$Field.Title= "Total Resources"
$Field.Description = "Number of Resources working on the Project"

#set default value
$Field.DefaultValue = "10"

$Field.EnforceUniqueValues = $False

#set field hidden
$Field.Hidden = $False

#set field required
$Field.Required = $True
$Field.FieldTypeKind = "Text"
$Field.Indexed = $False

#set field read only
#$Field.ReadOnlyField = $True

#Apply changes
$Field.Update()
$Ctx.ExecuteQuery()

Write-host "Field Settings Updated!"

PnP PowerShell 更新 SharePoint Online 列表中的字段属性

以下是如何使用 PnP PowerShell 自动更改 SharePoint Online 列表的字段设置:


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName = "Team Projects"
$FieldName = "ProjectStatus" #Internal Name

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred
    
    #Set Field Properties
    Set-PnPField -List $ListName -Identity $FieldName -Values @{Required=$True;DefaultValue="Active"}    
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

同样,您可以将列表字段的任何其他属性(例如“标题”或“描述”)设置为:


-Values @{Title="Name of the Column";Description="Updated List Description"}  

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

取消回复欢迎 发表评论:

关灯