Microsoft的WSS(Windows Sharepoint Services)公开了很多用于访问和管理Sharepoint站点的方法,在调用这些方法时可以通过CAML(Collaborative Application Markup Language)进行一些操作。其中Lists.UpdateListItems()方法提供了用于向Sharepoint List增、删、改数据的方法,但是需要通过CAML语句告诉Sharepoint如何更新数据,有关如何使用CAML以及如何编写CAML进行List数据更新,读者可以参考微软的MSDN文档。

http://msdn.microsoft.com/zh-cn/library/websvclists.lists.updatelistitems.aspx

顺便再给出调用Sharepoint站点的Web Service的地址:

http://Sitename/_vit_bin/lists.asmx?op=UpdateListItems

在使用Lists.UpdateListItems方法时,所使用的用于更新数据的CAML类似于下面这样:

<Batch OnError="Continue">
    <Method ID="1" Cmd="New">
        <Field Name="Title">Hello<Field>
        <Field Name="Document">5</Field>
   </Method>
   <Method ID="2" Cmd="New">
        <Field Name="Title" >World</Field>
        <Field Name="Document">5</Field>
   </Method>
</Batch>

也就是说我们可以在同一段CAML中批量操作数据。不过最近在实际应用中遇到了一个问题,那就是当我要更新的记录太多,比如20000行,可能我需要写一段特别长的CAML,这个时候当我们在程序中调用Web Service时WSS会给出这样的错误。

Some part of your SQL statement is nested too deeply. Rewrite the query or break it up into smaller queries.

就是说你所使用的CAML语句太长而被自动截断了。细心观察一下,发现被截断的CAML的前半部分已经成功执行到List中了,而后半部分没有被执行,看来我们需要自己动手来处理这个Bug了。最好的办法就是将过长的CAML分批进行处理,一部分一部分地执行。

 1 /// <summary>
 2         /// Breaks a larg CAML query into smaller batches to avoid the error "Some part of your SQL statement is nested too deeply. Rewrite the query or break it up into smaller queries."
 3         /// </summary>
 4         /// <param name="listService">The SharePoint list service to execute the CAML against.</param>
 5         /// <param name="listName">The name of the list to execute the CAML against.</param>
 6         /// <param name="elementLargeBatch">The CAML batch list of commands to be broken up.</param>
 7         /// <param name="intBatchSize">The size of batches to use.  If unsure use 300, it seems to work fairly well.</param>
 8         /// <returns>Returns the status of each method block posted through the updates parameter and can 
 9         /// be assigned to a System.Xml.XmlNode object.</returns>
10         public static XmlNode UpdateListItems(SqlClrSharePointSynchronizer.Lists.Lists listService, string listName, XmlElement elementLargeBatch, int intBatchSize)
11         {
12             // calculate useful information
13             int intMethodCount = elementLargeBatch.ChildNodes.Count;
14             int intBatchCount = (int)Math.Ceiling((double)intMethodCount / (double)intBatchSize);
15 
16             // prepare xml documents for batches and results
17             XmlDocument xmlDocBatch = new XmlDocument();
18             XmlDocument xmlDocResults = new XmlDocument();
19             XmlElement elementResults = xmlDocResults.CreateElement("Results");
20 
21             try
22             {
23                 // for each batch
24                 for (int intCurrentBatch = 0; intCurrentBatch < intBatchCount; intCurrentBatch++)
25                 {
26                     int intMethodStart = intCurrentBatch * intBatchSize;
27                     int intMethodEnd = Math.Min(intMethodStart + intBatchSize - 1, intMethodCount - 1);
28 
29                     XmlElement elementSmallBatch = CreateBatch(xmlDocBatch);
30 
31                     // for each method in the batch
32                     for (int intCurrentMethod = intMethodStart; intCurrentMethod <= intMethodEnd; intCurrentMethod++)
33                     {
34                         XmlElement element = (XmlElement)elementLargeBatch.ChildNodes[intCurrentMethod];
35                         elementSmallBatch.AppendChild(xmlDocBatch.ImportNode(element, true));
36                     }
37 
38                     // execute the batch
39                     XmlNode nodeBatchResult = listService.UpdateListItems(listName, elementSmallBatch);
40 
41                     // add the results of the batch into the results xml document
42                     foreach (XmlElement elementResult in nodeBatchResult.ChildNodes)
43                     {
44                         elementResults.AppendChild(xmlDocResults.ImportNode(elementResult, true));
45                     }
46 
47                     // clean up
48                     xmlDocBatch.RemoveAll();
49                 }
50             }
51             catch (SoapException ex)
52             {
53                 if (ex.Detail == null)
54                 {
55                     throw;
56                 }
57 
58                 //copy the exception detail into the Message so it will be available to SQL.
59                 throw new SoapException(ex.Detail.InnerText, ex.Code, ex.Actor, ex.Detail, ex);
60             }
61 
62             return (XmlNode)elementResults;
63         }
64 
65 /// <summary>
66         /// Create the batch element. e.g. &lt;Batch OnError="Continue"&gt;&lt;/Batch&gt;
67         /// </summary>
68         /// <param name="xmlDoc">The object of XmlDocument.</param>
69         /// <returns>Return the Batch element.</returns>
70         private static XmlElement CreateBatch(XmlDocument xmlDoc) 
71         {
72             XmlElement elementBatch = xmlDoc.CreateElement("Batch");
73             elementBatch.SetAttribute("OnError", "Continue");
74             return elementBatch;
75         }

我在使用的过程中发现超过600行的数据更新就会出现CAML被截断的情况,所以我干脆将intBatchSize设置为300,超过300行的CAML将会被分批执行。在Web Service中使用CAML经常会遇到这样或那样的问题,查询用的CAML问题更多,不过Microsoft在SP对象中对CAML的支持还是不错的,毕竟是经过封装的,使用起来要顺手许多。

转载于:https://www.cnblogs.com/jaxu/archive/2009/03/20/1417792.html

使用WSS的Lists.UpdateListItems()方法之被截断的CAML相关推荐

  1. 采用Lists.UpdateListItems方法更新列表项各种类型值的写法

    给SharePoint列表更新列表项的做法很多,最常用的就是调用Microsoft.SharePoint.dll中的对象,但是这样的程序只能在MOSS服务器上运行,如果在客户端呢,只能用Lists.U ...

  2. 【Salesforce】快速清除所有测试数据的方法,截断(Truncate)对象

    [Salesforce]快速清除所有测试数据的方法,截断(Truncate)对象 文章目录 [Salesforce]快速清除所有测试数据的方法,截断(Truncate)对象 场景 截断功能简介 使用方 ...

  3. vue项目访问服务器时:WebSocket connection to ‘wss://XXXX/‘的解决方法

    关于WebSocke的介绍:新手入门:websocket 简单来说,WebSocket 为web应用程序客户端和服务端之间(客户端服务端)提供了一种全双工通信机制,报错是因为发送报文的过程出现问题. ...

  4. SharePoint 2013开发入门探索(二)- 列表操作

    我们如何用代码对SharePoint列表做些例如增删改查的操作呢?如果您的程序可以部署到服务器上,就可以使用 服务器对象模型,因为服务器对象模型提供的功能最多,限制最少:否则可能要选择客户对象模型等其 ...

  5. Sharepoint CAML 增删改查 List

    Lists.UpdateListItems 方法 (websvcLists) Windows SharePoint Services 3 Adds, deletes, or updates the s ...

  6. c语言设计思路和有点不足,C语言总结报告

    1.当初你是如何做出选择计算机专业的决定的? 经过一个学期,你的看法改变了么,为什么? 你觉得计算机是你喜欢的领域吗,它是你擅长的领域吗? 为什么? 当初报考计算机专业,是看到计算机专业在当今社会有良 ...

  7. python返回长度值_Python 文件 truncate() 方法(截断返回截取长度)

    概述 Python 文件 truncate() 方法用于截断文件并返回截断的字节长度. 指定长度的话,就从文件的开头开始截断指定长度,其余内容删除:不指定长度的话,就从文件开头开始截断到当前位置,其余 ...

  8. sharepoint Lists Web service 用法

    概述 在sharepoint 项目中,后期做数据迁移时,会用到sharepoint的web service来完成把数据导入sharepoint站点的功能. web service 名称: http:/ ...

  9. writelines是python对文件的写操作方法_Python File writelines() 方法

    概述 writelines() 方法用于向文件中写入一序列的字符串.高佣联盟 www.cgewang.com 这一序列字符串可以是由迭代对象产生的,如一个字符串列表. 换行需要制定换行符 \n. 语法 ...

  10. 复杂人机智能系统功能分配方法综述

    本文来源:人机与认知实验室 摘要:功能分配是复杂人机智能系统设计进程中的重要内容, 它需要应用系统的分析方法, 合理地进行人.机两者的任务分配和科学地设计两者的功能结合.本文分析了国内外功能分配的研究 ...

最新文章

  1. 科学世界的人文关怀:开源科学与人工智能
  2. 博士申请 | 香港中文大学王思博助理教授招收图表示学习方向全奖博士生
  3. Highmaps网页图表教程之下载Highmaps与Highmaps的地图类型
  4. 为何python不好找工作k-给急着找工作的人一些建议,别在被骗了!
  5. JZOJ 5267. 费马点问题
  6. leetcode 341. Flatten Nested List Iterator | 341. 扁平化嵌套列表迭代器(Java)
  7. 添加列属性_css多列属性
  8. matlab字符串固定长度,限制Matlab用户界面编辑框中的字符串长度
  9. 【LeetCode】剑指 Offer 56. 数组中数字出现的次数
  10. 基于JAVA+SpringMVC+Mybatis+MYSQL的客户关系管理系统
  11. codeforces 707D-(DFS+bitset)
  12. 使用PowerShell和T-SQL在多服务器环境中规划SQL Server备份和还原策略
  13. java:IO流学习小结
  14. Android Studio新建工程syncing失败;Android studio Connection timed out: connect
  15. 【板栗糖GIS】如何给文件夹批量重命名
  16. 移动硬盘计算机无法打开硬盘,win10系统电脑无法打开移动硬盘的详细步骤
  17. 计算机蓝屏重启,电脑蓝屏重启,详细教您电脑经常自动蓝屏重启怎么办
  18. java pdf转图片原理_pdf转图片程序(java实现)
  19. Unity AssetBundle的打包 发布 下载与加载
  20. 关于2021年度一级建造师资格考试安徽考区考务工作有关事宜的通知

热门文章

  1. python统计分析-卡方分析和方差分析
  2. 线程学习(生产者消费者问题哲学家吃饭问题)
  3. 爬取贝壳租房信息存储到mongoDB
  4. 使用log4j失误导致系统假死,记录一下
  5. vim的异常退出处理
  6. 早年的一篇关于80286保护模式的文章
  7. noi linux 比赛使用哪个编译器,2020NOI考题及答案
  8. ORA-00932:数据类型不一致:应为NUMBER,但却获得CHAR
  9. lwj_C#_建立一个数学MathTool类包含的方法
  10. IDEA相对路径没有效果的问题