Recently I had a requirement to collect publishing pages URLs in a SharePoint 2016 List. Adding an item in a list is straight forward and that's probably the first development task every SharePoint developer does.
But I was not able to find enough resources on adding a 'HyperLink or Picture Type' column in an SPList.
Below is the code snippet to assign value to a hyper link column.
Hyper link column expects value in <ABSOLUTE URL>, <TITLE> format.
Important thing to note here is that hyperlink should always start with 'http://'. If you try to assign a relative URL or a URL which starts with 'www', you will always get SPFieldValidationException.
Hope it will help.
But I was not able to find enough resources on adding a 'HyperLink or Picture Type' column in an SPList.
Below is the code snippet to assign value to a hyper link column.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SPListItem objSPItem = objList.Items.Add(); | |
//objPageItem is the source list item. | |
objSPItem = ["Title"] = objPageItem.Title; | |
objSPItem["PAGE"] = objSiteWeb.Url + "/" + objPageItem.Url + ", " + objPageItem.Title; | |
objSPItem.Update(); |
Important thing to note here is that hyperlink should always start with 'http://'. If you try to assign a relative URL or a URL which starts with 'www', you will always get SPFieldValidationException.
Hope it will help.
No comments:
Post a Comment