Rob Garrett - Blogs

Welcome to Rob Garrett - Blogs Sign in | Join | Help
in Search
Google

Software/Technology Discussion

Software and Technology Tid-bits

Adding to SharePoint List from a Web Service

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: }
Share this post: Email it! | bookmark it! | digg it! | reddit!
Published Thursday, June 28, 2007 2:30 PM by Rob Garrett

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Links (6/28/2007) « Steve Pietrek’s SharePoint Stuff said:

PingBack from http://stevepietrekweblog.wordpress.com/2007/06/28/links-6282007/
June 28, 2007 9:44 PM

Leave a Comment

(required) 
(optional)
(required) 
Submit

Blurb


Head Shot
Rob Garrett is a British Expat living in Maryland USA. Rob is a trained software engineer and experienced in Windows .NET development.

Rob enjoys listening to Rock music, posting to blogs, driving in the country with the sunroof open, beer (not in conjunction with country driving) and spending time with his family.

This Blog

Syndication

Powered by Community Server, by Telligent Systems