What do you do when you have a list with custom forms (editform.aspx, dispform.aspx and newform.aspx) and you want to add some extra fields on that form.
Ok, so you get SharePoint Designer out and make the modifications to the forms. But how do we deploy this?
First I created a feature with an FeatureActivated Method
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb web = (SPWeb)properties.Feature.Parent;
SPList list = web.Lists[“Project Status Report”];
//Get the Content Type used for this list
SPContentType ctCustom = list.ContentTypes[“MyContentType”];
if (ctCustom == null)
{
throw new NullReferenceException(“Content Type MyContentType not found”);
}
else
{
// Update Edit, Display and New Forms
ctCustom.EditFormUrl = “Lists/MyList/csEditForm.aspx”;
ctCustom.NewFormUrl =“Lists/MyList/csNewForm.aspx”;
ctCustom.DisplayFormUrl =“Lists/MyList/csDispForm.aspx”;
ctCustom.Update(false); //do not update children
}
}
Now all we have to do is add the forms to the library (adding the forms to other locations will give you errors related to the master page file).
In the same feature add the forms to a Forms Folder and update the elements.xml of the feature with the following content. Make sure that you give the forms a name different from the standard editform.aspx, newform.aspx and dispform.aspx. As this will not replace existing forms.
<?xmlversion=“1.0“encoding=“utf-8“ ?>
<Elements xmlns=“http://schemas.microsoft.com/sharepoint/“>
<Module Name=“ListForms“ Url=“Lists/MyList” Path=“Forms“>
<File Url=“csEditForm.aspx” Name=“csEditForm.aspx” Type=“Ghostable” IgnoreIfAlreadyExists=“TRUE“ />
<File Url=“csDispForm.aspx” Name=“csDispForm.aspx” Type=“Ghostable” IgnoreIfAlreadyExists=“TRUE“ />
<File Url=“csNewForm.aspx” Name=“csNewForm.aspx” Type=“Ghostable” IgnoreIfAlreadyExists=“TRUE“ />
</Module>
</Elements>