SPFieldUrlValue class

Represents the value for an SPFieldUrl object.

Inheritance hierarchy

System.Object
  Microsoft.SharePoint.SPFieldUrlValue

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'Declaration
<SerializableAttribute> _
Public Class SPFieldUrlValue
'Usage
Dim instance As SPFieldUrlValue
[SerializableAttribute]
public class SPFieldUrlValue

Examples

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("https://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 = "https://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();
        }
    }
}
Imports System
Imports Microsoft.SharePoint

Module ConsoleApp

    Sub Main()

        Using site As New SPSite("https://localhost")

            Using web As SPWeb = site.OpenWeb("test")

                ' Get the Links list or create it if it does not exist.
                Dim list As SPList = web.Lists.TryGetList("Links")

                If list Is Nothing OrElse list.BaseTemplate <> SPListTemplateType.Links Then
                    ' Create the list.
                    Dim listId As Guid = web.Lists.Add("Links", "A list of interesting Web pages.", SPListTemplateType.Links)
                    list = web.Lists.GetList(listId, False)
                End If

                ' Create a link field value.
                Dim msdnValue As New SPFieldUrlValue()
                msdnValue.Description = "SharePoint Developer Center"
                msdnValue.Url = "https://msdn.microsoft.com/sharepoint"

                ' Print the field value.
                Console.WriteLine(msdnValue)

                ' Check if the list already has this link.
                Dim msdnItem As SPListItem = Nothing
                For Each item As SPListItem In list.Items
                    Dim rawValue As [Object] = item(SPBuiltInFieldId.URL)
                    Dim typedValue As New SPFieldUrlValue(rawValue.ToString())
                    If typedValue.Url = msdnValue.Url Then
                        msdnItem = item
                        Console.WriteLine("Existing link.")
                        Continue For
                    End If
                Next

                ' If it does not...
                If msdnItem Is Nothing Then
                    ' 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.")
                End If

            End Using

        End Using

        Console.Write(vbCrLf & "Press ENTER to continue....")
        Console.Read()
    End Sub

End Module

Thread safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See also

Reference

SPFieldUrlValue members

Microsoft.SharePoint namespace