Monday, February 1, 2016

Programatically assign value to a 'Hyperlink or Picture Type' column in a SharePoint 2016 List

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.

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();
view raw gistfile1.txt hosted with ❤ by GitHub
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.



No comments:

Post a Comment