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.
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.