【问题标题】:Create SharePoint CAML recurring calendar item创建 SharePoint CAML 定期日历项目
【发布时间】:2017-06-08 23:37:25
【问题描述】:

我正在使用 Lists.asmx Web 服务连接到我的 SharePoint 2010 服务器。我可以使用下面的 XML 上传一个新的日历项目,但我不知道要添加什么字段,以便它是“每个工作日重复”而不是跨越多天的事件。

<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 sharepoint-2010 caml


    【解决方案1】:

    您需要在事件创建过程中包含两个附加参数 - 一个让 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' 次出现之后或某个日历日期之前。

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

    总结以下 cmets 中讨论的完整 Batch 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>
    

    这会产生:

    【讨论】:

    • 我试过了,但还是不行。我更新了我的问题以显示我发送的确切 XML。 Sharepoint 返回错误 0,即很好,但是当我打开日历项目时,我收到一个红色错误,提示“缺少根元素”,然后是我刚刚创建的事件的 UID。
    • 重复数据必须是 XML 字符串,而不是 XML 对象。
    • 哈哈。好的,现在更近了。将其设置为字符串,现在它可以正确显示在日历视图中。但是,当我单击该项目时,sharepoint 会弹出一个窗口说:项目不存在。它可能已被其他用户删除。
    • 在您更新的示例 XML 中,您似乎已经丢弃/丢失了我的答案中显示的 &lt;repeat&gt; 元素。这是重复 XML 字符串中的一个 require 元素,应该包装您的 &lt;weekly.../&gt; 元素。
    • 另外,根据您执行呼叫的方式,您可能需要将循环 XML 包装在 CDATA 标记 (&lt;![CDATA[&lt;recurrence&gt;...&lt;/recurrence&gt;]]&gt;) 中。不过,我刚刚测试了您的 XML 的一个固定版本,并且能够成功创建一个重复项目。
    【解决方案2】:

    为了方便其他发现此内容的人,这里是创建循环元素所需的完整 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())))));
     } 
    

    【讨论】:

      猜你喜欢
      • 2011-03-02
      • 1970-01-01
      • 1970-01-01
      • 2020-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-06
      相关资源
      最近更新 更多