Create an application page in SharePoint 2013 using Visual Studio

How to create application pages in SharePoint 2013 using Visual Studio 2013?
SharePoint deveopment training course

This SharePoint 2013 tutorial explains, what are application pages in SharePoint 2013? How to create an application page in SharePoint 2013 using visual studio 2013/2015/2017.

Unlike Site Pages that are stored in content databases, application pages are stored in the IIS virtual directory (_layouts) folder. There will be no customization for the application pages, they are used for administration purposes.

We can create application pages using visual studio 2013 and since application pages are deployed to the file system, so there will be no sandboxed solution, we can create farm solutions.

Application Pages in SharePoint 2013

An application page is an ASP.NET Web page that is designed for use in a SharePoint Web site. Application pages are a specialized type of ASP.NET page.

The primary difference between an application page and a standard ASP.NET page is that an application page contains content that is merged with a SharePoint master page.

A master page enables application pages to share the same appearance and behavior as other pages on a site.

  • Application pages are very useful in SharePoint.
  • Application pages are shared across all sites on the server farm.
  • They are deployed under C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\TEMPLATE\LAYOUTS folder.
  • They are available through a virtual directory namely _layout.
  • These pages are basically for administrative tasks.
  • By default, the page will use the default.master page.
  • The page is derived from the base class LayoutsPageBase which is derived from Microsoft.SharePoint.WebControls namespace.
  • Application pages cannot be personalized or customized by the end-user because they are defined on the file system.
  • An example of the application page is settings.aspx.

SharePoint 2013 create application page using visual studio 2013

Follow the below steps to create an application page using visual studio 2013 in SharePoint 2013.

Open visual studio 2013 then click on File -> New -> Project. Then in the New Project dialog box, From installed templates go to Templates -> Visual C# -> Office/SharePoint -> SharePoint Solutions. Then choose SharePoint 2013 – Empty Project as shown in the fig below. Make sure you have chosen .NET Framework 4.5 also. Give a name and location for the project.

Then in the next dialog box, provide the local sharepoint site for debugging. And since application pages only support through Farm solution, choose farm solution as shown in the fig below:

create application page in sharepoint 2013 using visual studio
create an application page in sharepoint 2013 using visual studio

Now right click on the project then click on Add -> New Item…

Then in the Add New Item dialog box, choose Application Page (Farm Solution only) template as shown in the fig below:

create an application page in sharepoint 2013
create an application page in sharepoint 2013

Once the item added. You can see the Solution structure, visual studio automatically added a Layouts mapped folder and within that, it creates a subfolder on the name of the Application Page name. In my example, it added a subfolder name as “MyTestApplicationPage”.

sharepoint 2013 create application page
sharepoint 2013 create application page

The other thing you can notice is that it added one .aspx page as well as corresponding .cs file to it. So here in application pages, we can write code (server-side) in it. Also if you look at the aspx code it added various content places like below:

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>

<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35″ %>

<%@ Import Namespace="Microsoft.SharePoint" %>

<%@ Assembly Name="Microsoft.Web.CommandUI, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MyApplicationPage.aspx.cs" Inherits="MyTestApplicationPage.Layouts.MyTestApplicationPage.MyApplicationPage" DynamicMasterPageFile="~masterurl/default.master" %>

<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
</asp:Content>
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
</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>

If you look at the code behind file, you can see the class in inherited from LayoutsPageBase class like below:

public partial class MyApplicationPage : LayoutsPageBase

Application pages derived from LayoutsPageBase and LayoutsPageBase is inherited from UnsecuredLayoutsPageBase which are defined in ft.SharePoint.WebControlsnamespace.

LayoutsPageBase has built-in logic for checking permissions and UnsecuredLayoutsPageBase provides few variables, methods, and properties to configure permission for application pages, as if the application page permits access for anonymous users.

To add some code into the application page, I have added the below code in the .aspx page:

<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server">
My Application Page<br />

Enter Name:

<asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />
<asp:Button ID="btnClickMe" runat="server" Text="Click Me" OnClick="btnClickMe_Click" />
<asp:Label ID="lblName" runat="server" Text=""></asp:Label>
</asp:Content>

.cs code:
Then in the .cs code I am just adding the

protected void btnClickMe_Click(object sender, EventArgs e)
{
lblName.Text = txtName.Text;
}

Now right click on the solution and click on Deploy. This will deploy the solution to the SharePoint server. Once it is deployed successfully, you will be able to see the application page by visiting URL like below:

http://win-pfcp2dgt8di/sites/EnjoySharePoint/_layouts/15/MyTestApplicationPage/MyApplicationPage.aspx

You will be able to see the application page rendered like below:

sharepoint 2013 create application page using visual studio
sharepoint 2013 create application page using visual studio

Any user who has view application page access can see the page, so any user having read access also will be able to see the application page. If you want to secure something based on a group you can use the code like below:

protected void Page_Load(object sender, EventArgs e)
{
using (SPWeb site = SPContext.Current.Web)
{
if (site.DoesUserHavePermissions(SPBasePermissions.ManageWeb))
{
}
else
{
Response.Redirect("/_layouts/accessdenied.aspx");
}
}
}

In the above case if a user does not have ManageWeb will not be able to see the page, rather they will be redirected to the AccessDenied page. Here I have tested with one user having read access and that user navigates to the accident page like below:

sharepoint 2013 create application page using visual studio 2013
sharepoint 2013 create application page using visual studio 2013

You may like following SharePoint visual studio tutorials:

Hope this SharePoint tutorial explains, how to create an application page in SharePoint 2013 using visual studio 2013.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →