SPFieldUrlValue - Classe
Représente la valeur d'un objet SPFieldUrl .
Hiérarchie d’héritage
System.Object
Microsoft.SharePoint.SPFieldUrlValue
Espace de noms : Microsoft.SharePoint
Assembly : Microsoft.SharePoint (dans Microsoft.SharePoint.dll)
Syntaxe
'Déclaration
<SerializableAttribute> _
Public Class SPFieldUrlValue
'Utilisation
Dim instance As SPFieldUrlValue
[SerializableAttribute]
public class SPFieldUrlValue
Exemples
L'exemple suivant est une application de console qui montre comment un objet SPFieldUrlValue peut être utilisé pour obtenir et définir des valeurs de champ. L'application recherche dans la liste de liens pour un élément qui contient une Url particulière. Si la liste n'a pas un lien vers cette Url, l'application ajoute une.
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
Cohérence de thread
Tous les membres statique (Partagé dans Visual Basic)s publics de ce type sont thread-safe. Cela n’est pas garanti pour les membres d’instance.