Using Control Adapter to Set Default Values for Publishing Pages

February 24 2010 84 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% [?]

84 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

  18. Great ! Thank you very much

  19. your publishing page design amazing i m like him i m new visitor kindly more blogs

  20. buy review movie…

    Edit a paper online editing services Buy resume.. Buy resume help., Edit resume…..

  21. Acronis Cracks…

    buy cheap adobe creative suite buy cheap adobe creative suite. buy cheap adobe creative suite, buy cheap adobe creative suite….

  22. Lloyd says:

    When you want a keyboard to make use of in stay performances AND a full-featured MIDI controller, the Novation Launchkey may just be what you’re in search of.
    Some people could also be searching for a keyboard that
    lets them check out the fundamentals of music production, whereas professionals, alternatively,
    may be trying to spend money on a full-featured keyboard to make use of
    in their live performances. While numerous what you need shall be offered by this system itself, one of the best value
    midi keyboard should at the very least embody the likes of pitch
    and modulation management and a few separate contact pads for
    beat programming. Although designed for a musician who is gigging and desires quality tone and touch piano, the Yamaha P-155 can be used at
    dwelling for practicing. The keys are stable and semi-weighted, and
    it comes with solely five sounds-piano, electric
    piano, organ, synth and bass—that virtually sound like the real thing.

  23. Willa says:

    However it’s essential eat wholesome weight loss program and also
    apply daily workouts for protecting match after dropping weight and getting beautiful shape.
    Because of the high diploma of benefits that are found in these
    merchandise, their gross sales are step by step growing and people are getting
    back their dream figures. The Herbalife weight loss supplements that are being supplied
    by the company is made up of accredited elements,
    so that people tend to get rather a lot of benefits of these packages and usually are not having any negative
    effects. Walking is possibly one of the a lot more overlooked
    areas of workout. Many users really document one observe at a
    time, so they actually don’t need sixty-one keys since they
    will not use both the hands together. Many individuals are
    consuming these products and they’re taking a
    look at their determine which is slimming down in a quick time, if the
    products are consumed properly and regularly as per the instructions.

  24. German says:

    Hmm it seems like your blog ate my first comment (it was super long) so I guess I’ll just sum
    it up what I submitted and say, I’m thoroughly enjoying your blog.
    I too am an aspiring blog writer but I’m still new to everything.

    Do you have any suggestions for newbie blog writers?
    I’d certainly appreciate it.

  25. Marvelous, what a weblog it is! This web site gives helpful facts to us, keep it up.

  26. I relish, result in I found exactly what I used to be taking a look for.
    You have ended my 4 day lengthy hunt! God Bless
    you man. Have a great day. Bye

  27. You’re so cool! I do not think I’ve read through anything like this before.
    So wonderful to discover somebody with some unique thoughts on this issue.

    Really.. thank you for starting this up. This
    website is one thing that is required on the web, someone with a little originality!

  28. Elvera says:

    If you are going for finest contents like I
    do, only pay a quick visit this site all the time for the reason that it
    provides feature contents, thanks

  29. I think that is among the most important info for me.
    And i am satisfied studying your article. But wanna commentary on some general issues, The website taste is ideal,
    the articles is really excellent : D. Good task, cheers

  30. Aw, this was an extremely good post. Spending some time and actual effort to
    generate a superb article… but what can I say…
    I put things off a whole lot and never manage to get nearly
    anything done.

  31. Shin Leho says:

    Anyone know if durkin, o connor, ruane or parsons have a chance of making the 26? Even 1 of these making it would give the team and supporters a great boast at an opportune time during the game.

  32. Remarkable! Its in fact remarkable post, I have got much clear
    idea concerning from this paragraph.

  33. Thanks for some other informative site. The place else may
    I am getting that kind of info written in such an ideal approach?

    I’ve a project that I’m simply now working on, and I have
    been at the glance out for such information.

Leave a Reply