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

10,037

Solution 1

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.

Solution 2

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?

The SIMPLEST answer, is, change the "Add New Item" link to add a source URL for your thank you page. For example, instead of (I had to leave out the http):

www.yoursite.com/Lists/Links/NewForm.aspx

you change it to:

www.yoursite.com/Lists/Links/NewForm.aspx?Source=www.yoursite.com/ThankYou.aspx 

When the user click Submit from the NewForm page, they will be redirected to ThankYou.aspx.

You will probably have to use SharePoint Designer to change the link, however.

Share:
10,037
Brian Lyttle
Author by

Brian Lyttle

Northern Irish expatriate living and working near Philadelphia. I'm an Escalation Engineer at Yammer which is part of the Microsoft Office Division.

Updated on June 05, 2022

Comments

  • Brian Lyttle
    Brian Lyttle almost 2 years

    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?