SharePoint Online: Add Hyperlink or Picture Column to List using PowerShell

Requirement: Add a hyperlink column to the SharePoint Online list.

The Hyperlink or Picture column in SharePoint Online is used to store URLs of web addresses or pictures. The field type can be configured to hold either a URL or a Picture. Here are the steps to add a hyperlink field to the SharePoint Online list:

  1. Browse to your SharePoint Online site and navigate to the target list in which you want to add a hyperlink or picture column.
  2. Under the List tab, click on the “Create Column” button in the ribbon.
  3. Provide the Name to your new column, specify the type as “Hyperlink or Picture”. Select the format of the URL as either Hyperlink or Picture from the “Format URL as” dropdown. Add Hyperlink or Picture Column to SharePoint Online List using PowerShell
  4. Fill in other optional values such as Field description, Required, and Add to default view settings.  Click on “OK” to create a Hyperlink or Picture field in the SharePoint Online list.

Here is the PowerShell script to add a hyperlink field to the SharePoint Online list. Just set the parameters accordingly and run the script.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\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 to Add URL Column to SharePoint Online List

Use the Add-PnPField cmdlet in PnP PowerShell with type as “URL” to create a hyperlink field in SharePoint online list:

#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

Similarly, we can create a hyperlink field from the XML schema.

#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

Set the Format=’Image’ attribute for the Picture field.

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

2 thoughts on “SharePoint Online: Add Hyperlink or Picture Column to List using PowerShell

  • Hi Salaudeen,

    It shows the option for hyperlink, not browse image. How to browse image through Picture column?
    Could you please help me on this.

    Thanks

    Reply
    • There is no way to do this out of the box. You have to use Custom/3rd party controls.. E.g. https://archive.codeplex.com/?p=sparqubepicturelite

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *