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

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

In this post, we will discuss how we can create application pages in SharePoint 2013 using visual studio 2013. Unlike Site Pages which are stored in content databases, application pages are stored in IIS virtual directory (_layouts) folder. There will be no customization for the application pages, they are used for administration purpose.

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.

In my previous posts, we discussed how to create Web Part page in SharePoint 2013 using visual studio 2013 as well as we also discussed how to create site pages in SharePoint 2013 using visual studio 2013.

Follow below steps to create application page using visual studio 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 choosen .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:

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

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:

 How to create application pages in SharePoint 2013 using Visual Studio 2013?
How to create application pages in SharePoint 2013 using Visual Studio 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”.

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

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.

LayoutsPageBasehas 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:

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

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 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:

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

Hope this article will be helpful to you.


You May Also like the Following SharePoint Online Tutorials:

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 →