SharePoint 2010客户端对象模型异常处理

 

本文中我们将讨论在使用客户端对象模型时,如何处理异常。将分别针对 .Net 托管客户端和和ECMAScript进行解释。
为了满足对多个服务器请求进行响应的需要,依托于异常机制,在SharePoint 2010中引入一个新类ExceptionHandlingScope。这个类包含了一些方法,用来把代码包装在一个范围内,来对ClientContext实例中的批处理命令中发生的异常进行处理。
让我们看一个例子。该例子会查询一个名为NonExistentList的列表,并更新该列表的描述属性。 当第一次执行此代码,并假设该列表不存在,将会在调用ExecuteQuery时抛出一个异常。这个异常会被捕获,并其接下来会在catch块中创建该列表。

.Net 对象模型 

以下是在控制台应用程序中的实现:
01 public static void exceptionExample()
02 {
03     ClientContext ctx = new ClientContext("http://sp2010u/it");
04     ExceptionHandlingScope exScope = new ExceptionHandlingScope(ctx);
05     using (exScope.StartScope())
06     {
07         using (exScope.StartTry())
08         {
09             //获取列表 NonExistingList 并更新其描述
10             List myList = ctx.Web.Lists.GetByTitle("NonExistingList");
11             myList.Description = "这是一段新的描述";
12             myList.Update();
13         }
14         using (exScope.StartCatch())
15         {
16             // 新建一个名为 NonExistingList的列表
17             ListCreationInformation listCreationInfo = new ListCreationInformation();
18             listCreationInfo.Title = "NonExistingList";
19             listCreationInfo.Description = "在catch块中创建的";
20             listCreationInfo.TemplateType = (int)ListTemplateType.GenericList;
21             List oList = ctx.Web.Lists.Add(listCreationInfo);
22         }
23         using (exScope.StartFinally())
24         {
25             // 更新列表 NonExistingList的描述
26             List myList = ctx.Web.Lists.GetByTitle("NonExistingList");
27             myList.Description = "这是一段在final块中创建出来的描述";
28             myList.Update();
29         }
30     }
31     ctx.ExecuteQuery();
32 }

整个客户端代码块被包在一个ExceptionHandlingScope的StartScope方法内,该对象定义在客户端代码块的顶部,放在所有客户端对象操作之前。

然后,每个逻辑块(比如获取,创建和更新该列表的代码块)分别被包装在其各自的操作范围中(StartTry,StartCatch和StartFinally)。 只有在StartTry代码块中发生一个例外时StartCatch块才会被执行。StartFinally代码块将永远会被执行,不论是否发生异常。

运行后在SharePoint中可以看到如下结果:

在ECMAScript中使用  

下面的代码可以放在一个HTML表单Web部件中运行(直接贴在WebPart属性->源编辑器中):
01 <script type="text/ecmascript" language="ecmascript">
02     var targetWeb;
03     var clientContext;
04     function exceptionExample()
05    {
06     clientContext = new SP.ClientContext.get_current();
07     var exScope = new SP.ExceptionHandlingScope(clientContext);
08     var startScope = exScope.startScope();
09     var tryScope = exScope.startTry();
10     // 获取NonExistingList列表并更新其描述
11     var myList = clientContext.get_web().get_lists().getByTitle("NonExistingList");
12     myList.set_description("这是一段新描述");
13     myList.update();
14     tryScope.dispose();
15       
16     var catchScope = exScope.startCatch();
17     //新建一个NonExistingList列表
18     var listCreationInfo = new SP.ListCreationInformation();
19     listCreationInfo.set_title("NonExistingList");
20     listCreationInfo.set_description("在catch块中创建的描述");
21     listCreationInfo.set_templateType(SP.ListTemplateType.genericList);
22     clientContext.get_web().get_lists().add(listCreationInfo);
23     catchScope.dispose();
24       
25     var finallyScope = exScope.startFinally();
26     // 更新列表NonExistingList的描述
27     var myList = clientContext.get_web().get_lists().getByTitle("NonExistingList");
28     myList.set_description("这是一段在final块中创建的描述");
29     myList.update();
30     finallyScope.dispose();
31     startScope.dispose();
32       
33     this.clientContext.executeQueryAsync(Function.createDelegate(this, this.onSucceededCallback),Function.createDelegate(this, this.onFailedCallback));
34     }
35     function onSucceededCallback(sender, args)
36     {
37         alert("完成!");
38     }
39   
40     function onFailedCallback(sender, args) {
41         alert('请求失败. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
42     }
43 </script>
44 <input id="Button1" type="button" value="运行代码" onclick="exceptionExample()" />

ExceptionHandlingScope在JavaScript中的用法几乎和在C#中相同。因为JavaScript中没有使用构造器,所以您必须手工调用scope.dispose来销毁范围对象。

 

参考资料

Exception handling Client object model sharepoint 2010

来源:http://www.cnblogs.com/Sunmoonfire/archive/2010/08/26/1809026.html

posted @ 2010-09-15 16:23  绿森林  阅读(234)  评论(0编辑  收藏  举报