December 12, 2008

How to set custom forms (new, edit and display) for a specific content type by code

Let's imagine a content type e.g. Student and you want to set custom forms for it (new, edit and display) as in the previous example. The custom forms have to be set using the NewFormUrl, EditFormUrl and DisplayFormUrl properties of the SPContentType class.

using (SPSite mySiteCollection = new SPSite("http://myserver/myspsite"))
    using (SPWeb mySite = mySiteCollection.OpenWeb(mySiteCollection.RootWeb.ID))
        SPList studentsList = mySite.Lists["Students"];
        SPContentType studentContentType = studentsList.ContentTypes["Student"];
        string newurl = "_layouts/STUDENT/APPLICATIONPAGES/NewStudent.aspx";
        string editurl = "_layouts/STUDENT/APPLICATIONPAGES/EditStudent.aspx";
        string displayurl = "_layouts/STUDENT/APPLICATIONPAGES/DisplayStudent.aspx";
        studentContentType.EditFormUrl = editurl;
        studentContentType.NewFormUrl = newurl;
        studentContentType.DisplayFormUrl = displayurl;

No comments: