Get URL Value of SharePoint Hyperlink Field

12 10 2009

I needed to get the value of a SharePoint hyperlink field in a custom list the other day so I simply tried to access the value of the field directly in the object model:

SPListItem item = list.GetItemById(1);

string URL = item[“URL”].ToString();

The only problem with this is whilst it gets the value, you end up with two URL’s separated by a comma. This is because one is the actual display text for the field and the other is the value (the actual URL itself).

In order to get just the URL value, you need to use the following object model code:

SPFieldUrlValue value = new SPFieldUrlValue(item[“URL”].ToString());

string URL = value.Url;

You can also use value.Description to get the actual display text for the field.

Simples! Hope this helps 🙂