SP.Field Class

SharePoint 2010

Last modified: April 07, 2011

Applies to: SharePoint Foundation 2010

Represents a field in a list on a Microsoft SharePoint Foundation Web site.

SP.Field

The following example creates an input button on an application page that gets a specified field, creates a description for the field, and displays the field title and new description.

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<script type="text/ecmascript" language="ecmascript">

    var fieldCollection;
    var oneField = null;
    function runCode() {
        var clientContext = SP.ClientContext.get_current();
        if (clientContext != undefined && clientContext != null) {
            var webSite = clientContext.get_web();
            taskList = webSite.get_lists().getByTitle("Tasks");

            fieldCollection = taskList.get_fields();
            this.oneField = fieldCollection.getByInternalNameOrTitle("Title");

            this.oneField.set_description("MyNewFieldDescription");
            this.oneField.update();

            clientContext.load(this.fieldCollection);
            clientContext.load(this.oneField);
            clientContext.executeQueryAsync(Function.createDelegate(this, this.OnLoadSuccess), Function.createDelegate(this, this.OnLoadFailed));
        }
    }

    function OnLoadSuccess(sender, args) {
        var fieldInfo = '';
        fieldInfo += 'Field Title: ' + oneField.get_title() + '\n' + 'Description: ' + oneField.get_description() + '\n';
        alert(fieldInfo);
    }

    function OnLoadFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
</script>

    <input id="Button1" type="button" value="Run Code" onclick="runCode()" />

</asp:Content>

Show: