SPField.StaticName property

Gets or sets a static name for the field.

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

Syntax

'Declaration
Public Property StaticName As String
    Get
    Set
'Usage
Dim instance As SPField
Dim value As String

value = instance.StaticName

instance.StaticName = value
public string StaticName { get; set; }

Property value

Type: System.String
A string that contains the static name of the field.

Exceptions

Exception Condition
NotSupportedException

The field belongs to a list that has an external datasource and the value you are attempting to set is not the same as the value of the InternalName property.

Remarks

If the field is in a list that has an external datasource, the StaticName property always returns the value of the InternalName property. An exception is thrown if you try to set the StaticName property to a value that is not the same as the value of the InternalName property.

Otherwise, the values returned by the StaticName and InternalName properties can be different. Note that you can set the StaticName property whereas the InternalName property is read-only.

The value of the InternalName property is unique within a field collection. The value of the StaticName property is not necessarily unique.

Examples

The following example is a console application that illustrates the differences between a field's display name, static name, and internal name. Note that the code sets only the first two names. The field's internal name is created by SharePoint Foundation.

using System;
using Microsoft.SharePoint;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://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("http://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

The application prints the following output to the console.

Title: My Custom Field
Internal name: My_x0020_Custom_x0020_Field
Static name: MyStaticName

Press ENTER to continue...

See also

Reference

SPField class

SPField members

Microsoft.SharePoint namespace

TryGetFieldByStaticName(String)