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

[玩转系统] SharePoint Online:使用 PowerShell 将超链接或图片列添加到列表

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

SharePoint Online:使用 PowerShell 将超链接或图片列添加到列表


要求: 将超链接列添加到 SharePoint Online 列表。

如何使用 PowerShell 在列表中创建超链接或图片列?

SharePoint Online 中的“超链接”或“图片”列用于存储网址或图片的 URL。字段类型可以配置为保存 URL 或图片。以下是将超链接字段添加到 SharePoint Online 列表的步骤:

  1. 浏览到 SharePoint Online 网站并导航到要在其中添加超链接或图片列的目标列表。
  2. 在“列表”选项卡下,单击功能区中的“创建列”按钮。
  3. 为新列提供名称,将类型指定为“超链接或图片”。从“URL 格式为”下拉列表中选择 URL 的格式为“超链接”或“图片”。

    [玩转系统] 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-HyperLinkPictureColumnToList()
{ 
    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] $Format ="Hyperlink"
    )

    #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
        {
            #Define XML for Field Schema
            $FieldSchema = "<Field Type='URL' ID='{$FieldID}' DisplayName='$DisplayName' Name='$Name' Description='$Description' Required='$IsRequired' Format='$Format' />"
            $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="ProjectReference"
$DisplayName="Project Reference"
$Description="Enter the Project Reference"
$Format="Hyperlink" #or "Image"

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

PnP PowerShell 将 URL 列添加到 SharePoint Online 列表

在 PnP PowerShell 中使用类型为“URL”的 Add-PnPField cmdlet 在 SharePoint 在线列表中创建超链接字段:


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

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

#Add Hyperlink Field to list
Add-PnPField -Type URL -List $ListName -DisplayName "Company Website" -InternalName "CompanyWebsite" -AddToDefaultView

同样,我们可以从 XML 模式创建一个超链接字段。


#Define XML Schema for URL Field
$FieldXML= "<Field Type='URL' Name='CompanyWebsite' ID='$([GUID]::NewGuid())' DisplayName='Company Website' Format='Hyperlink' ></Field>"

#Add URL Field to list
Add-PnPFieldFromXml -FieldXml $FieldXML -List $ListName

设置“图片”字段的 Format='Image' 属性。

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

取消回复欢迎 发表评论:

关灯