SPFieldUrlValue class
Representa o valor de um objeto SPFieldUrl .
Inheritance hierarchy
System.Object
Microsoft.SharePoint.SPFieldUrlValue
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Syntax
'Declaração
<SerializableAttribute> _
Public Class SPFieldUrlValue
'Uso
Dim instance As SPFieldUrlValue
[SerializableAttribute]
public class SPFieldUrlValue
Examples
O exemplo a seguir é um aplicativo de console que demonstra como um objeto de SPFieldUrlValue pode ser usado para obter e definir valores de campo. O aplicativo pesquisa a lista de Links para um item que contenha uma Url específica. Se a lista não tem um link para esse Url, o aplicativo adiciona um.
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.