How to: Avoid Page Validation Errors using SharePoint APIs in Web Applications

Applies to: SharePoint Foundation 2010

Web applications in SharePoint Foundation run under the Application Pool identity. For security reasons, most of the SharePoint Foundation APIs have restrictions when they are accessed from a Web application. Therefore, these APIs can throw a variety of page validation errors. To avoid this problem, update your Web application as described below.

Updating Your Application

To update your Web application to avoid page validation errors, perform the following steps:

1. Run the Web application in the SharePoint context

To do this, create a virtual directory inside the _layouts directory of any SharePoint site and host the Web application under it.

2. Ensure consolidation of application pool

Make sure that the Web application is running under the same application pool as the SharePoint Central Administration. This ensures that the SharePoint Foundation administration functionalities such as Content Deployment, User Profile Management, and others are accessed through the Central Administration application pool identity.

3. Add a Form Digest control to your ASPX page

Most of the APIs can be accessed through the Web application after completing steps one and two above. However, some SharePoint Foundation APIs and functionalities may require even greater security validation. If you still get a security validation error after implementing the above steps, you must add a SharePoint Foundation Form Digest Web control in your ASPX page. The Form Digest control validates the Web application when it attempts to access the SharePoint Foundation APIs. Therefore, the Web application is considered a secure application and can access SharePoint Foundation. To use the Form Digest Web control you must register a tag prefix for SharePoint Foundation and then add the Form Digest control in the ASPX page.

Following is a code sample that demonstrates how to register the tag prefix:.

<%@ Register Tagprefix="SharePoint" 
   Namespace="Microsoft.SharePoint.WebControls" 
   Assembly="Microsoft.SharePoint 
   Version=14.0.0.0
   Culture=neutral, 
   PublicKeyToken=71e9bce111e9429c" %>

Now, add the SharePoint Foundation Form Digest control to the page:

<SharePoint:FormDigest ID="fd" runat="server" PageUri=""/>

You must set the PageURI property on the Form Digest control to the page where it is located. If you do not do this, you will receive a null object reference error.