Wednesday 2 October 2013

Change Default Form Per Content Type

By default in SharePoint list, the following things happen:

  • When you click New Item, you will be redirected to /NewForm.aspx page.
  • When you click Edit Item, you will be redirected to /EditForm.aspx page.
  • When you click View Item, you will be redirected to /DispForm.aspx page.

If you don't want to use these default forms and you want to use a different form, you need to change the default form per content type of the SharePoint list. Basically, you can do it by using SharePoint designer but sometimes it is not working that's why I prefer doing it programmatically. And since I'm doing it always, I already created a console application (exe program) in order to change it via code.


The console application requires five input parameters as listed below:
  1. Full URL of the site
  2.          e.g. http://www.xc.com/sites/domain
  3. Name of the SharePoint list
  4.          e.g. Domain Account Request Application
  5. Name of the content type
  6.          e.g. Request
  7. Type of the form that you want to change. (You need to input here either new, edit or display.)
  8.          e.g. new
  9. Name of the different form
  10.          e.g. NewForm2.aspx


Here is the code that I used:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace ChangeDefaultFormPerContentType
{
    class Program
    {
        static void Main(string[] args)
        {

            if (args.Length != 5)
            {
                Console.WriteLine("Incomplete parameter");
                Console.WriteLine("Input the parameters. Format: <full site url> <listName> <content type Name> <Form Type: new | edit | display> <Form Name>");
                Console.WriteLine("e.g. http://www.abc.com/sites/Firewall Perimeter Request new NewForm2.aspx");
                return;
            }

            string siteUrl = args[0];  // "http://www.xc.com/sites/domain"; //
            string listName = args[1]; // "Domain Account Request Application"; //
            string contenType = args[2]; // "Request"; //
            string formType = args[3]; // "new"; //
            string formName = args[4]; // "NewForm2.aspx"; //

            using (SPSite site = new SPSite(siteUrl))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    web.AllowUnsafeUpdates = true;
                    SPList list = web.Lists[listName.Trim()];
                    string sz = list.RootFolder.Url;
                    SPContentType ct = list.ContentTypes[contenType.Trim()];

                    switch (formType.Trim())
                    {
                        case "new":
                            {
                                ct.NewFormUrl = sz + "/" + formName.Trim();
                                ct.Update();
                                list.Update();
                                Console.WriteLine(string.Format("NewFormUrl of {0} content type was updated successfully into {1}", contenType, formName));
                                break;
                            }
                        case "edit":
                            {
                                ct.EditFormUrl = sz + "/" + formName.Trim();
                                ct.Update();
                                list.Update();
                                Console.WriteLine(string.Format("EditFormUrl of {0} content type was updated successfully into {1}", contenType, formName));
                                break;
                            }
                        case "display":
                            {
                                ct.DisplayFormUrl = sz + "/" + formName.Trim();
                                ct.Update();
                                list.Update();
                                Console.WriteLine(string.Format("DisplayFormUrl of {0} content type was updated successfully into {1}", contenType, formName));
                                break;
                            }
                    }


                }
            }

        }
    }
}



No comments:

Post a Comment