kwizcom banner advertisement

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

How to Create Hyperlink or Picture Column to List using PowerShell
  • Browse to your SharePoint Online site and Navigate to the target list in which you want to add hyperlink or picture column.
  • Under the List tab, click on "Create Column" button in the ribbon.
  • 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" drop down.
    Add Hyperlink or Picture Column to SharePoint Online List using PowerShell
  • Fill other optional values such as Field description, Required and Add to default view settings.  Click on "OK" to create Hyperlink or Picture field in SharePoint Online list.

PowerShell to Add Hyperlink or Picture Column to SharePoint Online List:
#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="",
        [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 
SharePoint Online: Add Hyperlink or Picture Column to List using PowerShell SharePoint Online: Add Hyperlink or Picture Column to List using PowerShell Reviewed by Salaudeen Rajack on October 26, 2016 Rating: 5

2 comments:

  1. 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

    ReplyDelete
    Replies
    1. 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

      Delete

Please Login and comment to get your questions answered!

Powered by Blogger.