How to Allow anonymous access to SharePoint application pages


Create the project

In Visual Studio 2010, create a new "Empty SharePoint Project" and name it "MyAnonymousWebPages".
  • Click OK
  • Enter your SharePoint site
  • Select "Deploy as a farm solution"
Adding the custom Pages
Next, we need to map the 'layouts' directory to our project, so our files are deployed
there.
  • Right click on the project name 'MyAnonymousWebPages' > Add > SharePoint "Layouts"
  • Mapped Folder
  • SharePoint will automatically add a folder under 'layouts' with the name of our
  • project. Right click on this folder >Add > New Item > Application Page
  • Name the page "HelloWorld.aspx"
  • Your project should now look like this:


Allowing anonymous access
So far, we haven't done anything special. The steps we've taken is the basic approach
to adding custom application pages. In the following steps, we will inherit from "UnsecuredLayoutsPageBase". This is a base class for application pages that do not require the user to have any permissions.
  • Open up HelloWorld.aspx.cs
  • By default, the page is inherited from "LayoutsPageBase". Change the inheritance
  • from "LayoutsPageBase" to "UnsecuredLayoutsPageBase".
public partial class HelloWorld : UnsecuredLayoutsPageBase
  • Next, we need to override a property of the UnsecuredLayoutsPageBase, to allow the
  • anonymous access. Be sure to change the 'getter' to return true
protected override bool AllowAnonymousAccess { get { return true; } }
  • Add some content to your markup in the 'PlaceHolderMain'
Deploy the solution
  • Right click on your project name, and select 'Deploy'
  • Once deployment is complete, browse to your page by navigating to
http://{yourSharePointUrl}/_layouts/MyAnonymousWebPages/HelloWorld.aspx
  • See your custom page, like below (notice, by default, the ApplicationPage template
  • in Visual Studio is designed to automatically inherit from the master page. You
  • could instead replace the markup with markup from a standard aspx page template).


Comments

Popular posts from this blog

How to remove app from SharePoint 2013/2016

The model backing the context has changed since the database was created. Consider using Code First Migrations to update the database