Share via


ProcessBatchData and New Event Items

I was recently asked to help provide some code that addressed a couple of concerns:  1) how to use the ProcessBatchData method to add new items to a list and 2) how to create the Xml that ProcessBatchData uses to create different types of events, namely all day events and recurring events.  In this post I'm simply going to share the code I wrote, along with a few caveats and things to think about when you're doing this. 

  1. Another part of the scenario (which was from a SharePoint 2007 site that is moving to SharePoint 2010) was having the deletion of an item in one list be used as a key for deleting a number of items in a different list.  While that is easy enough to code, I would suggest in SharePoint 2010 it would be better to set up lookup between the lists that enforces referential integrity.  That allows you to do cascading deletes so when an item in one list is deleted, all the related records in the other list will be deleted for you automatically.
  2. ProcessBatchData is not a panacea for deletes anyways.  It operates more efficiently because it doesn't actually delete items, it just moves them to the recycle bin.
  3. In the code sample below I demonstrate using ProcessBatchData to add one regular, one all day, and one recurring event.  HOWEVER, you need to take the recurring event with a grain of salt because the recurrence pattern XML is only valid for the particular scenario I created.  To get different recurrence patterns (or any other Xml representation of the data), I HIGHLY recommend you use the Lists web service to retrieve items from a calendar list.  The recurrence pattern Xml will be included in the Xml that's returned.

So with those caveats in mind, here's the sample code (crossing fingers that my blogging editor here doesn't completely screw up the formatting).  I wrote and tested this against a SharePoint 2010 server but I'm guessing it will probably work against a SharePoint 2007 server as well.

        protected void AddBtn_Click(object sender, EventArgs e)

        {

            //sample code that uses ProcessBatchData to add several new items

 

            try

            {

                //get a reference to the list

                SPList theList = SPContext.Current.Web.GetList("/Lists/Events");

 

                //create the XML that will be used to add the items

                System.Text.StringBuilder addXml = new System.Text.StringBuilder(4096);

 

                //create the root node of the add Xml instructions

                addXml.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");

 

                //loop through once for each type of event - regular, all day,

                //and recurring

                for (int i = 0; i < 3; i++)

                {

                    //create the commands that indicate we are creating a new item

                    //that needs to be saved

                    addXml.Append("<Method ID=\"A" + i.ToString() + "\"><SetList Scope=\"Request\">" +

                        theList.ID.ToString() + "</SetList>" +

                        "<SetVar Name=\"ID\">New</SetVar>" +

                        "<SetVar Name=\"Cmd\">Save</SetVar>");

 

                    //add values for each of the fields in the event

 

                    //Title

                    addXml.Append("<SetVar Name=\"urn:schemas-microsoft-com:office:office#Title\">Event " + i.ToString() + "</SetVar>");

 

                    //Location

                    addXml.Append("<SetVar Name=\"urn:schemas-microsoft-com:office:office#Location\">At the Park</SetVar>");

 

                    //Event Date - ENCODE YOUR DATES or all your fields may show up empty

                    addXml.Append("<SetVar Name=\"urn:schemas-microsoft-com:office:office#EventDate\">" +

                        SPUtility.CreateISO8601DateTimeFromSystemDateTime(new DateTime(2010,

                            5, 26, 10, 0,0, DateTimeKind.Local)) + "</SetVar>");

                   

                    //End Date - ENCODE YOUR DATES or all your fields may show up empty

                    addXml.Append("<SetVar Name=\"urn:schemas-microsoft-com:office:office#EndDate\">" +

                        SPUtility.CreateISO8601DateTimeFromSystemDateTime(new DateTime(2010,

                        5, 26, 11, 0, 0, DateTimeKind.Local)) + "</SetVar>");

 

                    //Description

                    addXml.Append("<SetVar Name=\"urn:schemas-microsoft-com:office:office#Description\">This is a new event</SetVar>");

 

                    //if this is the second item, make it an all day event

                    if (i == 1)

                    {

                        addXml.Append("<SetVar Name=\"urn:schemas-microsoft-com:office:office#fAllDayEvent\">1</SetVar>");

                        addXml.Append("<SetVar Name=\"urn:schemas-microsoft-com:office:office#Duration\">86340</SetVar>");

                    }

 

                    //if this is the third item, make it a recurring event

                    if (i == 2)

                    {

                        //set the flag indicating we are using a recurrence pattern

                        addXml.Append("<SetVar Name=\"urn:schemas-microsoft-com:office:office#fRecurrence\">1</SetVar>");

                        addXml.Append("<SetVar Name=\"urn:schemas-microsoft-com:office:office#EventType\">1</SetVar>");

 

                        //this recurrence pattern is for every 1 week on Monday and Wednesday

                        //it is going to recur for 10 instances

                        //NOTE: YOU MUST ENCODE THE DATA!

                        //for other recurrence patterns suggest you create them in a list

                        //and use the Lists web service to retrieve the item and examine

                        //the RecurrenceData field

                        addXml.Append("<SetVar Name=\"urn:schemas-microsoft-com:office:office#RecurrenceData\">" +

                        System.Web.HttpUtility.HtmlEncode("<recurrence><rule>" +

                            "<firstDayOfWeek>su</firstDayOfWeek><repeat><weekly mo=\"TRUE\" " +

                            "we=\"TRUE\" weekFrequency=\"1\" /></repeat><repeatInstances>10" +

                            "</repeatInstances></rule></recurrence>") + "</SetVar>");

                    }

 

                    //complete the method for this transaction

                    addXml.Append("</Method>");

                }

 

                //add the closing tag to the add Xml instructions

                addXml.Append("</Batch>");

 

                //run the code to add the items

                SPContext.Current.Web.ProcessBatchData(addXml.ToString());

            }

            catch (Exception ex)

            {

                //add appropriate error handling and reporting here

                Debug.WriteLine("Error: " + ex.Message);

            }

        }

Comments

  • Anonymous
    January 01, 2003
    The comment has been removed