Back to Insights

Dynamically toggle SharePoint 2010 list item composite fields

Here is the scenario: You have a SharePoint 2010 list form where, depending on some business rules, you need to set some of the fields off or set them to appear in a read-only mode. You can't define any events in your rendering template to handle any rules (e.g., some fields being disabled or greyed out during a new/edit operation). Let's say you don't want to allow editing of the body field on the EditForm once the list item has been created. If you have a custom control in your template you can actually access other fields in the rendering template and set their values.

In my scenario I have a custom publish button on an EditForm of my rendering template. My button inherits from "Microsoft.SharePoint.WebControls.PublishButton":

public class PublishPost : PublishButton

Now, in my "CreateChildControls"() I will access the body "CompositeField" in my template and make it disabled for editing ("SPControlMode.Edit"). Here is how:

protected override void CreateChildControls()
        {
            base.CreateChildControls();
            switch (ControlMode)
            {
                case SPControlMode.Edit:
                    {
                            CompositeField bodyField = ((CompositeField)this.Parent.Parent.Parent.FindControl("Body"));
                            if (bodyField != null)
                            {
                                bodyField.ControlMode = SPControlMode.Display;
                            }               
                    }
                    break;
// ??.. handle any other template modes such as New etc.
                default:
                    Visible = false;
                    break;
            }
        }

All of the above is a control that I had referenced from the rendering template just like any other ASP control.

First define it:

<%@ Register TagPrefix="mycontrols" Assembly=" MyProject.SharePoint, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c67ffbf29a6aaaaa" Namespace="MyProject.SharePoint.Controls" %>

Then reference it:

<mycontrols:PublishPost ID="PublishPost1? runat="server" />

Enjoy!

Share