Friday, November 19, 2010

How to make an _layouts page (application page) in SharePoint anonymous?

Scenario:
How to make an _layouts page (application page) in SharePoint anonymous?

Explanation:
The application pages (custom pages that are created inside the layouts folder) in SharePoint  will ask for authentication even when a user tries to access them from a site that is configured for anonymous access (public facing site).

Resolution:

  • Make sure that the application page is inherited from "Microsoft.SharePoint.WebControls.UnsecuredLayoutsPageBase"
  • Override the AllowAnonymousAccess property and return true
If the application page does not have code-behind, the below snippet will mark the property "AllowAnonymousAccess" to true:

<%@ Page Language="C#" AutoEventWireup="true" Inherits="Microsoft.SharePoint.WebControls.LayoutsPageBase" DynamicMasterPageFile="~masterurl/default.master" %>
<script runat="server" type="text/C#">
protected override bool AllowAnonymousAccess
{  
    get
    {  
        return true;  
    }  
}
</script>

If the application page has code-behind, the below snippet will mark the property "AllowAnonymousAccess" to true:

public class ApplicationPage :  Microsoft.SharePoint.WebControls.UnsecuredLayoutsPageBase
{
  protected override bool AllowAnonymousAccess { get { return true; }
}

Tuesday, November 16, 2010

How to repair corrupt SPWebConfigModification Objects in SharePoint?

Scenario: How to repair corrupt SPWebConfigModification objects?

Explanation: Recently I worked on automating web.config deployments through PowerShell and during that process, I have ended up corrupting the SPWebConfigModification objects several times.

An indication of corrupted SPWebConfigModification objects is the generic exception "Object reference not set to instance of an object" when you try to make any changes to web.config using SPWebConfigModification class from code.

The below steps could be followed to fix the corrupted SPWebConfigModification objects:

Navigate to the table "Objects" in database "SharePoint_Config" and run the below script:

SELECT    Id, ClassId, ParentId, Name, Status,  Version, Properties
FROM        Objects
WHERE    (Name LIKE '%WebConfig%') 

In order to make any updates this table, we will need to disable the triggers on it.
By default,  the table "Objects" has the following triggers attached to it:
  • trigger_OnDeleteObjects
  • trigger_OnTouchObjects


DISABLE TRIGGER [dbo].[trigger_OnDeleteObjects] ON Objects
DISABLE TRIGGER [dbo].[trigger_OnTouchObjects] ON Objects

Perform an IISRESET.

Identify the corrupted row and delete it.

I have also tried to modified the corrupted item but it did not work
out. 

Enable triggers on the Objects table:

ENABLE TRIGGER [dbo].[trigger_OnDeleteObjects] ON Objects
ENABLE TRIGGER [dbo].[trigger_OnTouchObjects] ON Objects

Reference: thekid.me.uk