SharePoint 2010: Enable Anonymous Users to Add Items to a List using the Client Object Model
How to let anonymous users add items to a SharePoint list/library using client-side object model (JavaScript).
Follow the steps listed below:
Enable Anonymous access at the Web Application level
Go to SharePoint Central Administration, Application Management, and Manage Web Applications page. Select the Web Application, Authentication Providers, click on the zone, and select Enable anonymous access.
Now, navigate to the corresponding web site, Site Actions, Site Permissions, and Anonymous Access. Configure what anonymous users can access at the site level
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. Navigate to List Settings, Permissions for this list, click on Stop Inheriting Permissions and click OK. Click on Anonymous Access and select Add Items.
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).
// // Microsoft provides programming examples for illustration only, // without warranty either expressed or implied, including, but not // limited to, the implied warranties of merchantability and/or // fitness for a particular purpose. // // This sample assumes that you are familiar with the programming // language being demonstrated and the tools used to create and debug // procedures. Microsoft support professionals can help explain the // functionality of a particular procedure, but they will not modify // these examples to provide added functionality or construct // procedures to meet your specific needs. If you have limited // programming experience, you may want to contact a Microsoft // Certified Partner or the Microsoft fee-based consulting line at // (800) 936-5200. // // For more information about Microsoft Certified Partners, please // visit the following Microsoft Web site: // https://partner.microsoft.com/global/30000104 // // Allows AddItem operation using anonymous access private static void AllowAnonAccess() { 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 void RemoveAnonAccess() { 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!"); }
Perform an 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
- Creating a List Item Using ECMAScript (JavaScript, JScript)
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:
The item created by the anonymous user: