Share via


SPFieldUrlValue class

表示SPFieldUrl物件的值。

Inheritance hierarchy

System.Object
  Microsoft.SharePoint.SPFieldUrlValue

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

Syntax

'宣告
<SerializableAttribute> _
Public Class SPFieldUrlValue
'用途
Dim instance As SPFieldUrlValue
[SerializableAttribute]
public class SPFieldUrlValue

Examples

下列範例是示範如何使用SPFieldUrlValue物件,來取得和設定欄位值的主控台應用程式。應用程式會搜尋包含特定 Url 的項目連結清單。如果清單並沒有該 Url 的連結,應用程式會加上陰影。

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.

請參閱

參照

SPFieldUrlValue members

Microsoft.SharePoint namespace