How to Get URL value from SharePoint Hyperlink Field C#

How to Get URL value from SharePoint Hyperlink Field C#

In this post, we’re going to explain How to retrieve a URL value from SharePoint Hyperlink Field using C# SharePoint Server Object Model (SSOM).


SharePoint Get Hyperlink Field Value C#

Consider you have a custom list that contains a “Hyperlink or Picture” column, and you would like to get and retrieve the URL value from this SharePoint Hyperlink field using C# SSOM.

SharePoint Get Hyperlink Field Value C#
SharePoint Get Hyperlink Field Value C#

Therefore, I have tried the below code:

Get URL value from Hyperlink Field in SharePoint using C#
SharePoint Get Hyperlink Field Value C# programmatically
using (SPSite site = new SPSite("SiteURL"))
   {
     using (SPWeb web = site.OpenWeb())
      {
       SPList list = web.Lists["ListName"];
       SPQuery query = new SPQuery();
       query.Query = "<View><ViewFields><FieldRef Name='URL'/></ViewFields></View>"; 
       SPListItemCollection Items = list.GetItems(query);
      // Bind Checkbox List
       foreach (SPListItem Item in Items)
          {
            string URL = Item["URL"].ToString();
          }
      }
}

Unfortunately, the above code will return

  • The URL,
  • The Description.

But in most cases, you may need to only retrieve the URL value from SharePoint Hyperlink field without description as shown below:

http://URL

How to Get URL value from SharePoint Hyperlink Field C#?

To overcome this issue, and get only the URL without description from SharePoint Hyperlink Field Using C#, you should use the “SPFieldUrlValue SharePoint” Object model Class as written in the below code:

SPFieldUrlValue URLvalue = new SPFieldUrlValue(item["URL"].ToString());
string acturlURL =  URLvalue.Url;
Get URL value without description from Hyperlink Field in SharePoint using C#

As per the above code, the retrieved value should be only the URL without description now.

Full code

using (SPSite site = new SPSite("SiteURL"))
   {
     using (SPWeb web = site.OpenWeb())
      {
       SPList list = web.Lists["ListName"];
       SPListItem item = list.GetItemByID(1);
       SPFieldUrlValue URLvalue = new SPFieldUrlValue(item["Hyperlink Field Name"].ToString());
       HyperLinkControl.NavigateUrl =  URLvalue.Url; // the url value
      }
}

Conclusion

In conclusion, we have learned how to programmatically get The SharePoint Hyperlink field using C#. as well as we learned how to retrieve the URL value without description from the SharePoint Hyperlink field using C#.

Applies To
  • SharePoint 2016.
  • SharePoint 2013.
  • SharePoint 2010.
You may also like to read
Have a Question?

If you have any related questions, please don’t hesitate to ask it at deBUG.to Community.

1 thought on “How to Get URL value from SharePoint Hyperlink Field C#”

  1. hi
    tnx for the article.
    iam doing a excel file import using programmatically and trying to bind the hyperlink value from the excel to a hyperlink field in the splist.
    but i am unable to do that

Leave a Reply