Get URL Value of SharePoint Hyperlink Field

Get URL Value of SharePoint Hyperlink Field

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:

[code lang="csharp"]SPListItem item = list.GetItemById(1);</span>

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

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:

[code lang="csharp"]SPFieldUrlValue value = new SPFieldUrlValue(item[“URL”].ToString());

string URL = value.Url;[/code]

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

Simples! Hope this helps :-)

One thought on “Get URL Value of SharePoint Hyperlink Field

  1. Nancy

    Is there a way to extract a hyperlink description in SharePoint Designer so I can use workflow to write that description value to a separate text field that I can use in other ways?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">