Compartilhar via


SP.FieldCollection.addFieldAsXml(schemaXml, addToDefaultView, options) Method

Applies to: SharePoint Foundation 2010

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

var value = SP.FieldCollection.addFieldAsXml(schemaXml, addToDefaultView, options);

Parameters

  • schemaXml
    A Collaborative Application Markup Language (CAML) string that contains the schema. It must not be null.

Type: String

  • addToDefaultView
    Specifies to add the field to the default list view.

    true if the field is added to the default list view; otherwise, false.

Type: Boolean

Type: SP.AddFieldOptions

Return Value

Type: SP.Field

Applies To

SP.FieldCollection Class

Exceptions

Exceptions

Descriptions

SPException

One or more field types are not installed properly, the formula is empty for the calculated field type, or an error occurred during the processing of the specified XML. Error code: -2146232832.

SPException

One or more field types are not installed properly. Error code: -2130575340.

SPException

Formula is empty for the calculated field type. Error code: -2130575199.

SPQueryThrottledException

The throttling limit is reached. Error code: -2147024860.

SPQueryThrottledException

There is a join throttle failure. Error code: -2147024749.

ArgumentException

The field with the specified internal name or title does not exist in the collection at the given scope. Error code: -2147024809.

UnauthorizedAccessException

The current user has insufficient permissions. Error code: -2147024891.

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>

See Also

Reference

SP.FieldCollection Methods

SP.FieldCollection Properties

SP Namespace