I created a simple SharePoint solution and added one application page, in code behind I am not able to access SPContext.Current because it is returning null see below code

using System;
using Microsoft.SharePoint;

namespace MyCompany.Sharepoint.Layouts.MyCompany.Sharepoint
    public partial class Auth : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
            if (SPContext.Current != null)


I am accessing this by this URL


For more details see discussion under @Robert Lindgren's answer SPContext.Current is null in application page


You should use a different base class for the page if you want SPContext on it:

Use Microsoft.SharePoint.WebControls.LayoutsPageBase instead, detailed here

| improve this answer | |
  • I just changed this line 'public partial class Auth : Microsoft.SharePoint.WebControls.LayoutsPageBase' and when I access page with same URL showing '404 FILE NOT FOUND' – Govind Malviya Mar 18 '16 at 13:57
  • Please doublecheck that the file actually is still present in the correct location in 15 hive – Robert Lindgren Mar 18 '16 at 13:58
  • Yes it exists I checked in this location 'C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\LAYOUTS\MyCompany.Sharepoint' – Govind Malviya Mar 18 '16 at 14:01
  • OK, so something is fishy with how it was originally created. Please recreate it following these steps: 1. In Solution Explorer, choose your project. 2. On the menu bar, choose Project, Add New Item. 3. In the Add New Item dialog box, choose the Application Page (Farm Solution Only) template. – Robert Lindgren Mar 18 '16 at 14:14
  • I created new page and facing same issue sharepoint:24357/_layouts/15/MyCompany.Sharepoint/… 404 FILE NOT FOUND – Govind Malviya Mar 18 '16 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.