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

[玩转系统] 使用 PowerShell 将单行文本/多行文本字段添加到 SharePoint 列表

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

使用 PowerShell 将单行文本/多行文本字段添加到 SharePoint 列表


如何在SharePoint中添加单行文本或多行文本列?

  • 浏览到 SharePoint 中的列表或库 >> 单击功能区中的“列表”或“库”选项卡。
  • 单击“管理视图”组下的“创建列”按钮。
  • 在“创建列”页面中,键入列的名称。
  • 根据您的要求选择列的类型为“单行文本”或“多行文本”。

    [玩转系统] 使用 PowerShell 将单行文本/多行文本字段添加到 SharePoint 列表

  • 在“附加列设置”部分中,提供描述、必填字段、唯一性、验证等字段的值。单击“确定”进行保存。

如何使用 PowerShell 以编程方式将单行文本字段或多行文本字段添加到 SharePoint 列表:


#configuration parameters
$WebURL="https://portal.crescent.com/"
$ListName="Service Requests"

#Get site and List objects
$web = Get-SPWeb $WebURL
$List = $web.Lists.TryGetList($ListName)

#Check if List Exists
if($List)
{
    #Setup Field Properties
    $FieldName = "Overall Progress"
    $FieldType = [Microsoft.SharePoint.SPFieldType]::Text
    #For Multiple Lines of Text field, use: $FieldType = microsoft.sharepoint.SPFieldType]::Note
    $IsRequired = $False

    #Add the Field
    $NewField=$List.Fields.Add($FieldName,$FieldType,$IsRequired)
}
else
{
    write-host "List not found!"
}

用于将单行文本添加到 SharePoint 列表的 PowerShell 脚本:

让我们把它变成一个可重用的函数。


#Function to Add a Single Line of Text
Function Add-SPSingleLineOfTextField([Microsoft.SharePoint.SPList]$List, [string]$DisplayName, [string]$Name, [string]$Required)
{
    if(!$List.Fields.ContainsField($DisplayName))
    {
        $FieldXML = "<Field Type='Text' DisplayName='"+ $DisplayName +"' Required='"+ $Required +"' MaxLength='255' Name='"+ $Name +"' />" 
        $NewField=$List.Fields.AddFieldAsXml($FieldXML,$True,[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)
        write-host "New Field Added!" -f Green
    }
    else
    {
        write-host "Field exists already!" -f RED
    }        
}

PowerShell 将多行文本字段添加到 SharePoint 列表


#Function to Add Multiple Lines of Text
Function Add-SPMultipleLinesOfTextField([Microsoft.SharePoint.SPList]$List, [string]$DisplayName, [string]$Name, [string]$Required, [string]$NumLines, [string]$RichText, [string]$RichTextMode)
{
    if(!$List.Fields.ContainsField($DisplayName))
    {
        #Frame Field XML
        $FieldXML = "<Field Type='Note' DisplayName='"+ $DisplayName +"' Name='"+ $Name +"' Required='"+ $Required +"' NumLines='"+ $NumLines +"' RichText='" + $RichText +"' RichTextMode='"+ $RichTextMode +"' Sortable='FALSE'/>"
        #Add Field
        $NewField=$List.Fields.AddFieldAsXml($FieldXML,$True,[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)
        write-host "New Multiple Linex of Text Field Added!" -f Green
    }
    else
    {
        write-host "Field exists already!" -f RED
    }        
}

现在,我们可以调用该函数来添加一行文本:


#configuration parameters
$WebURL="https://portal.crescent.com/"
$ListName="Service Requests"

#Get site and List objects
$web = Get-SPWeb $WebURL
$List = $web.Lists.TryGetList($ListName)

#Check if List Exists
if($List)
{
    #Call the function to add single line of Text
    Add-SPSingleLineOfTextField $List "Overall Progress" "Progress" $False
}
else
{
    write-host "List not found!"
}

让我们调用该函数来添加多行文本字段:


#configuration parameters
$WebURL="https://portal.crescent.com/"
$ListName="Service Requests"

#Get site and List objects
$web = Get-SPWeb $WebURL
$List = $web.Lists.TryGetList($ListName)

#Check if List Exists
if($List)
{
    #Add Plain Text
    #Add-SPMultipleLinesOfTextField $List "Stage Comments" "comments" $False 6 $False "Compatible"
    
    #Add Rich Text
    Add-SPMultipleLinesOfTextField $List "Stage Comments" "comments" "FALSE" 6 "TRUE" "Compatible"
    
    #Add Enhanced Rich Text
    #Add-SPMultipleLinesOfTextField $List "Stage Comments" "comments" "FALSE" "6" "TRUE" "FullHtml"
}
else
{
    write-host "List not found!"
}

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

取消回复欢迎 发表评论:

关灯