Site Column Web Part

Did you ever wonder if you could plug a SharePoint Field Control into a Web Part?

I did today – and yes, you can. The code is trivial. Instantiate the Field Control and add it to the controls collection. Ok, maybe normal people do not do that. But I wanted to add some flexibility to the page layout (Moss in a WCM scenario) so that site administrators can move the page content around.

Check-out the simplified example of my Site Column Web Part. I did not compile the code below, but you will get the idea. The administrator will need to assign the FieldName property correctly. Either you could create a custom Editor Part so that the administrator only can choose between the available site columns that have not yet a Field Control assigned or you could create a web part for each site column with the FieldName somewhat hard-coded.

namespace SiteColumnWebPart
{
    public class SiteColumnWebPart : Microsoft.SharePoint.WebPartPages.WebPart
    {
        private string fieldName = null;

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [System.ComponentModel.Category("Custom Properties")]
        [WebDisplayName("Field Name")]
        [WebDescription("Name of the site column you want to bind to.")]
        public string FieldName
        {
            get { return (fieldName == null) ? “SomeSiteColumnsName" : fieldName; }
            set { fieldName = value; }
        }

        protected override void CreateChildControls()
        {
              base.CreateChildControls();
              RichHtmlField c = new RichHtmlField();
              c.FieldName = FieldName;
              this.Controls.Add(c);
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *