Tuesday, May 22, 2007

Sharepoint Web services - UpdateLists gotcha

After some success with Sharepoint Web services, adding fields to a list programmatically, I wanted to be able to also update and delete them from the same program.

Microsoft's sample code (which works ok) in both the WSS2 and WSS3 examples shows your being able to add 2 fields using the XML new field node definitions as follows:










HOWEVER if you try to delete or update these new fields you've added you get all sorts of nasty and unhelpful errors back saying that the web service won’t do what you’ve asked it.

After 4 or 5 hours of head scratching I eventually figured out that because the MS examples create fields that are derived from a BaseType ie FromBaseType="TRUE" - you can't update or delete a FromBaseType Field. (At least I can't find a way to do it - and it would seem to be a bad thing to do anyhow!) (link here to SPField.FromBaseType defn.)

So if you want to programmatically add new fields to Sharepoint lists, and you want to update or delete them afterwards DO NOT include the attribute FromBaseType="True".

This is an example of an XML new field node definition which can be edited or deleted programmatically.

No comments: