SPFieldUrlValue class
SharePoint 2013
Represents the value for an SPFieldUrl object.
Namespace:
Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
The following example is a console application that demonstrates how an SPFieldUrlValue object can be used to get and set field values. The application searches the Links list for an item that contains a particular Url. If the list does not have a link to that Url, the application adds one.
using System; using Microsoft.SharePoint; namespace ConsoleApp { class Program { static void Main(string[] args) { using (SPSite site = new SPSite("http://localhost")) { using (SPWeb web = site.OpenWeb("test")) { // Get the Links list or create it if it does not exist. SPList list = web.Lists.TryGetList("Links"); if (list == null || list.BaseTemplate != SPListTemplateType.Links) { // Create the list. Guid listId = web.Lists.Add("Links", "A list of interesting Web pages.", SPListTemplateType.Links); list = web.Lists.GetList(listId, false); } // Create a link field value. SPFieldUrlValue msdnValue = new SPFieldUrlValue(); msdnValue.Description = "SharePoint Developer Center"; msdnValue.Url = "http://msdn.microsoft.com/sharepoint"; // Print the field value. Console.WriteLine(msdnValue); // Check if the list already has this link. SPListItem msdnItem = null; foreach (SPListItem item in list.Items) { Object rawValue = item[SPBuiltInFieldId.URL]; SPFieldUrlValue typedValue = new SPFieldUrlValue(rawValue.ToString()); if (typedValue.Url == msdnValue.Url) { msdnItem = item; Console.WriteLine("Existing link."); continue; } } // If it does not... if (msdnItem == null) { // Create a new list item and set the URL field value. msdnItem = list.Items.Add(); msdnItem[SPBuiltInFieldId.URL] = msdnValue; msdnItem.Update(); Console.WriteLine("Link added."); } } } Console.Write("\nPress ENTER to continue...."); Console.ReadLine(); } } }