Condividi tramite


SPFieldCollection.AddFieldAsXml Method (String)

Creates a field based on the specified schema.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

Syntax

'Declaration
Public Function AddFieldAsXml ( _
    strXml As String _
) As String
'Usage
Dim instance As SPFieldCollection
Dim strXml As String
Dim returnValue As String

returnValue = instance.AddFieldAsXml(strXml)
public string AddFieldAsXml(
    string strXml
)

Parameters

Return Value

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

Examples

The following code example uses CAML to define a field and adds the field to the specified list.

Dim webSite As SPWeb = SPContext.Current.Site.AllWebs("MySite")
Try
    Dim fields As SPFieldCollection = webSite.Lists("MyList").Fields

    Dim newField As String = "<Field Type=""Calculated"" " + 
    "DisplayName=""New_Field_Display_Name"" ResultType=""Currency"" " + 
    "ReadOnly=""TRUE"" Name=""New_Field_Internal_Name"">" + 
    "<Formula>=Currency_Field_Name*100</Formula>" + 
    "<FieldRefs><FieldRef Name=""Currency_Field_Name"" />" + 
    "</FieldRefs></Field>"

    fields.AddFieldAsXml(newField)
Finally
    webSite.Dispose()
End Try
using (SPWeb oWebsite = SPContext.Current.Site.AllWebs["MySite"])
{
    SPFieldCollection collFields = oWebsite.Lists["MyList"].Fields;

    string strNewField = "<Field Type=\"Calculated\" " +
    "DisplayName=\"New_Field_Display_Name\" ResultType=\"Currency\" " +
    "ReadOnly=\"TRUE\" Name=\"New_Field_Internal_Name\">" +
    "<Formula>=Currency_Field_Name*100</Formula>" +
    "<FieldRefs><FieldRef Name=\"Currency_Field_Name\" />" + 
    "</FieldRefs></Field>";

    collFields.AddFieldAsXml(strNewField);
}

Note

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Disposing Objects.

See Also

Reference

SPFieldCollection Class

SPFieldCollection Members

AddFieldAsXml Overload

Microsoft.SharePoint Namespace