Stefan Goßner

Senior Escalation Engineer for SharePoint (WSS, SPS, MOSS, SP2010) and MCMS

Blogs

How to set posting properties content in authoring mode directly

  • Comments 1
  • Likes

MCMS allows editing of placeholder content by shipping placeholder control. But to edit property content it is necessary to first save the page and then open the property page and modify the content manually. In addition using the property page will allow to modify all properties and not only a subset which might be preferable in some situations.

This article discusses a solution used in the sample template of Connector for SharePoint Technologies for this problem.

The solution uses a normal input control (e.g. a TextBox control). What needs to be done is to ensure that the property content of the input control is loaded with the current property content whenever the page is loaded and to avoid doing this if it is a postback as the user already might have changed the content and would be anoyed if it is changed back to the value stored in the repository. This can be achieved by adding the following code to the Page_Load event:

if

(!Page.IsPostBack)
{
    DescriptInput.Text = CmsHttpContext.Current.Posting.Description;
}

DescriptInput is the TextBox control being used. Here the Description property is used but the same will work for any other property or custom property.

Beside this the content of the TextBox control need to be written back to the repository whenever the page gets saved but not with every postback as the user still might decide to cancel his edit action. The easiest solution for this is to register a new event handler for the event being raised when Web author saves the current content. Such an event handler would look like this:

private void saveDescription(object sender, WebAuthorPostingEventArgs e)
{
    e.Posting.Description = DescriptInput.Text;
}

To register this event hander the following code needs to be added to the OnInit event handler:

WebAuthorContext.Current.SavePostingEvent += new WebAuthorPostingEventHandler(saveDescription);

In addition you need to decide whether you would like to show this control in presentation mode or only during authoring mode and also to ensure that the control is only enabled in authoring mode. The following code added to the Page_Load event will do the job:

WebAuthorContextMode webauthorMode = WebAuthorContext.Current.Mode;

DescriptInput.Enabled = (webauthorMode == WebAuthorContextMode.AuthoringNew ||
                                  webauthorMode == WebAuthorContextMode.AuthoringReedit);

DescriptInput.Visible = (webauthorMode == WebAuthorContextMode.AuthoringNew ||
                                webauthorMode == WebAuthorContextMode.AuthoringReedit ||
                                webauthorMode == WebAuthorContextMode.PresentationUnpublished);

For reuse you can add the complete code either to a server control - like the placeholder controls - or to a user web control. Here is the complete solution based on a user web control:

namespace StefanG.UserWebControls
{
   
using System;
    using System.Data;
    using System.Drawing;
   
using System.Web;
   
using System.Web.UI.WebControls;
   
using System.Web.UI.HtmlControls;
    using Microsoft.ContentManagement.Publishing;
    using Microsoft.ContentManagement.WebControls;

   
public abstract class DescriptPropertyControl : System.Web.UI.UserControl
    {
       
protected System.Web.UI.WebControls.TextBox DescriptInput;

       
private void Page_Load(object sender, System.EventArgs e)
        {
            // deploy property to TextBox if it is not a postback

           
if (!Page.IsPostBack) 
            {
                DescriptInput.Text = CmsHttpContext.Current.Posting.Description;
            }

            // show, hide, enable and disable the control appropriatly
            WebAuthorContextMode webauthorMode = WebAuthorContext.Current.Mode;
            DescriptInput.Enabled = (webauthorMode == WebAuthorContextMode.AuthoringNew ||
                                              webauthorMode == WebAuthorContextMode.AuthoringReedit);
            DescriptInput.Visible = (webauthorMode == WebAuthorContextMode.AuthoringNew ||
                                            webauthorMode == WebAuthorContextMode.AuthoringReedit ||
                                            webauthorMode == WebAuthorContextMode.PresentationUnpublished);
        }

        // new event handler being fired during save which will write back the content
        private void saveDescription(object sender, WebAuthorPostingEventArgs e) 
        {
            e.Posting.Description = DescriptInput.Text;
        }

        override protected void OnInit(EventArgs e)
        {
           
InitializeComponent();
           
base.OnInit(e);

           
// register new event being called during save
           
WebAuthorContext.Current.SavePostingEvent += new WebAuthorPostingEventHandler(saveDescription);
        }

       
private void InitializeComponent()
        {
           
this.Load += new System.EventHandler(this.Page_Load);
        }
   
}
}

 

Comments
  • Hi, I'm trying to implement something like this in VB.Net, however when I try and add the event handler it breaks.


    WebAuthorContext.Current.SavePostingEvent += new WebAuthorPostingEventHandler(saveDescription);


    Intellisense dosnt find Current.SavePostingEvent and if I try to compile I get an error. I did however dind Webauthorcontext.Current.Events.Addhandler() but I don't know what to do with it.

    If you have any ideas I would be very appreciative.

    Brett

    bgarnier@burntsand.com

Raw Html Fix