博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SharePoint中用不存在的"对象名"获取"对象"时的异常处理
阅读量:6975 次
发布时间:2019-06-27

本文共 4991 字,大约阅读时间需要 16 分钟。

文章主旨:分辨在SharePoint的WebApplication, Site, Web, List, Item, File级别下,用不存在的"对象名"获取"对象"时是否马上抛出异常。用形式化的方式表示如下:SPWebService.ContentService.WebApplications["webAppName"],webApp.Sites["siteName"],web.Webs["webName"],  web.Lists["listName"] and list.Items["itemIndex"], folder.Files["SeverRelativeUrl"]

经过自己的测试发现:webApplication, site, web, (即前三个)即使对象名字不存在也不会抛异常,而是在Try{}中继续执行后面语句;List, Item, File(即后三个)如果对象名字不存在,会获得不了该对象,直接抛异常去执行Catch(){}中的语句。

第一部分:测试webApplication, site, web级别。先定义每个级别中对象的数量都为0,然后用不存在的名字去获得对象之后,让其数量加1,如果输出的数量为0,则说明直接抛异常;如果输出的数量是1,则说明没有直接抛异常。请看如下代码:

 

 
1
using
System;
2
 
using
System.Collections.Generic;
3
 
using
System.Linq;
4
using
System.Text;
5
using
Microsoft.SharePoint;
6
using
Microsoft.SharePoint.Administration;
7
8
namespace
MyTesting2
9
{
10
class
Program
11
{
12
static
void
Main(
string
[] args)
13
{
14
//
--SPWebApplication, SPSite and SPWeb don't throw exception.
15
16
SPWebApplicationCollection myWebAppCol
=
SPWebService.ContentService.WebApplications;
17
int
webAppCount
=
0
;
18
try
19
{
20
//
SharePoint - 123456的名字不存在
21
SPWebApplication testWebApp
=
myWebAppCol[
"
SharePoint - 123456
"
];
22
webAppCount
++
;
23
Console.WriteLine(
 
 
"
Program doesn't throw exception. And the count of webApp is: {0}
"
, webAppCount);
24
}
25
catch
26
{
27
Console.WriteLine(
 
 
"
Exception: The WebApplcition doesn't exist. And the count of WebApp is: {0}
"
, webAppCount);
28
}
29
30
31
SPWebApplication mWebApp
=
myWebAppCol[
"
SharePoint - 12345
"
];
32
int
siteCount
=
0
;
33
try
34
{
35
//
MyTeamSite100的名字不存在
36
SPSite testSite
=
mWebApp.Sites[
"
MyTeamSite100
"
];
37
siteCount
++
;
38
Console.WriteLine(
"
Program doesn't throw exception. And the count of site is: {0}
"
, siteCount);
39
}
40
catch
41
{
42
Console.WriteLine(
 
 
"
Exception: The site doesn't exist. And the count of site is: {0}
"
, siteCount);
43
}
44
45
46
SPSite mySite
=
new
SPSite(
"
http://mosstemplate:12345/sites/MyTeamSiteCollection1
"
);
47
SPWeb myRootWeb
=
mySite.RootWeb;
48
SPWeb myWeb
=
myRootWeb.Webs[
"
MyTeamSite1
"
];
49
50
int
webCount
=
0
;
51
52
try
53
{
54
//
NewNewSubWeb的名字不存在
55
SPWeb testSubWeb
=
myWeb.Webs[
"
NewNewSubWeb
"
];
56
webCount
++
;
57
Console.WriteLine(
"
Program doesn't throw exception. And the count of web is: {0}
"
, webCount);
58
}
59
catch
60
{
61
Console.WriteLine(
"
Exception: This wed doesn't exist. And the count of web is: {0}
"
, webCount);
62
}
63
}
64
}
65
66
}
67

 

经过我的测试,输出的结果如下:Program doesn't throw exception. And the count of webApp is: 1

                                          Program doesn't throw exception. And the count of site is: 1

                                          Program doesn't throw exception. And the count of web is: 1

可以看出,虽然用不存在的名字去获取相应的对象,程序也没有直接抛异常,而是继续执行后面的语句。

 

第二部分:测试List, Item, File级别。(判断方式和上面的一样)请看如下代码:

 

 
1
using
System;
2
using
System.Collections.Generic;
3
using
System.Linq;
4
using
System.Text;
5
using
Microsoft.SharePoint;
6
using
Microsoft.SharePoint.Administration;
7
 
8
9
namespace
MyTesting2
10
{
11
class
Program
12
{
13
static
void
Main(
string
[] args)
14
{
15
16
#region
17
//
--SPList, SPItem and SPFile throw exception.
18
19
SPSite mySite
=
new
SPSite(
"
http://mosstemplate:12345/sites/MyTeamSiteCollection1
"
);
20
SPWeb myRootWeb
=
mySite.RootWeb;
21
SPWeb myWeb
=
myRootWeb.Webs[
"
MyTeamSite1
"
];
22
SPList myList
=
myWeb.Lists[
"
Tasks
"
];
23
SPList yourList
=
myWeb.Lists[
"
NewDocLib1
"
];
24
25
int
listCount
=
0
;
26
int
itemCount
=
0
;
27
int
fileCount
=
0
;
28
29
#region
30
try
31
{
32
//
MyNewNewDocLib名字不存在
33
SPList testList
=
myWeb.Lists[
"
MyNewNewDocLib
"
];
34
listCount
++
;
35
Console.WriteLine(
"
Program doesn't throw exception. And the count of list is: {0}
"
, listCount);
36
}
37
catch
38
{
39
Console.WriteLine(
"
Exception: This list doesn't exist. And the count of list is: {0}
"
, listCount);
40
}
41
#endregion
42
43
#region
44
try
45
{
46
//
下标为100的Item不存在
47
SPItem testItem
=
myList.Items[
100
];
48
itemCount
++
;
49
Console.WriteLine(
"
Program doesn't throw exception. And the count of item is: {0}
"
, itemCount);
50
}
51
catch
52
{
53
Console.WriteLine(
"
Exception: This item doesn't exist. And the count of item is: {0}
"
, itemCount);
54
}
55
#endregion
56
57
try
58
{
59
//
myFile名字不存在(即:没有此ServerRelativeUrl)
60
SPFile mFile
=
yourList.RootFolder.Files[
"
sites/MyTeamSiteCollection1/MyTeamSite1/NewDocLib1/myFile
"
];
61
fileCount
++
;
62
Console.WriteLine(
"
Program doesn't throw exception. And the count of file is: {0}
"
, fileCount);
63
}
64
catch
65
{
66
Console.WriteLine(
"
Exception: This file doesn't exist. And the count of file is: {0}
"
, fileCount);
67
}
68
#endregion
69
}
70
}
71
}
72

经过我的测试,输出结果如下:Exception: This list doesn't exist. And the count of list is: 0

 

                                       Exception: This item doesn't exist. And the count of item is: 0

                                       Exception: This file doesn't exist. And the count of file is: 0

可以看出:在此三个级别中,用不存在的名字去获得对象时,程序会直接抛出异常。(Try{}中后面的语句将不会执行)。

 

区分在这几个级别中,用不存在的名字去获取对象时是否直接抛异常的作用是什么呢?

答案是:可以用这种方法去判断,以此为名的对象是否已经存在,如果存在,就不去在此创建此对象;如果不存在,则说明此名字唯一,那么就可以根据是否抛异常,分别在Try{}中或者Catch(){}中取创建此对象。【即:保证创建对象的唯一性,并且保证此唯一的对象一定会被创建成功】

转载地址:http://blesl.baihongyu.com/

你可能感兴趣的文章
《C++代码设计与重用》——2.6 接口一致性
查看>>
《AngularJS高级程序设计》——2.4 小结
查看>>
Spark Streaming + Spark SQL 实现配置化ETL流程
查看>>
算法之冒泡排序
查看>>
袋鼠云成企业服务潜力新星 云市场生态持续发酵
查看>>
synchronized 修饰在 static方法和非static方法的区别
查看>>
如何把握住视频直播的风口,打造安全可靠的视频直播平台
查看>>
jquery.form.js失效问题。
查看>>
jetty 基础
查看>>
码栈开发手册(三)---编码方式开发(高级课程②)
查看>>
Hbase 学习(十) HBase Snapshots
查看>>
记一次8小时惊心动魄的服务器+网站升级
查看>>
too many open files
查看>>
Eclispe清除项目缓存无需删除.metadata文件
查看>>
Fedora14升级到Fedora15问题汇总(原创)
查看>>
Oracle数据库语句大全
查看>>
系统知识点笔记
查看>>
Gradle 2.3 发布
查看>>
数据库内核月报 - 2015 / 10-MySQL · 特性分析 · MySQL权限存储与管理
查看>>
Swift教程_零基础学习Swift完整实例(五)_swift完整实例(构建数据层)
查看>>