SharePoint - Add Calendar Item thru Web Service in Sharepoint...

Asked By Gaurav Rana on 18-May-09 08:39 AM

Hi Everyone,

I have a requirement which states that i have to add an item into calendar list for sharepoint website with the use of web services which sharepoint provides.

I have achieved the basic functionality which is quite easy to do. But, i want to go in a little bit in detail..

There are three options with the calendar item

1. All Day Event
2. Recurrence
3. Workspace

I want to handle this options also (with the help of web service)

Sample Code is as follows:

//Lists object Initialization
                Lists.Lists lstServices = new Calendar_CRUD_SERVICE.Lists.Lists();

                //Assign Proper URL for Web Services
                lstServices.Url = ConfigurationManager.AppSettings["WebsiteURL"] + "/_vti_bin/Lists.asmx";

                //String Object that will contain the Values of List Item with respected Columns
                string _strBatch = "<Method ID='1' Cmd='New'>" +
                    "<Field Name='Title'>" + _strTitle + "</Field>" +
                    "<Field Name='Location'>" + _strLocation + "</Field>" +
                    "<Field Name='EventDate'>" + _dtmStartTime.ToString("yyyy-MM-ddTHH:mm:ssZ") + "</Field>" +
                    "<Field Name='EndDate'>" + _dtmEndTime.ToString("yyyy-MM-ddTHH:mm:ssZ") + "</Field>" +
                    "<Field Name='Description'>" + _strDescription + "</Field> </Method>";

                // SharePoint Web Serices require authentication
                lstServices.Credentials = System.Net.CredentialCache.DefaultCredentials;

                //Either GUID or list name will work.
                String listName = System.Configuration.ConfigurationManager.AppSettings["CalendarList"];

                // XML Document Object
                XmlDocument xmlDoc = new System.Xml.XmlDocument();
                XmlElement elBatch = xmlDoc.CreateElement("Batch");
                elBatch.SetAttribute("OnError", "Continue");
                elBatch.SetAttribute("ListVersion", "1");

                //Assign List Item Values to XmlNode Object
                elBatch.InnerXml = _strBatch;

                // Update List Items
                XmlNode ndReturn = lstServices.UpdateListItems(listName, elBatch);
                return ndReturn;

Add Calendar Item thru Web Service in Sharepoint - Jeff W replied to Gaurav Rana on 02-Jun-09 03:52 PM

I had a similar request and found that the column needed to set the 'All Day Event' check box is called fAllDayEvent.  So in your case you would just add one more line to your batch element, something like this:

"<Field Name='fAllDayEvent'>" + "1" + "</Field>" +

Haven't yet looked into Recurrence or Workspace.

Add Calendar Item thru Web Service in Sharepoint... - Pierre Gourven replied to Gaurav Rana on 09-Sep-09 03:28 AM

Hi Gaurav,

I tried to do the same thing but without success.
I created the above CAML query:

sBatch = "<Method ID='1' Cmd='New'>"
sBatch += "<Field Name = 'ContentType'>Événement</Field>"
sBatch += "<Field Name = 'Modified'>2009-08-31 14:18:13</Field>"
sBatch += "<Field Name = 'ModerationStatus'>0</Field>"
sBatch += "<Field Name='Title'>TESTPGO</Field>"
sBatch += "<Field Name='EventDate'>2009-09-10 15:00:00</Field>"
sBatch += "<Field Name='BaseName'>3_</Field>"
sBatch += "</Method>"

Dim xmlDoc2 As New System.Xml.XmlDocument()
Dim elBatch As System.Xml.XmlElement = xmlDoc2.CreateElement("Batch")
elBatch.InnerXml = sBatch

/*I call UpdateListItems*/
Dim ndReturn As XmlNode = listService.UpdateListItems("Congés", elBatch)

I get nothing in my calendar and get a 0x81020014 error which means that the query  misses a field.

Have you got an idea of what's wrong with my code?

Thanks a lot


Eureka - Pierre Gourven replied to Pierre Gourven on 09-Sep-09 06:01 AM

Finally got a solution.
I tried to set too many fields.
I just set the fields you did and it's OK.
Problem to get the Recurrence Information - Pierre Gourven replied to Pierre Gourven on 29-Dec-09 05:42 AM
I tried to copy an recurrent event from one calendar to an other but I didn't succeed in.
My event is created but the recurrence information is not taken into account.
Does anyone have an idea?

Here is a part of my code:

            Dim vTitle As String        'Titre de l'événement'
            Dim vLocation As String     'Lieu de l'événement'
            Dim vEventDate As String    'Date de début de l'événement'
            Dim vEndDate As String      'Date de fin de l'événement'
            Dim vDescription As String  'Description de l'événement'
            Dim vfAllDayEvent As String 'Evénement sur toute la journée' 
            Dim vRecurrencedata As String 'Informations de récurrence'
            Dim vfRecurrence As String
            Dim vEventType As String
            Dim vRecurrence As String
            For Each row As XmlNode In rows               
                vTitle = row.Attributes("ows_Title").Value
                vLocation = row.Attributes("ows_Location").Value
                vEventDate = row.Attributes("ows_EventDate").Value
                vEndDate = row.Attributes("ows_EndDate").Value
                If Not row.Attributes("ows_EventType") Is Nothing Then
                    vEventType = row.Attributes("ows_EventType").Value
                End If
                If Not row.Attributes("ows_Recurrence") Is Nothing Then
                    vRecurrence = row.Attributes("ows_Recurrence").Value
                End If
                If Not row.Attributes("ows_Description") Is Nothing Then
                    vDescription = row.Attributes("ows_Description").Value
                End If
                If Not row.Attributes("ows_fAllDayEvent") Is Nothing Then
                    vfAllDayEvent = row.Attributes("ows_fAllDayEvent").Value.ToString
                End If
                If Not row.Attributes("ows_RecurrenceData") Is Nothing Then
                    vRecurrencedata = row.Attributes("ows_RecurrenceData").Value
                End If
                If Not row.Attributes("ows_fRecurrence") Is Nothing Then
                    vfRecurrence = row.Attributes("ows_fRecurrence").Value
                End If
                'Déclaration de la liste cible'
                listService = New Lists(MOSS_Site_URL_Cible)
                ' Renseignement des informations du compte utilisateur windows à utiliser pour se connecter
                listService.Credentials = New NetworkCredential(MOSS_Utilisateur, MOSS_Password, MOSS_Domaine)
                ' XML permettant de traiter les lignes reçues

                sBatch = "<Method ID='1' Cmd='New'>"
                sBatch += "<Field Name = 'Created'>" + DateTime.Today.ToString("yyyy-MM-ddthh:mm:ssz") + "</Field>"
                sBatch += "<Field Name = 'Title'>" + vTitle + "</Field>"
                sBatch += "<Field Name = 'Location'>" + vLocation + "</Field>"
                sBatch += "<Field Name = 'EventDate'>" + vEventDate + "</Field>"
                sBatch += "<Field Name = 'EndDate'>" + vEndDate + "</Field>"
                If Not vDescription Is Nothing Then
                    sBatch += "<Field Name = 'Description'>" + vDescription + "</Field>"
                End If
                If Not vfAllDayEvent Is Nothing Then
                    sBatch += "<Field Name = 'fAllDayEvent'>" + vfAllDayEvent + "</Field>"
                End If
                If Not vfRecurrence Is Nothing Then
                    sBatch += "<Field Name = 'fRecurrence'>" + vfRecurrence + "</Field>"
                End If
                If Not vRecurrence Is Nothing Then
                    sBatch += "<Field Name = 'Recurrence'>" + vRecurrence + "</Field>"
                End If
                If Not vEventType Is Nothing Then
                    sBatch += "<Field Name = 'EventType'>" + vEventType + "</Field>"
                End If
                If Not vRecurrencedata Is Nothing Then
                    sBatch += "<Field Name = 'RecurrenceData'>" + vRecurrencedata + "</Field>"
                End If
                sBatch += "</Method>"
                ' II - Copie des événements dans le calendrier cible de Sharepoint
                ' Création d'un noeud xml de type Batch permettant de mettre à jour les attributs du document avec la méthode
                '  UpdateListItems du web Service SharePoint de classe lists
                Dim xmlDoc2 As New System.Xml.XmlDocument()
                Dim elBatch As System.Xml.XmlElement = xmlDoc2.CreateElement("Batch")
                elBatch.InnerXml = sBatch
                ' Appel de la méthode UpdateListItems du service web MOSS Lists.asmx permettant de mettre à jour les atrtributs d'un fichier SharePoint à partir d'une requête CAML
                Dim ndReturn As XmlNode = listService.UpdateListItems("Cible", elBatch)


andy ring replied to Gaurav Rana on 04-Apr-18 03:44 AM