2

I have created SharePoint Custom Application page.

I'm trying to make that application page anonymous.

So it can be viewed by any user.

For that I have did Below Workaround

  • I have make web application anonymous access SharePoint 2013.
  • Used unsecuredlayoutspagebase to allow anonymous access
  • I have used RunWithElevatedPrivileges in code behind.
  • Overriden AllowAnonymousAccess to always return true

Still it will a ask for credentials.

Please Check My Code

    using System;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;
    using System.Web;
    using System.IO;


    namespace Icalendar.Layouts.Icalendar
    {
        public partial class Icalendarfeed : UnsecuredLayoutsPageBase
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {


                using (SPSite site = new SPSite(SPContext.Current.Web.Url))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList calendarList = web.Lists["Events"];

                        SPQuery query = new SPQuery();

                        query.ExpandRecurrence = true;

                        query.Query = "<Where>" +
                                      "<DateRangesOverlap><FieldRef Name=\"EventDate\" />" +
                                      "<FieldRef Name=\"EndDate\" />" +
                                      "<FieldRef Name=\"RecurrenceID\"/>" +
                                      "<Value Type=\"DateTime\"><Month /></Value></DateRangesOverlap>" +
                                      "</Where>";

                        query.CalendarDate = new DateTime(DateTime.Now.Year,DateTime.Now.Month, 1);

                        SPListItemCollection calendarItems = calendarList.GetItems(query);
                        //string now = DateTime.Now.ToUniversalTime().ToString();
                        string now = Microsoft.SharePoint.Utilities.SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Now); ;
                        System.Text.StringBuilder sw = new System.Text.StringBuilder();
                        sw.AppendLine("BEGIN:VCALENDAR");
                        sw.AppendLine("PRODID:-//Microsoft Corporation//SharePoint MIMEDIR//EN");
                        sw.AppendLine("VERSION:2.0");
                        sw.AppendLine("METHOD:PUBLISH");


                        foreach (SPListItem item in calendarItems)
                        {

                           // Console.WriteLine(item["Title"] + ": starts " + item["EventDate"].ToString() + " and ends " + item["EndDate"].ToString());
                            sw.AppendLine("BEGIN:VEVENT");
                            sw.AppendLine("UID;TYPE=SharePoint:" + item.ID);
                            sw.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmss}", Convert.ToDateTime(item["EventDate"])) + "Z");
                            sw.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmss}", Convert.ToDateTime(item["EndDate"])) + "Z");
                            sw.AppendLine("LOCATION:" + Convert.ToString(item["Location"]));
                            sw.AppendLine("TRANSP:OPAQUE");
                            sw.AppendLine("SEQUENCE:0");
                            sw.AppendLine("SUMMARY;LANGUAGE=en-us:" + Convert.ToString(item["Title"]));
                            sw.AppendLine("DTSTAMP:" + string.Format("{0:yyyyMMddTHHmmss}", Convert.ToDateTime(now)) + "Z");
                            sw.AppendLine("DESCRIPTION: " + item["Description"]);
                            sw.AppendLine("CLASS:PUBLIC");
                          //  sw.AppendLine(string.Format("CREATED:{0:yyyyMMddTHHmmss}", DateTime.UtcNow));

                            sw.AppendLine("END:VEVENT");

                        }
                        sw.AppendLine("END:VCALENDAR");

                        string FileName = "Event.ics";


                        Response.Clear();
                        Response.ContentType = "text/calendar";
                        Response.AddHeader("content-disposition", "inline; filename=" + FileName);
                        Response.Write(sw.ToString());
                        Response.End();



                    }
                }

                });


            }
        }
    }
  • 1
    Check this once c-sharpcorner.com/blogs/… – TARUN May 24 '17 at 12:56
  • Have you tried the above link.. ? Any update..? – TARUN May 26 '17 at 7:23
  • 1
    Yes. It works. You can post answer – Akshay Dattatray Nangare May 26 '17 at 7:25
  • 1
    Posted the link as answer – TARUN May 26 '17 at 7:36
  • why the whole code is within SPSecurity.RunWithElevatedPrivileges(delegate() { block ? – Mathematics Jan 23 '18 at 14:13
3

Original post :
Anonymous Access: SharePoint 2013 Custom Application Page

Try the below steps:

Step 1: Open the Central Admin and go to Manage Web Application (under Application Management) .

Step 2: Choose the Web Application you want anonymous access for and click on Authentication Provider .

enter image description here

Click Default,

enter image description here

Default

Step 3: Check Anonymous Access and Click OK.

enter image description here

Access Step 4: Open Visual Studio, create New Sharepoint Project, and add Application Page in it .

Step 5: Now remove DynamicMasterPageFile="~masterurl/default.master" from aspx page .

Ste

Step 6: Change the Code in aspx.cs to UnsecuredLayoutsPageBase as per your requirement below,

public partial class PageName: UnsecuredLayoutsPageBase  
{  
        protected override bool AllowAnonymousAccess   
        {  
            get   
            {  
                return true;  
            }  
        } 
    }

enter image description here

Step 7: Open the Page in Browser (No Login Pop-up will come).

enter image description here

|improve this answer|||||

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.