Manage behavior of SharePoint 2010 composite fields

fix

February 10th, 2010

Let’s say you have a custom rendering template that defines how your New/Edit form is going to look like. You can’t define any events in your rendering template to handle any rules – such as some fields being disabled or greyed out during New/Edit operation. My business goal is not 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 case I have a custom Publish button on a 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;
            }
        }

You’ll notice I have this.Parent.Parent.Parent.FindControl(“Body”)  this will really depend on the structure of your rendering template so you may not end up with buch of Parent  after all. Now in my case I had my control referenced from the rendering template just like any other ASP control:

First defined:

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

Then referenced:

<mycontrols:PublishPost ID=”PublishPost1″ runat=”server” />

Hope that helps with your form customizations and business rules enforcement!


news

Any information posted on this blog does not reflect views of respective product vendors unless explicitely stated.

featured