2009年4月5日 星期日

Validator control in webpart

Sometimes we'll create a webpart that contains ASP.Net control and maybe we'll add some validator contorl in this webpart.In your publishing page, you add that webpart and click publish, and maybe you'll get a error message:
This page contains content or formatting that is not valid. You can find more information in the affected sections.

This error message cause from the validator control in this webpart. However, you won't those validator will cause when you click save as a draft /check in / publish. How could we avoid this error occur?

  1. Check webpart mode
    Check if the web part is in edit or design mode and only add the validator if not:
    if (this.WebPartManager.DisplayMode != WebPartManager.EditDisplayMode && this.WebPartManager.DisplayMode != WebPartManager.DesignDisplayMode)
    {
    // in display mode(I add validator control in here)
    }
  2. using the SPContext.Current.FormContext.FormMode.
    // check if the form is in display mode
    bool inDisplayMode = SPContext.Current.FormContext.FormMode == SPControlMode.Display;
    protected override void CreateChildControls()
    {
    base.CreateChildControls();// Add the text box and validator
    bool inDisplayMode = SPContext.Current.FormContext.FormMode == SPControlMode.Display;
    if (inDisplayMode
    {
    // in display mode(I add validator control in here)
    }
    }

2009年3月23日 星期一

Use [Today] and [Me] flag in Calculated Column

You can create a calculated column in a sharepoint list. It's usefal column to using some excel function to help you calculate some data in your list.Sharepoint have two key word in function: [Today] and [Me]. However, unfortunately if you try to add ‘[Today]‘ in formula, it gives error : “Calculated columns cannot contain volatile functions like Today and Me”.



Therefore, you can follow steps as following:

1.Open up the List Settings page where you want this column.
2.Create column named “Today”. The type doesn’t matter here, so let type as “Single Line of Text” and just click “Ok”.






3.Now create the calculated column where you need to use current date within formula and add “Today” from available columns



Once this is done, we no longer need that our own generated column “Today” so you can delete it (Although when you need to edit formula you will need this again, so either recreate this dummy Today field or make it hidden using code behind. Otherwise SharePoint will put you again on that error screen).
This will trick the SharePoint to use [Today] (current date) as part of formula.

You can apply same trick for the current user i.e. [Me] too. Create a dummy field named “Me” use it in your formula, save the calculated column, and delete that dummy “Me” field.