I've been racking my brain for the last couple of days trying to figure out how to add a list item to a WSS 3/MOSS 2007 list via the object model from an anonymous web service. At first I ran into access permissions, as one might expect, which led me down a path of setting permissions on the list, all to no avail.
The following code permits object method calls to run as the application pool user:
1: SPSecurity.RunWithElevatedPrivileges(
2: delegate { // code goes here });
When embedded my code to update the list inside the delegate, the call to Update on the SPListItem throws a InvalidOperationException.
After some painful head scratching, and a lot of Googlin' I finally found this post:
http://www.eggheadcafe.com/software/aspnet/30130724/error-the-web-applicatio.aspx
After implementing the suggested change, my code is as follows:
1: SPSite site = null;
2: try
3: { 4: SPSecurity.RunWithElevatedPrivileges(
5: delegate { site = new SPSite(_authSettings.Settings["FbaPortal"]); }); 6:
7: using (SPWeb web = site.OpenWeb())
8: { 9: web.AllowUnsafeUpdates = true;
10: SPList list = web.Lists["PledgeCount"];
11: SPListItem myNewItem = list.Items.Add();
12: myNewItem["Title"] = email;
13: myNewItem["EmailAddress"] = email;
14: myNewItem["Subscribed"] = subscribed;
15: myNewItem.Update();
16: }
17: }
18: finally
19: { 20: if (null != site)
21: site.Dispose();
22: }