本文中我们将讨论在使用客户端对象模型时,如何处理异常。将分别针对 .Net 托管客户端和和ECMAScript进行解释。
为了满足对多个服务器请求进行响应的需要,依托于异常机制,在SharePoint 2010中引入一个新类ExceptionHandlingScope。这个类包含了一些方法,用来把代码包装在一个范围内,来对ClientContext实例中的批处理命令中发生的异常进行处理。
让我们看一个例子。该例子会查询一个名为NonExistentList的列表,并更新该列表的描述属性。 当第一次执行此代码,并假设该列表不存在,将会在调用ExecuteQuery时抛出一个异常。这个异常会被捕获,并其接下来会在catch块中创建该列表。
.Net 对象模型
以下是在控制台应用程序中的实现:
01 |
public static void exceptionExample() |
04 |
ExceptionHandlingScope exScope = new ExceptionHandlingScope(ctx); |
05 |
using (exScope.StartScope()) |
07 |
using (exScope.StartTry()) |
10 |
List myList = ctx.Web.Lists.GetByTitle( "NonExistingList" ); |
11 |
myList.Description = "这是一段新的描述" ; |
14 |
using (exScope.StartCatch()) |
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); |
23 |
using (exScope.StartFinally()) |
26 |
List myList = ctx.Web.Lists.GetByTitle( "NonExistingList" ); |
27 |
myList.Description = "这是一段在final块中创建出来的描述" ; |
整个客户端代码块被包在一个ExceptionHandlingScope的StartScope方法内,该对象定义在客户端代码块的顶部,放在所有客户端对象操作之前。
然后,每个逻辑块(比如获取,创建和更新该列表的代码块)分别被包装在其各自的操作范围中(StartTry,StartCatch和StartFinally)。 只有在StartTry代码块中发生一个例外时StartCatch块才会被执行。StartFinally代码块将永远会被执行,不论是否发生异常。
运行后在SharePoint中可以看到如下结果:
![](https://pic002.cnblogs.com/img/sunmoonfire/201008/2010082613562494.png)
在ECMAScript中使用
下面的代码可以放在一个HTML表单Web部件中运行(直接贴在WebPart属性->源编辑器中):
01 |
<script type= "text/ecmascript" language= "ecmascript" > |
04 |
function exceptionExample() |
06 |
clientContext = new SP.ClientContext.get_current(); |
07 |
var exScope = new SP.ExceptionHandlingScope(clientContext); |
08 |
var startScope = exScope.startScope(); |
09 |
var tryScope = exScope.startTry(); |
11 |
var myList = clientContext.get_web().get_lists().getByTitle( "NonExistingList" ); |
12 |
myList.set_description( "这是一段新描述" ); |
16 |
var catchScope = exScope.startCatch(); |
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); |
25 |
var finallyScope = exScope.startFinally(); |
27 |
var myList = clientContext.get_web().get_lists().getByTitle( "NonExistingList" ); |
28 |
myList.set_description( "这是一段在final块中创建的描述" ); |
30 |
finallyScope.dispose(); |
33 |
this .clientContext.executeQueryAsync(Function.createDelegate( this , this .onSucceededCallback),Function.createDelegate( this , this .onFailedCallback)); |
35 |
function onSucceededCallback(sender, args) |
40 |
function onFailedCallback(sender, args) { |
41 |
alert( '请求失败. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace()); |
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