This topic has not yet been rated - Rate this topic

CompositeField Class

Represents a field control and its label and description.

Namespace:  Microsoft.SharePoint.WebControls
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: No
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class CompositeField : FieldMetadata
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
CompositeField
Description

The Microsoft.SharePoint.WebControls.CompositeField class inherits from the Microsoft.SharePoint.WebControls.FieldMetadata base class which exposes a FieldMetadata.Field property in order to institute a reference to the current SPField instance for the control context.

CompositeField is called principally from RenderingTemplates and is the primary mechanism to independently render out SPListItem field(s). When the template is called, the control will execute an override on the DefaultTemplateName. The name will return "DisplayCompositeField" if the control mode is SPControlMode.Display, otherwise it will return "CompositeField".

The Usage Scenario

The primary usage of CompositeField is rendering a field with a recognizable field control based on a specific SPListItem. 

In the below, I am creating the required proxy objects by hydrating a SPSite and SPWeb by using a literal URL value. Following, a SPList object is hydrated by looking for the list with the name "My List". Following, we are instantiating a new CompositeField object, and setting the minimal amount of properties, most importantly the CompositeField.ListId and the CompositeField.FieldName.

C# Code Example

public class ExampleWebPart : WebPart
{
protected override void CreateChildControls()
{
using (SPSite site = new SPSite("My Url"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["My List"];
CompositeField field = new CompositeField();
field.EnableViewState = false;
field.ListId = list.ID;
field.FieldName = "My Field Name";
field.ID = "field";
Controls.Add(field);
}
}
}
}

Visual Basic .NET Code Example

Public Class ExampleWebPart
Inherits WebPart
Protected Overloads Overrides Sub CreateChildControls()
Using site As New SPSite("My Url")
Using web As SPWeb = site.OpenWeb()
Dim list As SPList = web.Lists("My List")
Dim field As New CompositeField()
field.EnableViewState = False
field.ListId = list.ID
field.FieldName = "My Field Name"
field.ID = "field"
Controls.Add(field)
End Using
End Using
End Sub
End Class

Adam Buenz
SharePoint Foundation MVP - http://www.sharepointsecurity.com