创建 SharePoint CAML 循环日历项目

Create SharePoint CAML recurring calendar item

我正在使用 Lists.asmx 网络服务连接到我的 SharePoint 2010 服务器。我可以使用下面的 XML 上传一个新的日历项目,但我不知道要添加什么字段,所以它是一个 "recurring every weekday" 而不是跨越多天的事件。

<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>

我正在创建代码 gist

您需要在事件创建过程中包含两个附加参数 - 一个让 SharePoint 知道这是一个重复事件(fRecurrence,一个布尔值),另一个具有实际的重复模式(RecurrenceData,一个 XML 字符串).

您可以在此处阅读有关 RecurrenceData 结构的更多信息 XML: http://thehightechheels.blogspot.com/2012/12/sharepoint-evenet-recurrencedata-xml.html

其中包含很多内容,因此我不想在这里重述所有内容,而是分享一个示例,说明如何创建一个按要求在每个工作日重复发生的事件:

<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>

一般来说,重复规则有 3 个部分:一周中的哪一天被认为是第一天;重复事件的模式,以及;它何时结束,这可能永远不会发生在“n”次之后或某个日历日期之前。

<repeat> 部分中,您可以非常灵活地设置模式。对于您的方案,结合使用 <weekly> 元素和 weekFrequency 参数可确保每周重复此事件。在那一周内,我们可以设置一周中的哪几天包含该事件;通过将星期一到星期五设置为 "TRUE"(motuwethfr),我们告诉 SharePoint 重复那些日子里的每一天。

总结下面评论中讨论的完整批次 XML 更改,这是我在本地 SP2010 环境中使用的输入:

<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>

结果如下:

为了让其他人更容易找到它,这里是创建重复元素所需的完整 C# 代码。

 private static XDocument GenerateBatchInsertXML(string title, DateTime start, DateTime end)
 {  
     // http://thehightechheels.blogspot.com/2012/12/sharepoint-evenet-recurrencedata-xml.html
     var recurrence = new XElement("recurrence",
                             new XElement("rule",
                                 new XElement("firstDayOfWeek", "su"),
                                 new XElement("repeat",
                                     new XElement("daily",
                                         new XAttribute("weekday", "TRUE"))),
                                 new XElement("windowEnd", String.Format("{0:o}", end))));

     return new XDocument(
        new XElement("Batch",
         new XAttribute("OnError", "Continue"),
         new XAttribute("DateInUtc", "True"),
         new XElement("Method",
           new XAttribute("ID", "1"),
           new XAttribute("Cmd", "New"),
           new XElement("Field", new XAttribute("Name", "Title"), title),
           new XElement("Field", new XAttribute("Name", "EventDate"), String.Format("{0:o}", start)),
           new XElement("Field", new XAttribute("Name", "EndDate"), String.Format("{0:o}", end)),
           new XElement("Field", new XAttribute("Name", "fAllDayEvent"), "1"),
           new XElement("Field", new XAttribute("Name", "fRecurrence"), "1"),
           new XElement("Field", new XAttribute("Name", "EventType"), "1"),
           new XElement("Field", new XAttribute("Name", "UID"), "{" + Guid.NewGuid() + "}"),
           new XElement("Field", new XAttribute("Name", "TimeZone"), "0"),
           new XElement("Field", new XAttribute("Name", "RecurrenceData"),
             new XCData(recurrence.ToString())))));
 }