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:
 
Posted on 3-Feb-09 by Jens-Thomas Hönow
0 Comments  |  Trackback Url  |  Link to this post | Bookmark this post with:        
Tags: Deployment, Sharepoint
 

Links to this post

Comments

Name:
URL:
Email:
Comments: