SP.FieldCollection.addFieldAsXml Method (sp.js)

Creates a field based on the specified schema, Boolean value, and field options.

Applies to: apps for SharePoint | SharePoint Foundation 2013 | SharePoint Server 2013

SP.FieldCollection.addFieldAsXml()

Return value

Type: SP.Field

Example

The following example adds an input button on an application page that creates a field based on schema information and then displays the available fields in the Announcements list, including the new field.

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

    var fieldSchema = '<Field Type="Text" DisplayName="NewField" Name="NewField" />';

    function runCode() {
        var clientContext = new SP.ClientContext();
        var targetList = clientContext.get_web().get_lists().getByTitle('Announcements');
        fieldCollection = targetList.get_fields();
        fieldCollection.addFieldAsXml(fieldSchema, true, SP.AddFieldOptions.addToDefaultContentType);
        clientContext.load(fieldCollection);
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    }

    function onQuerySucceeded() {
        var message = "NewField added to Announcements list.\n\nThe following fields are available:\n\n";
        var fields = '';
        var listEnumerator = fieldCollection.getEnumerator();
        while (listEnumerator.moveNext()) {
            fields += listEnumerator.get_current().get_title() + "; ";
        }
        alert(message + fields);
    }

    function onQueryFailed(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>

REST resource endpoint

See FieldCollection resource for more information.

Endpoint URI structure

http://<sitecollection>/<site>/_api/web/lists(listid)/fields/createFieldAsXml

HTTP requests

This resource supports the following HTTP commands:

  • POST

POST syntax

POST http://<sitecollection>/<site>/_api/web/lists(listid)/fields/createFieldAsXml

Resource parameters

  • parameters

Parameters

  • schemaXml

  • addToDefaultView

  • options