使用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 任 ...
随机推荐
- Linux启动/停止/重启Mysql数据库的方法——转载
Mysql启动.停止.重启常用命令 a.启动方式1.使用 service 启动:[root@localhost /]# service mysqld start (5.0版本是mysqld)[root ...
- 【软件工程】用map 实现把英语文本文件词和个数打印出来
#include <iostream> #include <fstream> #include <string> #include <map> usin ...
- stm32串口之存储与解析
最近在做一个小项目,需要用stm32串口接受Arduino发送的一个不定长的数据,并且解析数据,执行其中的命令:秉着不在中断中做过多任务的思想,我们将从串口中接受到的字符放到一个数组当中. 定义数组 ...
- .NET Remoting获取配置通道:
接上文: public static string ChannelManagerUrl { get { retu ...
- [转]Excel 取汉字拼音首位
转自:http://jingyan.baidu.com/article/63acb44adca44461fcc17e85.html 转自:http://jingyan.baidu.com/articl ...
- C++奇数魔方阵
理解了算法的思想就很简单了, 首先将1放到第一排的中间,然后向左上角走,当行数超过索引,则将该数放到最后一排接下来的一列中去,当列数超过索引,则放到上一排的第一列中去,若都没有超过索引,但是此时的右上 ...
- Android中ListView异步加载图片错位、重复、闪烁问题分析及解决方案
我们在使用ListView异步加载图片的时候,在快速滑动或者网络不好的情况下,会出现图片错位.重复.闪烁等问题,其实这些问题总结起来就是一个问题,我们需要对这些问题进行ListView的优化. 比如L ...
- 安装window服务
1 使用管理员权限启动命令提示符 2 输入 cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 3 输入installUtil.exe 服务文件所在目录 ...
- Ubuntu 12.04(32位)安装Oracle 11g(32位)
安装过程(主要过程就直接copy别人的教程了)及问题: 1.将系统更新到最新: sudo apt-get updatesudo apt-get dist-upgrade 2. 如果使用的Ubuntu不 ...
- ns3模拟无线Ad hoc 网络通信
Ad hoc网络 Ad hoc网是一种多跳的.无中心的.自组织无线网络,又称为多跳网(Multi-hop Network).无基础设施网(Infrastructureless Network)或自组织 ...