kwizcom banner advertisement

SharePoint Online: Update HyperLink Field Value using PowerShell

Requirement: Update hyperlink field in SharePoint Online using PowerShell
sharepoint online powershell update hyperlink field

SharePoint Online PowerShell to Update Hyperlink Field
Lets add a new link to Link list using PowerShell.
#Load SharePoint Online 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"

#Variables
$SiteURL="https://crescent.sharepoint.com"
$ListName = "Quick Links"
$FieldName = "URL"

#Get Credentials to connect
#$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)

#Add New Item to Link list
If($List -ne $Null)
{
    $ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
    $ListItem = $List.AddItem($ListItemInfo)
    $ListItem["Title"] = "Support Center"

    #Set Hyperlink field properties    
    $Link = New-Object Microsoft.SharePoint.Client.FieldUrlValue
    $Link.Url = "https://crescenttech.sharepoint.com/sites/support"
    $Link.Description = "Support Center URL"

    #Update Hyperlink Field
    $ListItem[$FieldName] = [Microsoft.SharePoint.Client.FieldUrlValue]$Link
    $ListItem.Update()
    $Ctx.ExecuteQuery()
 
    Write-host "New Item Added to Link List!" -ForegroundColor Green
}
Similarly, we can update existing hyperlink field values using PowerShell in SharePoint Online. Lets update all link list items with URL "/sites/support" with "/sites/supportV2".
#Load SharePoint Online 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"

#Variables
$SiteURL="https://crescent.sharepoint.com"
$ListName = "Quick Links"
$FieldName = "URL"

#Hyperlink Field Values
$HyperLinkValue = "Support Center"
$HyperLinkURL = "/sites/support" #Provide FULL URL for External URLs
$HyperLinkNewURL = "/sites/supportV2"

#Get Credentials to connect
#$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)

#Define the CAML Query to Filter by Hyperlink value and Get List Item
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "@
<View>
    <Query>
        <Where>
            <Eq >
                <FieldRef Name='$FieldName' /><Value Type='URL'>$HyperLinkURL</Value>
            </Contains >
        </Eq>
    </Query>
</View>"
 
#Get All List Items matching the query
$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()

#Update the List Item's Hyperlink Field value
If($ListItems.Count -gt 0)
{
    ForEach($Item in $ListItems)
    {
        #Get Hyperlink field value and update
        $HyperLinkField = [Microsoft.SharePoint.Client.FieldUrlValue]$Item[$FieldName]
        $HyperLinkField.Url = $HyperLinkNewURL
        $HyperLinkField.Description = $HyperlinkDescription

        #Update Hyperlink Field
        $Item[$FieldName] = $HyperLinkField
        $Item.Update()
        $Ctx.ExecuteQuery()
 
        Write-host "Hyperlink Field Value Updated in Link List!" -ForegroundColor Green
    }
}
Else
{
    Write-host "Hyperlink Field Value '$($HyperLinkURL)' Not Found in Link List!" -ForegroundColor Yellow
}
SharePoint Online: Update HyperLink Field Value using PowerShell SharePoint Online: Update HyperLink Field Value using PowerShell Reviewed by Salaudeen Rajack on August 13, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.