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.

 

1 Comment

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

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

    Response.Write(myUrl.Url);

Comments have been disabled for this content.