Retrieving a URL field from a list

Recently I had a need to retrieve a URL field (Field Type was Hyperlink or Picture) from a SharePoint List so that I could use the URL to dynamically create a Hyperlink.

Much to my surprise when I fetched the column from the list, I neglected to do any checking to make sure that the field only contained the URL, turns out SharePoint stores the value for the URL in the list as a comma separated string.

If you split the string on the comma the first part will be the full URL ( http://servername/Pages/PageName.aspx ) including the protocol and server name while the second part is the URL ( /Pages/PageName.aspx ) minus the protocol and server name.

 

Published Thursday, October 30, 2008 12:23 PM by dmccollough
Filed under: , ,

Comments

# re: Retrieving a URL field from a list

Thursday, March 12, 2009 11:36 AM by atr2000

MOSS 2007 - You can utilize SPFieldUrlValue class to split the information out.

SPFieldUrlValue myUrl = new SPFieldUrlValue(mySPListItem["HyperlinkFieldName"].ToString());

Response.Write(myUrl.Url);