SharePoint - How can I customize NewForm.aspx in custom feature?

Brian Lyttle picture Brian Lyttle · Mar 1, 2009 · Viewed 9.9k times · Source

I have created a custom list in a SharePoint site and generated a Visual Studio 2008 project using SharePoint Solution Generator. I can package this as a feature and install it. It runs fine on my server.

After testing this out, I've been able to add a custom masterpage to the feature which is deployed to the _catalogs/masterpage folder. Here it is:

<Elements Id="2196306F-3F37-40b5-99CF-42E89B93888A" xmlns="http://schemas.microsoft.com/sharepoint/">
    <Module Name="DefaultMasterPage" Url="_catalogs/masterpage" RootWebOnly="FALSE">
      <File Url="gcbranding.master" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE" />
    </Module>
</Elements>

Now that I have a custom masterpage in my site, I would like to have this used for the creation of new items. But I don't want to have to set the master from SharePoint Designer.

Going back to the generated solution, it has NewForm.aspx etc. with the list schema. How do I...

  1. Customize the form that is displayed for new items, and have it redirect to a thankyou.aspx page rather than showing all the list items?
  2. Set the url to the master page correctly?

I'm lost on point number 1. Do I need to create a custom webpart and embed that in NewForm.aspx?

On point 2 I have made some headway but have run into an issue. If I set the master like this in my NewForm.aspx...

 MasterPageFile="~/masterurl/gcmaster.master"

It will install OK, but when I hit the site I get an error because ~ is not allowed in the URL. If I use _catalogs/masterpage in the directive, it will not find the master because the URL is relative. Only this code seems to work:

MasterPageFile="../../_catalogs/masterpage/gcmaster.master"

What's the best practice way of setting the master page file in SharePoint, when deploying a custom feature/solution?

Answer

Jason picture Jason · Mar 1, 2009

Re Masterpage: I think you want '~masterurl/gcmaster.master'. no "/" between the "~" and "master".

Re NewForm: You can create your own code-behind page for NewForm.aspx, change the Inherits attribute to your own class. I think I would start by having my custom code behind inherit from SharePoint.SharePoint.WebPartPages.WebPartPage, and go from there.