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).
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.
Therefore, I have tried the below code:
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
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;
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
- SharePoint Lookup Field Multiple Values Via C#.
- Get and Set a SharePoint Lookup Field Value Using Server Object Model C#.
- Get and Set a SharePoint Multiple Choice Field Value Using Server Object Model C#.
Have a Question?
If you have any related questions, please don’t hesitate to ask it at deBUG.to Community.
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