使用ExceptionHandlingScope进行高效的SharePoint CSOM编程
异常处理
在我们使用SharePoint API的时候,获取某些对象的时候,可能会出异常,那么CSOM如何处理这种情况呢。
我们在获取某个List的时候,代码如下:
using (ClientContext clientContext = new ClientContext("https://cnblogtest.sharepoint.com"))
{
var pasword = new SecureString();
"abc123!@#".ToCharArray().ToList().ForEach(pasword.AppendChar);
clientContext.Credentials = new SharePointOnlineCredentials("test001@cnblogtest.onmicrosoft.com", pasword);//设置权限
var currentWeb = clientContext.Web;
//此API调用时,如果此List在服务器端不存在,会出现异常。
var list = currentWeb.Lists.GetByTitle("Documents Test");
clientContext.Load(list);
clientContext.ExecuteQuery();//执行查询,返回异常
}
如果服务器端出现异常,服务器会把异常的相关信息通过JSON对象返回给CSOM端。这里面,我们截取异常的Response来看一下:

这个JSON对象里面明确的显示了在执行GetListById的时候,返回的异常信息。
当通过currentWeb.Lists.GetByTitle("Documents Test") 获取List的时候,如果服务器端如果不存在这个List,会出现异常。我们在写服务器端的应用程序的时候,只需要try catch就可以了,但是我们知道ClientAPI本身的调用时通过WCF来进行调用的,我们只有在执行了ExecuteQuery之后,才知道服务器端是否出现异常,然后服务器把异常信息包装后返回给CSOM。
这种API的行为会给我们带来一系列的问题。如果我们在List不存在的时候,需要新建一个,如果通过不在CSOM引发异常的方式来执行呢?上面的例子中,如果我们在List不存在时新建一个List,仍然需要一次请求,如何能再一次请求中实现呢?
使用ExceptionHandlingScope来提高CSOM程序的性能
这个类就是我们用于处理服务器端异常的常用类。上面的例子中,我们可以用如下代码来实现。
using (ClientContext clientContext = new ClientContext("https://cnblogtest.sharepoint.com"))
{
var pasword = new SecureString();
"abc123!@#".ToCharArray().ToList().ForEach(pasword.AppendChar);
clientContext.Credentials = new SharePointOnlineCredentials("test001@cnblogtest.onmicrosoft.com", pasword);//设置权限
var currentWeb = clientContext.Web;
var exceptionHandlingScope = new ExceptionHandlingScope(clientContext);
//List list = null;
using (var currentScope = exceptionHandlingScope.StartScope())
{
using (exceptionHandlingScope.StartTry())
{
//此API调用时,如果此List在Server端不存在,会出现异常。
var listGetById = currentWeb.Lists.GetByTitle("Documents Test");
listGetById.Description = "List Get By Id";
listGetById.Update();
}
using (exceptionHandlingScope.StartCatch())
{
ListCreationInformation listCreationInfo = new ListCreationInformation();
listCreationInfo.Title = "Documents Test";
listCreationInfo.TemplateType = (int)ListTemplateType.DocumentLibrary;
listCreationInfo.Description = "List create in catch block";
currentWeb.Lists.Add(listCreationInfo);
}
}
List list = currentWeb.Lists.GetByTitle("Documents Test");
clientContext.Load(list);
clientContext.ExecuteQuery();//执行查询,不会出异常
//Server端是否出现了异常
Console.WriteLine("Server has Exception:" + exceptionHandlingScope.HasException);
//Server端异常信息
Console.WriteLine("Server Error Message:" + exceptionHandlingScope.ErrorMessage);
}
这个类解决了我们上面说的的问题,服务器端在编译的时候,会把ExceptionHandlingScope里面的代码编译成try catch这样的代码,因此我们可以通过一次请求来实现类似try catch这样的逻辑。
上面的代码再已经CSOM编译后的请求报文为:
<Request AddExpandoFieldTypeSuffix="true" SchemaVersion="15.0.0.0" LibraryVersion="15.0.0.0" ApplicationName=".NET Library"
xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009">
<Actions>
<ObjectPath Id="2" ObjectPathId="1" />
<ObjectPath Id="4" ObjectPathId="3" />
<ExceptionHandlingScope Id="5">
<TryScope Id="7">
<ObjectPath Id="10" ObjectPathId="9" />
<ObjectPath Id="12" ObjectPathId="11" />
<ObjectIdentityQuery Id="13" ObjectPathId="11" />
</TryScope>
<CatchScope Id="15">
<ObjectPath Id="18" ObjectPathId="17" />
<ObjectIdentityQuery Id="19" ObjectPathId="17" />
</CatchScope>
</ExceptionHandlingScope>
<Query Id="22" ObjectPathId="11">
<Query SelectAllProperties="true">
<Properties />
</Query>
</Query>
</Actions>
<ObjectPaths>
<StaticProperty Id="1" TypeId="{3747adcd-a3c3-41b9-bfab-4a64dd2f1e0a}" Name="Current" />
<Property Id="3" ParentId="1" Name="Web" />
<Property Id="9" ParentId="3" Name="Lists" />
<Method Id="11" ParentId="9" Name="GetByTitle">
<Parameters>
<Parameter Type="String">Documents Test</Parameter>
</Parameters>
</Method>
<Method Id="17" ParentId="9" Name="Add">
<Parameters>
<Parameter TypeId="{e247b7fc-095e-4ea4-a4c9-c5d373723d8c}">
<Property Name="CustomSchemaXml" Type="Null" />
<Property Name="DataSourceProperties" Type="Dictionary" />
<Property Name="Description" Type="Null" />
<Property Name="DocumentTemplateType" Type="Int32">0</Property>
<Property Name="QuickLaunchOption" Type="Enum">0</Property>
<Property Name="TemplateFeatureId" Type="Guid">{00000000-0000-0000-0000-000000000000}</Property>
<Property Name="TemplateType" Type="Int32">101</Property>
<Property Name="Title" Type="String">Documents Test</Property>
<Property Name="Url" Type="Null" />
</Parameter>
</Parameters>
</Method>
</ObjectPaths>
</Request>
从这个报文,中我们已经可以大致看出CSOM如何表示这种try catch finally的代码块。我们可以看到ExceptionHandlingScope也是和ClientObject一样有Id信息,这样服务器端就可以把对应的代码编译成server端的try catch finally了。
由于CSOM是基于http请求的,因此尽可能的减少请求,对于我们写出高效的程序至关重要。
下一篇文章主要会介绍 ConditionalScope的使用。
使用ExceptionHandlingScope进行高效的SharePoint CSOM编程的更多相关文章
- 使用ConditionalScope进行高效的SharePoint CSOM编程
在上一篇文章中讲述了 ExceptionHandlingScope的使用后,本章主要讲述ConditionalScope的用法. ConditionalScope在设计思路和解决问题上同Excepti ...
- 使用SharePoint CSOM 编写高效的程序
上一篇文章中简单的介绍了使用CSOM进行编程.今天主要讲一下CSOM使用中一些小技巧,可以让你的程序运行的更快. 单独加载某些属性 在上文中的例子,需要返回Web对象信息的时候,我们使用了如下的代码: ...
- 在设置代理的环境下使用SharePoint CSOM
SharePoint 的CSOM都是通过HttpRequest来实现和SharePoint服务器的交互的,那么我们如何设置HttpWebRequest的一些特性呢,如Cookie,WebProxy? ...
- SharePoint 2013的REST编程基础
1. SharePoint 2013对REST编程的支持 自从SharePoint2013开始, SharePoint开始了对REST 编程的支持,这样除了.NET , Silverlight, Po ...
- SharePoint 2013 开发——搜索架构及扩展
博客地址:http://blog.csdn.net/FoxDave SharePoint 2013高度整合了搜索引擎,在一个场中只有一个搜索服务应用程序(SSA).它集成了FAST,只有一个代码库 ...
- SharePoint 入门书籍推荐 转载来源http://www.cnblogs.com/jianyus/p/3513238.html
最近,总有人说刚入门SharePoint,没有好的资料或者电子书,资料推荐大家多看看博客园和CSDN的博客.对于看博客,我一般是两个思路,要么找一个人的从头到尾看一遍,觉得有意义的,就把地址加收藏:或 ...
- 什么是SharePoint?
在聊SharePoint开发之前,有必要说下什么是SharePoint. 在我工作的过程中,经常遇到客户对SharePoint不太了解的情况.有客户说,SharePoint太烂了,DropBox能做到 ...
- SharePoint 入门书籍推荐
最近,总有人说刚入门SharePoint,没有好的资料或者电子书,资料推荐大家多看看博客园和CSDN的博客.对于看博客,我一般是两个思路,要么找一个人的从头到尾看一遍,觉得有意义的,就把地址加收藏:或 ...
- 实现一个基于 SharePoint 2013 的 Timecard 应用(中)
门户视图 随着 Timecard 列表的增多,如何查找和管理这许多的 Timecard 也就成了问题.尤其对于团队经理而言,他除了自己填写的 Timecard,还要审核团队成员的 Timecard 任 ...
随机推荐
- 升级 DNX 和 DNVM
升级命令: dnvm upgrade -u dnvm upgrade -u –runtime CoreCLR -u 表示 unstable(不稳定),不带 -u 表示升级到最新稳定(stable)版本 ...
- iOS相关思考题
1.iOS如何应对APP版本升级,数据结构随之变化? 一般程序app升级时,数据库有可能发生改变,如增加表字段,增加表等. 此时有两种操作: 1 就是毫无留情的把本地旧数据库直接删掉,重新建立新的数据 ...
- ORACLE 数据块dump
1. rdba(Tablespace relative database block address) 是相对数据块地址,是数据所在的地址,rdba可就是rowid 中rfile#+block#. 根 ...
- ELK日志管理
ELK一般由三部分组成:logstash(日志格式化) + elasticsearch(检索) + Kibana(前台报表展示) 官网地址:https://www.elastic.co/ 本人在这用的 ...
- Spring +SpringMVC 实现文件上传功能。。。
要实现Spring +SpringMVC 实现文件上传功能. 第一步:下载 第二步: 新建一个web项目导入Spring 和SpringMVC的jar包(在MyEclipse里有自动生成spring ...
- IQKeyboardManager在某个页面取消键盘上面的Toolbar
使用IQKeyboardManager的时候,如果想在某个页面取消键盘上面的Toolbar,请使用如下方法,按控制器去操作 // 取消IQKeyboardManager Toolbar [[IQKey ...
- 自定义ImageView回调实现手动改变圆环大小
//-----------------自定义MyView继承Imageview------------------------------- package com.bw.yuanhuan; impo ...
- 更改Windows系统时间同步频率【windows 7,windows 8,win10】
Windows系统默认的时间同步间隔是7天,如果我们需要修改同步的时间间隔(同步频率),我们可以通过修改注册表来手动修改它的自动同步间隔以提高同步次数,保证时钟的精度,windows7,Windows ...
- snmp switch traffic交换机带宽
上代码 <?php function getstr1($strall,$str1,$str2,$html_charset='utf-8'){ $i1=mb_strpos($strall,$str ...
- 如何把android中布局文件(.xml)与相关的类(.java)进行关联?
eg:把一个布局文件名为page1.xml与MainActivity.java(工程自动生成)进行 1.在存放使用资源的res文件夹下的layout文件夹内新建一个XML布局文件,如命名为:page1 ...