Deploy Custom List Forms in Sharepoint - Part II

I'm back... So let's get started with the next part of explaining what I did to get custom forms deployable.
 
As written in the first part, I'm using code behind in the custom page. The code is pretty easy as it just will look for a ListFormWebPart object on the SP page and hides it.
The next question that comes up is, when can I check for the ListFormWebPart. It turned out that SP itself is checking every time the page is displayed if the WebPart exists on the page and if not adds it. The first chance in the ASP.NET event cycle after SP adds it is OnInitComplete. So I added the following code there:
 

foreach (WebPart webPart in Main.WebParts)

{

    if (webPart is ListFormWebPart)

    {

        webPart.Hidden = true;

        ((ListFormWebPart)webPart).ControlMode = SPControlMode.Display;

    }

}

 
Let me point out some things here - first, the variable Main. It is defined as a field in my class.
 

protected WebPartZone Main;

 
Main is actually the default name for the WebPartZone in a standard SP form page. Using the ASP.NET control binding, I get access to the web parts there. Btw this zone has to exists otherwise SP will add it automatically to add the ListFormWebPart. So we can be somehow sure, that it will not fail if we access it.
Next, the loop. We simple loop thru all available WebParts until we find the desired one. Once found we cannot delete the WebPart, but hide it.
You might wonder what ControlMode is good for. It actually defines how the ListFormWebPart will display the data fields from the current entry - possible values are New, Edit, Display and Invalid. As we don't want to deal with the WebPart, we set it to display, also to avoid double entries when editing the data.
 
That's the magic trick - nothing more.
 
To wrap it up, in my SP project I created a very simple class that derives from Microsoft.SharePoint.WebPartPages.WebPartPage and acts as a base class for my custom pages. Here the complete code of it:
 

namespace FC.SP.MyApp

{

    public class PageTemplate : WebPartPage

    {

        protected WebPartZone Main;

 

        protected override void OnInitComplete(EventArgs e)

        {

            base.OnInitComplete(e);

            foreach (WebPart webPart in Main.WebParts)

            {

                if (webPart is ListFormWebPart)

                {

                    webPart.Hidden = true;

                    ((ListFormWebPart)webPart).ControlMode = SPControlMode.Display;

                }

            }

        }

    }

}

 
Next post I will talk about the usage of this class in my pages.
 
Links:

Published: Feb-03-09 | 0 Comments | 0 Links to this post

Deploy Custom List Forms in Sharepoint - Part I

I did several SharePoint customizations with the focus on making them deployable. Although SharePoint 2007 is way better than previous versions, this can still be a big challenge, as there are a lot of hard-wired behaviors you have to work around.

 

This also applies to List forms. When using SharePoint Designer, customizing these forms is no problem. But when deploying them together with a custom list, the fun starts.

 

The problem is that SharePoint always needs a List Web Part in the page. If this Web Part is not included SharePoint will add it automatically. In my pages I also needed to use other means to display data from the list, so I had to replace the standard forms with custom forms.

 

When using SharePoint Designer you are able to manually remove this standard Web Part and all is fine from there on.

 

The only way around this problem is to remove the List Web Part when the custom form is displayed – this is done via some code behind. In short, I created a class that will act as my page template and all my custom pages will derive from it.

 

In the next part I will show in detail how this is done. In the end we will have a deployable solution that contains a custom list using custom forms.

 

Here some interesting links for forms customization:

Creating Custom List Forms for Creating, Editing, or Displaying a List Item

Customize the "NewForm.aspx" page for a SharePoint List

 

Deploy Custom List Forms in Sharepoint - Part II


Published: Nov-29-08 | 1 Comment | 0 Links to this post