Sharepoint学习笔记—习题系列--70-573习题解析 -(Q60-Q62)

Question 60
You have a SharePoint site collection that contains 100 subsites.
You plan to create a Web Part. The Web Part will be deployed to each subsite.
You need to ensure that the Web Part retrieves all of the files in the root directory of the current subsite.
You write the following code segment. (Line numbers are included for reference only.)
01 SPSite site = SPContext.Current.Site;
02 SPWeb web = SPContext.Current.Web;
03
Which code segment should you add at line 03?
A. site.AllWebs[1].Files
B. Site.RootWeb.Lists[0].Items;
C. web.Files;
D. web.RootFolder.SubFolders[0].Files ;
web.Users.Add(currentUser.LoginName, currentUser.Email, currentUser.Name, "");


解析:
 本题的目的是想要部署一个Webpart在诸多Subsites上,然后各自获取各个Subsite的根目录下的所有文件。
  问题的核心是本题的哪个选项代表了各个Subsite的要目录下的所有文件。
选项A. site.AllWebs[1].Files : 如果要获取某个SPSite对象中的某个指定的Web,貌似一般不能如此用,而应该是site.AllWebs["Site_Name"]; 而且即使本选项在表达上是正确的,在实现上也只是获取某个指定的Web的Root Directory下的所有文件。
选项B. Site.RootWeb.Lists[0].Items; 此选项也不太对,一般也是使用Site.RootWeb.Lists[“List_Name”]方法来获取某个List。况且本选项也只是获取某个List下的Items,而与Web下的Files无关。
选项C. web.Files; 正是本题的答案。返回的就是当前SPWeb对象的Root Directory中的所有的文件。
选项D. web.RootFolder.SubFolders[0].Files ; //想要返回的是Web中的RootFolder,即根文件夹下的指定子文件夹下的文件。
web.Users.Add(currentUser.LoginName, currentUser.Email, currentUser.Name, "");   //想要添加一个SPUser。 很显然,本选项不符合本题的要求。
所以本题目正确选项应该是C

参考:
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.files.aspx
http://msdn.microsoft.com/en-us/library/ee547496(v=office.14).aspx
http://msdn.microsoft.com/en-us/library/ms412253.aspx

 

Question 61
You have a SharePoint site that has the URL http://contoso.com/hr.
You are creating a new Web Part.
You need to create a reference to the current subsite without having to dispose of any returned objects.
Which code segment should you use?
A. SPSite siteCollection = new SPSite("http://www.contoso.com");
SPWebCollection site = siteCollection.AllWebs;
B. SPSite siteCollection = new SPSite("http://www.contoso.com");
SPWeb site = siteCollection.RootWeb;
C. SPSite site = SPContext.Current.Site;
D. SPWeb site = SPContext.Current.Web;


解析:
  本题的意图是要在一段WebPart的后台代码中引用当前Subsite对象进行相关操作,并无须担心释放此对象的相关资源。如果你看了Question 58,相信对解决本题就不会再有困难了。
  记住如下准则:永远不要去Dispose任何使用SPContext对象获取的资源。因为SPContext对象是由Sharepoint构架管理的,此对象什么时候回收由Sharepoint自身说了算,用不着你人为的干预,而且你的强行干预反而会引起系统的崩溃。由SPContext获取的资源有SPContext.Site, SPContext.Current.Site, SPContext.Web, and SPContext.Current.Web.等等。
  因为本题是针对的Subsite,所以引用的对象应该是SPContext.Current.Web,也即选项D符合本题的要求。
  选项A.B都是人为新创建了SPSite对象,所以此类对象就需要你Dispose它们的资源。
  选项C是引用的SPSite对象而不是SPWeb对象,所以不符合本题要求。
所以本题目正确选项应该是D
 

 

Question 62
You create a Feature named Feature1. Feature1 is activated in a SharePoint site.
You create a Web Part that contains the following code.
SPSite site = new SPSite("http://intranet/site1");
SPWeb web = site.OpenWeb();
SPFeatureDefinition feature = SPFarm.Local.FeatureDefinitions["Feature1"];
You need to modify the Web Part to activate Feature1 in Site1 only.
Which code segment should you add to the Web Part?
A. site.Features.Add(feature.Id);
B. site.WebApplication.WebService.Features.Add(feature.Id);
C. web.Features.Add(feature.Id);
D. web.Site.WebApplication.WebService.Features.Add(feature.Id);

解析:
 本题想做的事情是通过一个Webpart的后台代码实现仅在Site1(这里我理解Site1就是Site Collection)中激活Feature1。
先来看题干的代码做了些什么事情:
1.SPSite site = new SPSite("http://intranet/site1");
   创建了一个SPSite对象(一个Site Collection对象),此对象代表Site1,即获取Site1这个网站集(Site Collection)对象
2.SPWeb web = site.OpenWeb();
  请注意,此句使用的OpenWeb()没有任何参数,这种用法表示返回一个SPWeb,此SPWeb对象与上面SPSite的构建函数中使用的URL(即:http://intranet/site1)相关联。在本题也就是返回Site1的Root Web对象 (我们知道Sharepoint中每一个Site Collection都有一个Top Level的Web,这个Web就是此Site Collection的Root Web)。
3.SPFeatureDefinition feature = SPFarm.Local.FeatureDefinitions["Feature1"];
  获取一个SPFeatureDefinition对象,此对象包含SPFeature对象的一系列定义(如:Feature名,Feature的标识符,Feature的作用范围和Feature的版本)。本题此处就是获取一个当前Farm中的名为Feature1的这个Feature所对应的SPFeatureDefinition对象。
 
   我们在Question 58了解了SPFeatureCollection 对象,这个对象即是功能集合对象。我们可通过使用 SPWebService、SPWebApplication、SPSite 和 SPWeb 对象的 Features 属性来访问 SPFeatureCollection。如果功能出现在集合中,则该功能已在指定的范围内激活。我们可以使用SPFeatureCollection类的Add方法添加一个新的Feature到这个功能集合对象中。所以指定的Feature被Add方法添加到对应的SPFeatureCollection功能集合对象中时,也就意味着此Feature在功能集合对象所对应的范围内被激活了。
  知道了这点,我们就知道如何去寻找答案了:即找到Site1这个Site Collection级别的SPFeatureCollection 对象,然后把我们的Feature1添加到这个功能集合对象中,如上所述,
当Feature1出现在这个功能集合中时,则表明Feature1已在指定的范围内(即本题要求的Site1这个Site Collection级别内)激活了。
   所以来看看各选项:
选项A. site.Features.Add(feature.Id); 把Feature1添加到了Site1这个对象级别的SPFeatureCollection 对象中,所以正是本题的答案。
选项B. site.WebApplication.WebService.Features.Add(feature.Id);是在SPWebService级别上的SPFeatureCollection 对象中添加Feature1,所以不符合” activate Feature1 in Site1 only”的要求。
选项C. web.Features.Add(feature.Id); 是在SPWeb级别上的SPFeatureCollection 对象中添加Feature1,所以也不符合” activate Feature1 in Site1 only”的要求。
选项D. web.Site.WebApplication.WebService.Features.Add(feature.Id); 与选项B一样,也是在是在SPWebService级别上的SPFeatureCollection 对象中添加Feature1

所以本题目正确选项应该是A
参考:
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsite.openweb.aspx
http://msdn.microsoft.com/zh-cn/library/microsoft.sharepoint.administration.spfeaturedefinition.aspx
http://msdn.microsoft.com/zh-cn/library/microsoft.sharepoint.spfeaturecollection.aspx

 

posted @ 2013-07-18 08:29  wsdj  阅读(547)  评论(1编辑  收藏  举报