Share via


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)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

Syntax

'Declaration
<SerializableAttribute> _
<ClientCallableTypeAttribute(Name := "FieldUrlValue", ValueObject := True,  _
    ServerTypeId := "{FA8B44AF-7B43-43f2-904A-BD319497011E}")> _
<SubsetCallableTypeAttribute> _
Public Class SPFieldUrlValue
'Usage
Dim instance As SPFieldUrlValue
[SerializableAttribute]
[ClientCallableTypeAttribute(Name = "FieldUrlValue", ValueObject = true, ServerTypeId = "{FA8B44AF-7B43-43f2-904A-BD319497011E}")]
[SubsetCallableTypeAttribute]
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