Export (0) Print
Expand All
Expand Minimize

SPField.StaticName property

Gets or sets a static name for the field.

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

public string StaticName { get; set; }

Property value

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

ExceptionCondition
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.

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.

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();
        }
    }
}

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...
Show:
© 2014 Microsoft