SPField.StaticName property
取得或設定欄位的靜態名稱。
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Syntax
'宣告
Public Property StaticName As String
Get
Set
'用途
Dim instance As SPField
Dim value As String
value = instance.StaticName
instance.StaticName = value
public string StaticName { get; set; }
Property value
Type: System.String
包含靜態欄位名稱的字串。
Exceptions
Exception | Condition |
---|---|
NotSupportedException | 欄位是屬於具有外部資料來源的清單,並且您嘗試設定的值不是InternalName屬性的值相同。 |
備註
欄位是在具有外部資料來源] 清單中,如果StaticName屬性一律會傳回InternalName屬性的值。如果您嘗試將StaticName屬性設定為不是InternalName屬性的值相同的值,會擲回例外狀況。
否則, StaticName和InternalName屬性所傳回的值可以是不同。請注意您就可以設定StaticName屬性,而InternalName屬性則是唯讀屬性。
InternalName屬性的值是欄位集合內唯一的。StaticName屬性的值不是一定是唯一的。
Examples
下列範例會為主控台應用程式,以說明欄位的顯示名稱、 靜態名稱,與內部名稱之間的差異。請注意程式碼會前兩個的名稱。SharePoint Foundation,來建立此欄位的內部名稱。
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.RootWeb)
{
string strDisplayName = "My Custom Field";
string strStaticName = "MyStaticName";
SPField field = web.Fields.TryGetFieldByStaticName(strStaticName);
if (field == null)
{
string strInternalName = web.Fields.Add(strDisplayName, SPFieldType.Text, false);
field = web.Fields.GetFieldByInternalName(strInternalName);
field.StaticName = strStaticName;
field.Update();
}
Console.WriteLine("Title: {0}", field.Title);
Console.WriteLine("Internal name: {0}", field.InternalName);
Console.WriteLine("Static name: {0}", field.StaticName);
}
}
Console.Write("\nPress ENTER to continue...");
Console.ReadLine();
}
}
}
Imports System
Imports Microsoft.SharePoint
Namespace ConsoleApp
Friend Class Program
Shared Sub Main(ByVal args() As String)
Using site As New SPSite("https://localhost")
Using web As SPWeb = site.RootWeb
Dim strDisplayName As String = "My Custom Field"
Dim strStaticName As String = "MyStaticName"
Dim field As SPField = web.Fields.TryGetFieldByStaticName(strStaticName)
If field Is Nothing Then
Dim strInternalName As String = web.Fields.Add(strDisplayName, SPFieldType.Text, False)
field = web.Fields.GetFieldByInternalName(strInternalName)
field.StaticName = strStaticName
field.Update()
End If
Console.WriteLine("Title: {0}", field.Title)
Console.WriteLine("Internal name: {0}", field.InternalName)
Console.WriteLine("Static name: {0}", field.StaticName)
End Using
End Using
Console.Write(vbLf & "Press ENTER to continue...")
Console.ReadLine()
End Sub
End Class
End Namespace
應用程式會在列印下列輸出至主控台。
Title: My Custom Field
Internal name: My_x0020_Custom_x0020_Field
Static name: MyStaticName
Press ENTER to continue...