`

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

阅读更多
原帖地址:http://www.cnblogs.com/wsdj-ITtech/archive/2013/06/06/3075184.html

   这里我把从网上搜集到的针对Sharepoint 70-573的有关练习进行系统的解析,整理成一个系列, 分期、分批次共享出来,供大家研究。


    70-573考试注重的是"知道"相关知识点,而到了70-576则注重的是"应用"相关知识点;所以二者各有侧重。


   这里需要事先申明的是:   1. 不要把本系列当成Sharepoint 70-573的应试题库。


                                      2.  Sharepoint学习不是以考证为目的,真正的掌握必须要通过大量工程实践来达到。


                                      3.  但是,通过做练习,可以帮助我们加深对Sharepoint相关知识点的认知与掌握,不失为一个复习与整理Sharepoint知识的好方法。


                                  4.  所有的题目都是英文版的,但不会影响大家对题意的理解。


                        5.  由于解析篇幅限制,每篇文章涉及到的题目不会太多,题目数量也不会一致。


                                  6.  所有的题目都按: 问题,解析,答案,参考 4个部分进行组织


                                  7.  本系列是边收集,边整理,边解析,边发布,边更新; 所以需要一定的时间才能完成。


                                  8. 在本系列积累到一定数量时,我会提供清单目录,但在此之前如果有兴趣,你可以自行归纳整理。


       下面开始我们的进程:


=====================================================================================================


Question 1


You have a helper method named CreateSiteColumn that contains the following code segment.
private static void CreateSiteColumn(SPWeb web, string columnName)

}
You need to add a new site column of type Choice to a SharePoint site by using the helper method.
Which code segment should you include in the helper method?



A. SPField field = new SPFieldChoice(System.web.Lists[0].Fields, columnName);
B. web.Fields.Add(columnName, SPFieldType.Choice, true);
C. web.Lists[0].Fields.Add(columnName, SPFieldType.Choice, True);
D. web.Lists[0].Views[0].ViewFields.Add(columnName);


解析:


  本题的意图是向Sharepoint Site中添加一个新的Site Column。


  Site Columns是Sharepoint网站的一个重要底层结构,它是一类可重用的列定义或模板,可以将其分配给一个或多个 SharePoint 网站的一个或多个列表(List)。一个Site Column是由几个属性定义的,包括名称及其字段类型。例如 Title这个Column,其名称就是Title,字段类型就是Text。


我们即可以通过Sharepoint的管理界面添加新的Site Column,也可以通过Sharepoint Designer添加新的Site Column,还可以通过代码添加,比如使用如下代码


(http://msdn.microsoft.com/zh-cn/library/ms472869.aspx)



using (SPWeb oWebsite = SPContext.Current.Site.AllWebs["MySite"])
{
SPFieldCollection collFields = oWebsite.Lists["MyList"].Fields;
collFields.Add("MyField", Microsoft.SharePoint.SPFieldType.Text,
true);
}


 


 即指定某Sharepoint Site的某个列(List),使用ListFileds属性的Add方法添加Site Column,在添加的方法中可指定新的SiteColumn的名字,类型以及是否必需包含数值。因此在提供的备选答案中,B是正确答案。


选项A:使用方式不对,SPFieldChoice 构造函数的使用定义如下:



SPFieldChoice(SPFieldCollection, String)   Initializes a new instance of the SPFieldChoice class based on the specified field collection and field name.
SPFieldChoice(SPFieldCollection, String, String) Initializes a new instance of the SPFieldChoice class based on the specified field collection, type name, and display name.


 很明显A选项的使用方法与使用参数是不对的,尤其是第二个参数应该是FieldName而非ColumnName。
 如果要向一个List中添加Choice类型的Column则可以使用如下类似的代码:( 思路就是先向指定的List添加一个Choice类型的SiteColumn,再设置这个SiteColumn的备选项)



lstCustomList.Fields.Add("ABC", SPFieldType.Choice, false); //添加一个新的Field
lstCustomList.Update();
SPFieldChoice objChoiceCol = (SPFieldChoice)lstCustomList.Fields["ABC"]; //设置这个新添加Field的属性
string[] strdata = new string[2];
strdata[0] = "Open";
strdata[1] = "Close";
objChoiceCol.Choices.Add(strdata[0]);
objChoiceCol.Choices.Add(strdata[1]);
objChoiceCol.Update();
lstCustomList.Update();


 选项C.  没有这种用法。Lists【】中应该是ListName或Guid, 而不是所谓的索引值。类似的用法参见如下代码



private static void CreateList(string listName)
{
using (var site = new SPSite(SiteUrl))
{
var web = site.RootWeb;

var listId = web.Lists.Add(listName, string.Empty, SPListTemplateType.GenericList);
var list = web.Lists[listId];

list.OnQuickLaunch = true;
list.Update();

var title = list.Fields["Title"];
title.Title = "Name";
title.Update();

var empFieldName = list.Fields.Add("Employee", SPFieldType.Boolean, false);
var rateFieldName = list.Fields.Add("Salary/Rate", SPFieldType.Currency, true);
var bioFieldName = list.Fields.Add("Bio", SPFieldType.Note, false);

var view = list.DefaultView;
view.ViewFields.Add(empFieldName);
view.ViewFields.Add(rateFieldName);
view.ViewFields.Add(bioFieldName);
view.Update();
}


选项D.  也没有这种用法,况且如果要向一个List的视图中新添加Column,也仅限于添加已经存在的Column到此SPViewFieldCollection中。所以本选项是不能实现向List中添加新的Site Column的。而向List中添加新的Column的代码如下:



SPSite oSiteCollection = SPContext.Current.Site;
using(SPWeb oWebsite = oSiteCollection.AllWebs["Website_Name"])
{
SPList oList = oWebsite.Lists["List_Name"];
SPView oView = oList.Views["All Items"];
SPViewFieldCollection collViewFields = oView.ViewFields;
collViewFields.Add("Created");

oView.Update();
}


 所以本题目正确先项应该是B


 =====================================================================================================


Qestion2:


You have a Web application that contains the following code segment.
private void CreatingSPSite()
{
  SPSite siteCollection = null;
try
  {
   siteCollection = new SPSite("http://contoso.com");
  }
  finally
  {
  }
}
You need to prevent the code segment from causing a memory leak.
Which code segment should you add?
A.
if (siteCollection != null)
{
  siteCollection.Close();
}
B.
if (siteCollection != null)
{
  siteCollection.Dispose();
}
C.
siteCollection = null;
D.
siteCollection.WriteLocked = false;


解析:


  这是一个考证关于Sharepoint代码内存泄露的问题,Sharepoint既然是构建于.NET Framework之上的,为什么还存在内存泄露呢?


  这是因为某些Sharepoint的服务器对象(Services Objects) 如: SPSite, SPWeb 虽然是Managed Objects,但出于提高其运行效率的需要,它们内部的实现代码使用了某些UnManaged 的代码,因此,就造成了你需要自己去解决内存泄露的问题。  


   在本题的供选项中,C.D可以明显的排除,需要关注的就是A与B。


   我们知道,Dispose与Close都可以用于释放内存,但Close方法只能用在你先自行创建了SPSite对象,然后再去释放它。如果你是引用(Reference)了一个SPSite对象,你就不能通过Close方法去释放它。  


   所以我们通常推荐使用Dispose方法来代替Close方法,因为在Dispose方法中最终还是调用了Close方法,但它是通过IDisposable接口来实现的,所以.NET Framework的垃圾回收机制会调用 Dispose方法来释放与此对象相关的内存。 因此本题目正确选项应该是B


  相关参考:


   http://msdn.microsoft.com/zh-cn/library/microsoft.sharepoint.spsite.close.aspx


   http://msdn.microsoft.com/zh-cn/library/aa973248(v=office.12).aspx


   =====================================================================================================


 Question 3


You deploy a custom Web Part named WebPart1 to a SharePoint site.
WebPart1 contains the following code segment. (Line numbers are included for reference only.)
01 protected void Page_Load(object sender, EventArgs e)
02 {
03   SPSite site = null;
04   try
05   {
06     SPSite site = new SPSite("http://www.contoso.com/default.aspx");
07     SPWeb web = site.OpenWeb();
08
09     ...
10   }
11   catch
12   {
13
14   }
15   finally
16   {
17
18   }
19 }

After you deploy WebPart1, users report that the pages on the site load slowly.
You retract WebPart1 from the site.
Users report that the pages on the site load without delay. You need to modify the code in WebPart1 to prevent the pages from loading slowly.

What should you do?
A.
Add the following line of code at line 08:
site.ReadOnly = true;
B.
Add the following line of code at line 13:
site.Dispose();
C.
Add the following line of code at line 17:
site.Dispose();
D.
Add the following line of code at line 17:
site.ReadOnly = true;


解析:


   从题意分析,由于加载了一个用户定义的Webpart而使页面呈现缓慢,当然,对于一个Webpart而言可能有许多原因让页面呈现缓慢的,比如:当Webpart涉及到与后台数据库数据交换时,当Webpart与Webservice服务交互信息时……等等.


  而从本题目提供的代码中,仅涉及到了一个SPSite对象的创建,所以题目本身就把Webpart呈现缓慢的原因约束于SPSite对象上。


  对于A.D是把Site Collection设置只读,即:把Site Collection锁定,不再进行任何写入操作。此类操作并不影响页面的呈现,因为页面的呈现本来就是个读取的过程。


 所以问题归结到SPSite对象所引起的内存泄漏上,解决此问题要通过Dispose方法实现,问题是Dispose方法的解决时机,显然,此方法应该放在Finally代码区,因为此区不管你整体代码是否出现异常都会最执行到这里,而如果仅放在Catch区,则当上面的代码段并无异常时,由SPSite引起的内存泄漏还是没有解决,因为此内存泄漏并不属于Error范畴,所以不会跳到Catch区进行解决。


 所以本题目正确先项应该是C    

本文链接

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics