HOW TO: Allow anonymous users to add items to SharePoint list using client object model

This post is a contribution from Charls Tom Jacob, an engineer with the SharePoint Developer Support team.

This blog is based on a support ticket I handled recently. The requirement was to let anonymous users add items to a SharePoint list/library using client side object model(JavaScript).

Here are the steps needed to implement this.

1. Enable Anonymous access at the Web Application level

Go to SharePoint Central Administration => Application Management, select the Web Application => Authentication Providers, click on the zone, and select Enable anonymous access

clip_image002

2. Now, navigate to the corresponding web site, Site Action => Site Permissions => Anonymous Access.

Configure what anonymous users can access at the site level

clip_image004

3. Break the permission inheritance on the specific list in which you want to allow anonymous users to add items.

Navigate to the list/library settings for the list in which you want to allow Anonymous users to add items:

List Settings => Permissions for this list => Stop Inheriting Permissions, click OK => Anonymous Access

Select Add Items.

clip_image006

4. Enable “AddItem” operation for the client object model for the Web Application. You can run the following code as a console application (equivalent powershell script also should work).

// Allows AddItem operation using anonymous access

private static voidAllowAnonAccess(){

            Console.WriteLine("Enabling Anonymous access....");

            SPWebApplication webApp = SPWebApplication.Lookup(new Uri(webAppUrl));

            webApp.ClientCallableSettings.AnonymousRestrictedTypes.Remove(typeof(Microsoft.SharePoint.SPList), "GetItems");

            webApp.ClientCallableSettings.AnonymousRestrictedTypes.Remove(typeof(Microsoft.SharePoint.SPList), "AddItem");

         webApp.Update();

            Console.WriteLine("Enabled Anonymous access!");  

        } 

// Revokes Add/Get Item operation using anonymous access

        private static voidRemoveAnonAccess(){

            Console.WriteLine("Disabling Anonymous access....");

            SPWebApplication webApp = SPWebApplication.Lookup(new Uri(webAppUrl));

            webApp.ClientCallableSettings.AnonymousRestrictedTypes.Add(typeof(Microsoft.SharePoint.SPList), "GetItems");

            webApp.ClientCallableSettings.AnonymousRestrictedTypes.Add(typeof(Microsoft.SharePoint.SPList), "AddItem");

            webApp.Update();

            Console.WriteLine("Disabled Anonymous access!"); 

        }

Please do IISRESET after running this code

You can make use of the createListItem() and retrieveListItems() method discussed in the following articles to Add/Get items using the anonymous account:

How to: Retrieve List Items Using JavaScript

https://msdn.microsoft.com/en-us/library/hh185007(v=office.14).aspx

Creating a List Item Using ECMAScript (JavaScript, JScript)

https://msdn.microsoft.com/en-us/library/hh185011(v=office.14).aspx

Note: If you are using a custom layouts page to execute the JS Script, make sure it’s accessible to the anonymous users. Inherit the custom page from UnsecuredLayoutsPageBase with AllowAnonymousAccess set to true.

Anonymous layouts page:

clip_image001

Item created by the anonymous user:

clip_image002

 

Sample code for this is available for download.

Happy anonymous access!!

Anonymous_List_Operations.zip