Share via


FieldCollection.AddFieldAsXml method

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

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

Syntax

'Declaration
Public Function AddFieldAsXml ( _
    schemaXml As String, _
    addToDefaultView As Boolean, _
    options As AddFieldOptions _
) As Field
'Usage
Dim instance As FieldCollection
Dim schemaXml As String
Dim addToDefaultView As Boolean
Dim options As AddFieldOptions
Dim returnValue As Field

returnValue = instance.AddFieldAsXml(schemaXml, _
    addToDefaultView, options)
public Field AddFieldAsXml(
    string schemaXml,
    bool addToDefaultView,
    AddFieldOptions options
)

Parameters

  • schemaXml
    Type: System.String

    A Collaborative Application Markup Language (CAML) string that contains the schema.

    It must not be a null reference (Nothing in Visual Basic). It must not be empty. It must be a valid Collaborative Application Markup Language (CAML) string according to the schema specified in [MS-WSSFO2], section 2.2.9.3.3.1.

  • addToDefaultView
    Type: System.Boolean

    Specifies to add the field to the default list view.

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

Return value

Type: Microsoft.SharePoint.Client.Field
A Field object that represents the new field.

Exceptions

Exception Condition
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.

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

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

SPQueryThrottledException

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

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.

Examples

This code example creates a field based on schema information and then displays the available fields in the Announcements list, including the new field.

using System;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointFoundation.Samples
{
    class AddFieldAsXmlExample
    {
        static void Main()
        {
            string siteUrl = "http://MyServer/sites/MySiteCollection";

            ClientContext clientContext = new ClientContext(siteUrl);
            Web site = clientContext.Web;
            List targetList = site.Lists.GetByTitle("Announcements");
            FieldCollection collField = targetList.Fields;

            string fieldSchema = "<Field Type='Text' DisplayName='NewField' Name='NewField' />";
            collField.AddFieldAsXml(fieldSchema, true, AddFieldOptions.AddToDefaultContentType);

            clientContext.Load(collField);
            clientContext.ExecuteQuery();

            Console.WriteLine("NewField added to Announcements list.\n\nThe following fields are available:\n\n");
            foreach (Field myField in collField)
               Console.WriteLine(myField.Title);
        }
    }
}

See also

Reference

FieldCollection class

FieldCollection members

Microsoft.SharePoint.Client namespace