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

[玩转系统] SharePoint Online:使用 PowerShell 将数字列添加到列表

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

SharePoint Online:使用 PowerShell 将数字列添加到列表


要求:在 SharePoint Online 列表中创建数字列。

如何将数字列添加到 SharePoint Online 列表?

添加列是扩展 SharePoint Online 列表功能的快速而简单的方法。 SharePoint Online 中的数字列用于数字输入。此外,该字段可以配置为将值呈现为十进制数并设置最小-最大允许值。

  1. 浏览您的 SharePoint Online 网站并导航到要在其中添加数字列的目标列表。
  2. 在“列表”选项卡下,单击功能区中的“创建列”按钮。
  3. 为新列提供名称,并将类型指定为“数字”

    [玩转系统] SharePoint Online:使用 PowerShell 将数字列添加到列表

  4. 向下滚动并填写其他可选值,例如必需列、唯一值、最小值和最大值、小数位数、默认值等,然后单击“确定”以在 SharePoint Online 列表中创建数字列。

PowerShell 创建要在 SharePoint Online 中列出的数字列

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

#Custom function to add column to list
Function Add-NumberColumnToList()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $Name,
        [Parameter(Mandatory=$true)] [string] $DisplayName,
        [Parameter(Mandatory=$false)] [string] $Description=[string]::Empty,
        [Parameter(Mandatory=$false)] [string] $IsRequired = "FALSE",
        [Parameter(Mandatory=$false)] [string] $EnforceUniqueValues = "FALSE"
    )

    #Generate new GUID for Field ID
    $FieldID = New-Guid

    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 List
        $List = $Ctx.Web.Lists.GetByTitle($ListName)
        $Ctx.Load($List)
        $Ctx.ExecuteQuery()

        #Check if the column exists in list already
        $Fields = $List.Fields
        $Ctx.Load($Fields)
        $Ctx.executeQuery()
        $NewField = $Fields | where { ($_.Internalname -eq $Name) -or ($_.Title -eq $DisplayName) }
        if($NewField -ne $NULL)  
        {
            Write-host "Column $Name already exists in the List!" -f Yellow
        }
        else
        {
            $FieldSchema = "<Field Type='Number' ID='{$FieldID}' DisplayName='$DisplayName' Name='$Name' Description='$Description' Required='$IsRequired' EnforceUniqueValues='$EnforceUniqueValues' />"
            $NewField = $List.Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
            $Ctx.ExecuteQuery()    

            Write-host "New Column Added to the List Successfully!" -ForegroundColor Green  
        }
    }
    Catch {
        write-host -f Red "Error Adding Column to List!" $_.Exception.Message
    }
} 

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$Name="HeadCount"
$DisplayName="Head Count"
$Description="Enter the Number of Team Members in the Project"

#Call the function to add column to list
Add-NumberColumnToList -SiteURL $SiteURL -ListName $ListName -Name $Name -DisplayName $DisplayName -Description $Description 

要添加其他参数(例如最小值、最大值、百分比和小数),请将以下内容附加到架构 XML:


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

#Custom function to add column to list
Function Add-NumberColumnToList()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $Name,
        [Parameter(Mandatory=$true)] [string] $DisplayName,
        [Parameter(Mandatory=$false)] [string] $Description=[string]::Empty,
        [Parameter(Mandatory=$false)] [string] $IsRequired = "FALSE",
        [Parameter(Mandatory=$false)] [string] $EnforceUniqueValues = "FALSE",
        [Parameter(Mandatory=$true)] [string] $Min,
        [Parameter(Mandatory=$true)] [string] $Max,
        [Parameter(Mandatory=$true)] [string] $Percentage,
        [Parameter(Mandatory=$true)] [string] $Decimals
    )

    #Generate new GUID for Field ID
    $FieldID = New-Guid

    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 List
        $List = $Ctx.Web.Lists.GetByTitle($ListName)
        $Ctx.Load($List)
        $Ctx.ExecuteQuery()

        #Check if the column exists in list already
        $Fields = $List.Fields
        $Ctx.Load($Fields)
        $Ctx.executeQuery()
        $NewField = $Fields | where { ($_.Internalname -eq $Name) -or ($_.Title -eq $DisplayName) }
        if($NewField -ne $NULL)  
        {
            Write-host "Column $Name already exists in the List!" -f Yellow
        }
        else
        {
            $FieldSchema = "<Field Type='Number' ID='{$FieldID}' DisplayName='$DisplayName' Name='$Name' Description='$Description' Required='$IsRequired' EnforceUniqueValues='$EnforceUniqueValues' Min='$Min' Max='$Max' Percentage='$Percentage' Decimals='$Decimals' />"
            $NewField = $List.Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
            $Ctx.ExecuteQuery()    

            Write-host "New Column Added to the List Successfully!" -ForegroundColor Green  
        }
    }
    Catch {
        write-host -f Red "Error Adding Column to List!" $_.Exception.Message
    }
} 

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$Name="Probability"
$DisplayName="Probability"
$Description="Enter the Probability Percentage"
$Min="0"
$Max="1" #100%
$Percentage="TRUE"
$Decimals="2"

#Call the function to add column to list
Add-NumberColumnToList -SiteURL $SiteURL -ListName $ListName -Name $Name -DisplayName $DisplayName -Description $Description -Min $Min -Max $Max -Percentage $Percentage -Decimals $Decimals

PnP PowerShell 将数字列添加到 SharePoint Online 列表

也可以使用 PnP PowerShell 将列添加到 SharePoint Online 列表。


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$ListName= "Projects"

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

#Add Number Field to list
Add-PnPField -Type Number -List $ListName -DisplayName "Head Count" -InternalName "HeadCount"

这将在您的列表中添加一个名为“Head Count”的新列。您还可以指定其他选项,例如描述、必需、默认值等。同样,我们也可以从 XML 模式创建数字列。


#Define XML Schema for Number Field
$FieldXML= "<Field Type='Number' Name='HeadCount' ID='$([GUID]::NewGuid())' DisplayName='Head Count' Required ='FALSE' Min='1' Max='50' ></Field>"

#Add number Field to list from XML
Add-PnPFieldFromXml -FieldXml $FieldXML -List $ListName

设置 Percentage='TRUE' 属性将字段标记为百分比!

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

取消回复欢迎 发表评论:

关灯