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 sudio 2013 and since application pages are deployed to file system, so there will be no sandboxed solution, we can create farm solutions.
In my previous posts we discussed about how to create Web Part page in SharePoint 2013 using visual studio 2013 as well as we also discussed about 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:
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:
Once the item added. You can see the Solution structure, visual studio automatically added a Layouts mapped folder and within that it create a sub folder on the name of the Application Page name. In my example it added a sub folder name as “MyTestApplicationPage”.
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=220.127.116.11, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %>
<%@ Register Tagprefix=”Utilities” Namespace=”Microsoft.SharePoint.Utilities” Assembly=”Microsoft.SharePoint, Version=18.104.22.168, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %>
<%@ Register Tagprefix=”asp” Namespace=”System.Web.UI” Assembly=”System.Web.Extensions, Version=22.214.171.124, Culture=neutral, PublicKeyToken=31bf3856ad364e35″ %>
<%@ Import Namespace=”Microsoft.SharePoint” %>
<%@ Assembly Name=”Microsoft.Web.CommandUI, Version=126.96.36.199, 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 ID=”Main” ContentPlaceHolderID=”PlaceHolderMain” runat=”server”>
<asp:Content ID=”PageTitle” ContentPlaceHolderID=”PlaceHolderPageTitle” runat=”server”>
<asp:Content ID=”PageTitleInTitleArea” ContentPlaceHolderID=”PlaceHolderPageTitleInTitleArea” runat=”server” >
My Application Page
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, like 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 />
<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>
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:
You will be able to see the application page rendered like below:
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)
In the above case if 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 navigate to the accidenied page like below:
Hope this article will be helpful to you.