Search code examples
sharepointsharepoint-2010caml

Create SharePoint CAML recurring calendar item


I'm using the Lists.asmx webservices to connect to my SharePoint 2010 server. I can upload a new calendar item using the below XML, but I can't figure out what field to add so that it's a "recurring every weekday" instead of an event that spans multiple days.

<Batch OnError="Continue" ListVersion="1" ViewName="">  
  <Method ID="1" Cmd="New">  
    <Field Name="Title">Test Event</Field>  
    <Field Name="EventDate">2018-10-01 00:00:00</Field>  
    <Field Name="EndDate">2018-10-01 23:59:59</Field>
    <Field Name="fAllDayEvent">1</Field>
    <Field Name="fRecurrence">1</Field>
    <Field Name="EventType">1</Field>
    <Field Name="UID">{17ea5230-d1f4-4fe2-acc2-450e839998ee}</Field>
    <Field Name="RecurrenceData"><![CDATA[<recurrence>
  <rule>
    <firstDayOfWeek>su</firstDayOfWeek>
    <repeat>
      <weekly mo="TRUE" tu="TRUE" we="TRUE" th="TRUE" fr="TRUE" weekFrequency="1" />
    </repeat>
    <repeatInstances>10</repeatInstances>
  </rule>
</recurrence>]]></Field>
  </Method>
</Batch>

I'm creating the code as shown in this gist


Solution

  • You'll need to include two additional parameters are part of your event creation - one to let SharePoint know it is a recurring event (fRecurrence, a Boolean value) and another with the actual recurrence pattern (RecurrenceData, an XML string).

    You can read more about the structure of the RecurrenceData XML here: http://thehightechheels.blogspot.com/2012/12/sharepoint-evenet-recurrencedata-xml.html

    There's quite a bit to it so rather than re-stating everything there, I'll share an example of how to create an event that recurs each weekday as requested:

    <recurrence>
        <rule>
            <firstDayOfWeek>su</firstDayOfWeek>
            <repeat>
                <weekly mo="TRUE" tu="TRUE" we="TRUE" th="TRUE" fr="TRUE" weekFrequency="1" />
            </repeat>
            <repeatForever>FALSE</repeatForever>
        </rule>
    </recurrence>
    

    Generally speaking a recurrence rule has 3 parts: the day of the week considered the first day; the pattern for repeating the event, and; when it ends, which can be never, after 'n' occurrences or by a certain calendar date.

    Within the <repeat> section is where you have a lot of flexibility to set the pattern. For your scenario, the use of the <weekly> element combined with the weekFrequency parameter ensures this event will repeat every week. Within that week, we can then set which days of the week are to include the event; by setting Monday through Friday to "TRUE" (mo, tu, we, th, fr) we're telling SharePoint to repeat the event each of those days.

    Summarizing the full Batch XML changes discussed in the comments below, here is the input I've used on my local SP2010 environment:

    <Batch OnError="Return">
        <Method ID="1" Cmd="New">  
            <Field Name="Title">Test Event</Field>  
            <Field Name="EventDate">2018-10-01 00:00:00</Field>  
            <Field Name="EndDate">2018-10-01 23:59:59</Field>
            <Field Name="fAllDayEvent">1</Field>
            <Field Name="fRecurrence">1</Field>
            <Field Name="EventType">1</Field>
            <Field Name="Duration">86340</Field>
            <Field Name="WorkspaceLink">0</Field>
            <Field Name="TimeZone">0</Field>
            <Field Name="UID">{17ea5230-d1f4-4fe2-acc2-450e839998ee}</Field>
            <Field Name="RecurrenceData"><![CDATA[
    <recurrence>
        <rule>
            <firstDayOfWeek>su</firstDayOfWeek>
            <repeat>
                <daily weekday="TRUE" />
            </repeat>
            <repeatInstances>7</repeatInstances>
        </rule>
    </recurrence>]]>
            </Field>
        </Method>
    </Batch>
    

    Which yields this: enter image description here