设为首页 收藏本站
查看: 791|回复: 0

[经验分享] SharePoint中用不存在的"对象名"获取"对象"时的异常处理

[复制链接]

尚未签到

发表于 2015-9-25 13:26:14 | 显示全部楼层 |阅读模式
  文章主旨:分辨在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(){}中取创建此对象。【即:保证创建对象的唯一性,并且保证此唯一的对象一定会被创建成功】

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-118743-1-1.html 上篇帖子: sharepoint 数据库 说明 下篇帖子: SharePoint开发学习笔记4——使用aspx自定义表单的工作流(1)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表