Using Control Adapter to Set Default Values for Publishing Pages

February 24 2010 68 comments

Developers, who use customized page layouts bound with custom content type, have probably realized that if your custom content type is not the default content type of Pages library, you will not get default values populated when you create a new page. One way to work around this is to create feature, which sets your customized content type to be the default (1st content type) in Pages library. However, it does not work for the rest of the page layouts in that same library.

Here is another workaround to consider. Create a control adapter, which will populate default values to any of the publishing page in Pages library.

public class BaseFieldControlAdapter : ControlAdapter
{
 protected override void OnLoad(EventArgs e)
 {
  var bfc = Control as BaseFieldControl;
  var context = SPContext.Current;
           
  if (bfc != null && context != null)
  {
  var item = context.ListItem;

  if (item != null)
  {
   if (bfc.Field != null && !Page.IsPostBack)
    {
     var defaultValue = bfc.Field.DefaultValueTyped;

     if (defaultValue != null)
     {
      if (bfc.ItemFieldValue == null)
       bfc.ItemFieldValue = defaultValue;
      if (bfc.Value == null)
       bfc.Value = defaultValue;                                    
     }
    }
   }
  }
  base.OnLoad(e);
 }
}

When using adapter you need to create .browser file for the web application’s App_Browsers directory

<?xml version="1.0" encoding="utf-8" ?>
<browsers>
 <browser refID="Default">
  <controlAdapters>
   <adapter controlType="Microsoft.SharePoint.WebControls.BaseFieldControl"
   adapterType="..BaseFieldControlAdapter, ..., Ver..., Cul..., Pub..." />
   </controlAdapters>
  </browser>
</browsers>

As a bonus, if you override Render method, you can use it to mark Required field on the page in edit mode by changeing the css class of that control and then customize it as you wish.

protected override void Render(HtmlTextWriter writer)
{
 var bfc = Control as BaseFieldControl;

 if (SPContext.Current.FormContext.FormMode == SPControlMode.Edit ||
     bfc == null || bfc.Field == null || !bfc.Field.Required)
 {
  base.Render(writer);
  return;
 }

 using (var sw = new StringWriter())
  {
   using (var tw = new HtmlTextWriter(sw))
   {
    base.Render(tw);
   }

   sw.GetStringBuilder()
   .Replace("ms-formfieldcontainer", "ms-formfieldcontainer req");
   writer.Write(sw.ToString());
  }
}

Snippets above demonstrate just how to use control adapter to do this. You still need to check if current page is in edit mode and that it’s a new page (otherwise it will always display the default values, not the saved ones).

***********************************************
Jerry Seinfeld: Why not? We’re neighbors. What’s mine is yours.
Cosmo Kramer: [leaning against the door-frame and looking around in wonder] Really?
***********************************************

Popularity: 8% [?]

68 comments to “Using Control Adapter to Set Default Values for Publishing Pages”

  1. ブログこのについて 知らさ言った私の父に 、このサイト 素晴らしい 実際です。
    店内全品ポイント10倍 格安通販サイト http://www.ulucanlar.com.tr/gain-25121-204.html

  2. 私はよ すぐに |私はとしてRSSフィードRSSあなたすることはできません 電子メールサブスクリプションハイパーリンクまたは Eニュースサービス} {あなたのメールを検索する|見つけます。行うあなたが持っているいずれかを? ください 許可を ように 認識私は私が可能性購読してください。ありがとうございます。
    メール便送料無料 新しいスタイル http://www.yogawithjane.co.uk/tide-24075-219.html

  3. This machine is capable of making one or two cups of espresso at a time and includes a conventional steam wand for frothing milk for cappuccinos.

  4. The Brighton Dance Studios are composed of three rooms dedicated to bop, the biggest
    which is a basketball court-sized dance space outfitted with mirrors,
    a sound system, ballet barres and a coat room.

  5. Whether monetary causes, disagreements, a lack of expertise or understanding, or a contemporary method with a brand new set of eyes was the trigger for switching
    to a new SEO company, dumping your outdated agency is not all the time the suitable
    option.

  6. We are also blissful to make your tour private for a special occasion, or
    adapt excursions to your preferences.

  7. In interviews and on blogs about freelancing, freelancers
    list choice and flexibility as a benefit.

  8. Outsource support You can outsource IT help to a different company, who can present each proactive and reactive assist for your enterprise.

  9. If that is not likely acceptable or would not fit with your clip or in your channel you
    will have the choice of obtaining a license, and these
    fluctuate wildly in price, however even whenever you do obtain a license it’s still not straight ahead to maintain the music on your clip
    without the copyright holder’s advertising.

  10. First and foremost, it means fans can now get
    new music on the identical day worldwide fairly than having to
    wait for their very own nationwide launch day.

  11. Appreciating the commitment you put into your site and detailed information you offer.

    It’s awesome to come across a blog every once in a while that isn’t the
    same unwanted rehashed information. Fantastic read!
    I’ve saved your site and I’m including your RSS feeds to my
    Google account.

  12. I discovered your report very fascinating and had
    the same views as you on the park.

  13. On this web page you’ll find on-line classes for learners, free alphabet
    templates, plus a number of inspiration.

  14. In 2014, DreamWorks Animation held talks a few attainable sale to Japanese communications and media company Softbank Corp, a supply with knowledge of the matter instructed Reuters on the time.

  15. On the other hand, one who engaged in evil deeds and evil thoughts follows the dictates of his
    thoughts does occur to get pleasure from only unhealthy issues;
    he likes liquor, meat, garlic, onions, stale and tÍmasic food items.

  16. Your web site offered us with helpful information to work
    on. You have executed a formidable job and our total neighborhood shall be grateful to you.

  17. Using Control Adapter to Set Default Values for Publishing Pages | SharePoint Blues
    mackage jackets http://www.rcmpcc.ca/web/mackage.asp

Leave a Reply