SharePoint Application Page as Content Type
Create a Content Type
———————–
Open VS 2010
File new project–>SP2010->Content Type
Name it: MyContentTypeProject
Select Sandbox Solution
Select Item as Base Content Type
Default Code of Elements.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”>
<!– Parent ContentType: Item (0x01) –>
<ContentType ID=”0x0100609c323354d34416a02081c618ead23f”
Name=”MyContentTypeProject – ContentType1″
Group=”Custom Content Types”
Description=”My Content Type”
Inherits=”TRUE”
Version=”0″>
<FieldRefs>
</FieldRefs>
</ContentType>
</Elements>
You can change below lines
Inherits=”FALSE”
Overwrite=”TRUE”
Add some field to FieldRefs
Ex:
<FieldRefs>
</FieldRefs>
TO
<FieldRefs>
<FieldRef ID=”{DC964DD1-422F-46C5-B4FC-1A796F67DA6B}” Name=”MyMSContentType”/>
</FieldRefs>
Add below Application page navigation
<XmlDocuments>
<XmlDocument NamespaceURI=”http://schemas.microsoft.com/sharepoint/v3/contenttype/forms/url”>
<FormUrls xmlns=”http://schemas.microsoft.com/sharepoint/v3/contenttype/forms/url”>
<New>_layouts/MyApp/MyAppNew.aspx</New>
<Edit>_layouts/MyApp/MyAppEdit.aspx</Edit>
<Display>_layouts/MyApp/MyAppDis.aspx</Display>
</FormUrls>
</XmlDocument>
</XmlDocuments>
So the final Code looks like below.
<?xml version=”1.0″ encoding=”utf-8″?>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”>
<!– Parent ContentType: Item (0x01) –>
<ContentType ID=”0x0100609c323354d34416a02081c618ead23f”
Name=”MyContentTypeProject – ContentType1″
Group=”Custom Content Types”
Description=”My Content Type”
Inherits=”FALSE”
Overwrite=”TRUE”
Version=”0″>
<FieldRefs>
<FieldRef ID=”{DC964DD1-422F-46C5-B4FC-1A796F67DA6B}” Name=”MyMSContentType”/>
</FieldRefs>
<XmlDocuments>
<XmlDocument NamespaceURI=”http://schemas.microsoft.com/sharepoint/v3/contenttype/forms/url”>
<FormUrls xmlns=”http://schemas.microsoft.com/sharepoint/v3/contenttype/forms/url”>
<New>_layouts/MyApp/MyAppNew.aspx</New>
<Edit>_layouts/MyApp/MyAppEdit.aspx</Edit>
<Display>_layouts/MyApp/MyAppDis.aspx</Display>
</FormUrls>
</XmlDocument>
</XmlDocuments>
</ContentType>
</Elements>
Deploy the application
Go to sharePoint
http://SERVER:8888/_layouts/mngctype.aspx
Go to above page under – Custom Content Types
You can find your content type.
Now add content type to List.
If you try to click new item using new content type you will get error, since you didn’t add application pages.
Now create application pages.
Open VS 2010àSP2010àSelect Blank Solution
Name it: MyApp
Right Click on MyApp SolutionàAdd new item
Select Application Page. Name it: MyAppNew.aspx
<table>
<tr>
<td>
<asp:Label ID=”lblName” runat=”server” Text=”Name”></asp:Label>
</td>
<td>
<asp:TextBox ID=”txtName” runat=”server”></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID=”lblProjects” runat=”server” Text=”Projects”></asp:Label>
</td>
<td>
<asp:CheckBoxList ID=”cblProjects” runat=”server” RepeatDirection=”Horizontal” RepeatColumns=”4″></asp:CheckBoxList>
</td>
</tr>
<tr>
<td>
<asp:Label ID=”lblEmployee” runat=”server” Text=”Category”></asp:Label>
</td>
<td>
<asp:DropDownList ID=”ddlEmployees” runat=”server”></asp:DropDownList>
</td>
</tr>
</table>
At
<asp:Content ID=”Main” ContentPlaceHolderID=”PlaceHolderMain” runat=”server”>
</asp:Content>
So Final Code will be
<%@ Assembly Name=”$SharePoint.Project.AssemblyFullName$” %>
<%@ Import Namespace=”Microsoft.SharePoint.ApplicationPages” %>
<%@ Register Tagprefix=”SharePoint” Namespace=”Microsoft.SharePoint.WebControls” Assembly=”Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %>
<%@ Register Tagprefix=”Utilities” Namespace=”Microsoft.SharePoint.Utilities” Assembly=”Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %>
<%@ Register Tagprefix=”asp” Namespace=”System.Web.UI” Assembly=”System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35″ %>
<%@ Import Namespace=”Microsoft.SharePoint” %>
<%@ Assembly Name=”Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %>
<%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”MyAppNew.aspx.cs” Inherits=”MyApp.Layouts.MyApp.MyAppNew” DynamicMasterPageFile=”~masterurl/default.master” %>
<asp:Content ID=”PageHead” ContentPlaceHolderID=”PlaceHolderAdditionalPageHead” runat=”server”>
</asp:Content>
<asp:Content ID=”Main” ContentPlaceHolderID=”PlaceHolderMain” runat=”server”>
<table>
<tr>
<td>
<asp:Label ID=”lblName” runat=”server” Text=”Name”></asp:Label>
</td>
<td>
<asp:TextBox ID=”txtName” runat=”server”></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID=”lblProjects” runat=”server” Text=”Projects”></asp:Label>
</td>
<td>
<asp:CheckBoxList ID=”cblProjects” runat=”server” RepeatDirection=”Horizontal” RepeatColumns=”4″></asp:CheckBoxList>
</td>
</tr>
<tr>
<td>
<asp:Label ID=”lblEmployee” runat=”server” Text=”Category”></asp:Label>
</td>
<td>
<asp:DropDownList ID=”ddlEmployees” runat=”server”></asp:DropDownList>
</td>
</tr>
</table>
</asp:Content>
<asp:Content ID=”PageTitle” ContentPlaceHolderID=”PlaceHolderPageTitle” runat=”server”>
Application Page
</asp:Content>
<asp:Content ID=”PageTitleInTitleArea” ContentPlaceHolderID=”PlaceHolderPageTitleInTitleArea” runat=”server” >
My Application Page
</asp:Content>
Open .Cs file
Add below code
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;
using System.Data;
namespace MyApp.Layouts.MyApp
{
public partial class MyAppNew : LayoutsPageBase
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
using (SPSite objSite = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb objWeb = objSite.OpenWeb())
{
try
{
objWeb.AllowUnsafeUpdates = true;
SPList lstProjects = objWeb.Lists[“Projects”];
SPListItemCollection projItems = lstProjects.Items;
DataTable dtProjects = projItems.GetDataTable();
cblProjects.DataSource = dtProjects;
cblProjects.DataTextField = “Title”;
cblProjects.DataValueField = “ID”;
cblProjects.DataBind();
// bind to dropdown
ddlEmployees.DataSource = dtProjects;
ddlEmployees.DataTextField = “Title”;
ddlEmployees.DataValueField = “ID”;
ddlEmployees.DataBind();
objWeb.AllowUnsafeUpdates = false;
}
catch (Exception ex)
{
SPUtility.TransferToErrorPage(ex.Message);
}
}
}
}
}
}
}
Deploy the Application as Farm solution
Now go to list which you have applied Content type.
Click New item using content type, you will get
_layouts/MyApp/MyAppNew.aspx page
Similarly add MyAppEdit.aspx, MyAppDis.aspx page to application page solution.
You can add Save and cancel button to MyAppNew.aspx page
<tr>
<td>
</td>
<td align=”right”>
<asp:Button ID=”btnSave” runat=”server” Text=”Save” OnClick=”btnSave_Click” />
</td>
<td>
</td>
<td align=”left”>
<asp:Button ID=”btnCancel” runat=”server” Text=”Cancel” OnClick=”btnCancel_Click”
CausesValidation=”false” />
</td>
</tr>