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

[玩转系统] SharePoint Online:使用 PowerShell 设置列表列的默认值

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

SharePoint Online:使用 PowerShell 设置列表列的默认值


要求: SharePoint Online 设置列默认值设置。

如何在SharePoint Online中设置列默认值设置?

创建 SharePoint 列表时,您可以指定每列中存储的数据类型。但是,如果您想为列设置默认值怎么办?也许您希望确保每当添加新项目时都会在列中自动填充特定值。本文将向您展示如何为 SharePoint Online 列表中的列设置默认值。

要预设 SharePoint Online 列表中字段的默认值,

  1. 转到列表设置 >> 在“列”下,通过单击其标题来选择字段。
  2. 在“默认值”部分下,您可以指定字段的默认值,以便无需用户输入即可自动填充。

    [玩转系统] SharePoint Online:使用 PowerShell 设置列表列的默认值

您还可以在创建列时指定默认值!

使用 PowerShell CSOM 配置默认列值

以下是如何使用 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"

#Function to set default value of a field
Function Set-SPOFieldDefaultValue($SiteURL, $ListName, $FieldInternalName, $DefaultValue)
{
    Try { 
        #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 Field
        $Field = $List.Fields.GetByInternalNameOrTitle($FieldInternalName)
        $Field.DefaultValue = $DefaultValue
        $Field.Update()
        $Ctx.ExecuteQuery()
 
        Write-host -f Green "Default Value set for Field Successfully!"
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}
#Set parameter values
$SiteURL = "https://Crescent.sharepoint.com"
$ListName = "Team Documents"
$FieldInternalName = "Classification" #Internal Name
$DefaultValue = "Invoice; Proposal; Quotation"

Set-SPOFieldDefaultValue -SiteURL $SiteURL -ListName $ListName -FieldInternalName $FieldInternalName -DefaultValue $DefaultValue

要设置 SharePoint Online 列表列的默认值,您可以使用 PowerShell 更新字段的“DefaultValue”属性。同样,您可以将日期列的默认值设置为今天的日期,如下所示:


Set-SPOFieldDefaultValue -SiteURL "https://Crescent.sharepoint.com" -ListName "Tasks" -FieldInternalName "DueDate" -DefaultValue "[Today]"

SharePoint Online:使用 PnP PowerShell 的列默认值设置

设置 SharePoint Online 列表列默认值的 PnP PowerShell 方法:


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName = "Team Documents"
$ColumnName = "Classification" #Internal Name
$DefaultValue = "Invoice; Proposal; Quotation"

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

#Set Default Value for the field
Set-PnPField -Identity $ColumnName -List $ListName -Values @{DefaultValue=$DefaultValue}

结果:

[玩转系统] SharePoint Online:使用 PowerShell 设置列表列的默认值

SharePoint Online:设置查找字段的默认值

要设置 SharePoint Online 列表中查找列的默认值,请使用以下命令:


#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/PMO"
$ListName = "Projects"
$FieldName = "ParentProject" #Internal Name

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Set Default Value for the Person column
$DefaultValue = "1;#OS Upgrade" #ID and Text of the Parent Item in the given format

Set-PnPField -Identity $FieldName -List $ListName -Values @{DefaultValue=$DefaultValue}

您还可以使用 Set-PnPDefaultColumnValues cmdlet 设置 SharePoint Online 文档库中列的默认值。


Set-PnPDefaultColumnValues -List Documents -Field TaxKeyword -Value "Crescent|Projects|InfraBuild"

SharePoint Online:设置个人或组列的默认值

以下是如何设置文档库中“人员”列的默认值(但这不适用于列表!):


#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Retail"
$ListName = "Documents"
$FieldName = "ProjectManager" #Internal Name
$DefaultUserID = "i:0#.f|membership|[email protected]"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Get the User
$User = Get-PnPUser -Identity $DefaultUserID

#Set Default Value for the Person column
Set-PnPDefaultColumnValues -List $ListName -Field $FieldName -Value "$($User.Id);#$($User.LoginName)"

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

取消回复欢迎 发表评论:

关灯