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

Question 73
You create a Web Part that calls a function named longCall.
You discover that longCall takes a long time to execute. You need to display in the Developer Dashboard how long it takes to execute longCall.
Which code segment should you use?
A. DateTime startTime = DateTime.Now;
longCall();
Trace.Write("Long Call " + DateTime.Now.Subtract(startTime).Seconds);
B. DateTime startTime = DateTime.Now;
longCall();
Trace.TraceWarning("Long Call " + DateTime.Now.Subtract(startTime).Seconds);
C. Monitor.Enter("Long Call");
if (true)
{
  longCall();
}
Monitor.Exit("Long Call");
D. using (SPMonitoredScope monitoredScope = new SPMonitoredScope("Long Call"))
{
  longCall();
}

解析:
这是关于Developer Dashboard(开发人员面板)的题。
开发人员面板是 Microsoft SharePoint Foundation 2010 中引入的仪表框架。此面板在概念上与 ASP.NET 页面跟踪类似,它提供了一些诊断信息,可帮助开发人员或系统管理员解决难以隔离的页面组件问题。
在过去,用于调试因代码中这些实例的额外开销所导致的性能问题的唯一方法是,将调试器附加到代码并监视 SQL Server Profiler 踪迹。利用开发人员面板,开发人员可通过编程方式(通过使用对象模型)或可视方式(通过查看页面输出)来识别此类问题。
虽然统一日志记录服务 (ULS) 日志中记录了性能问题和资源使用率信息,但解释原始数据可能相当耗时。利用开发人员面板,可以将所有相关信息关联在一起,这样便能更轻松地识别这些类型的问题。
开发人员可以捕获如下信息:
开发人员面板包含一个可扩展机制,用于在不同的范围下测量各种性能计数器。在开发人员面板内部,可使用以下性能计数器来监视请求的每个阶段的使用率和资源消耗。
每线程计数器
这些计数器可测量当前请求或计时器作业的值:
• 线程执行时间
• 数量、持续时间、调用堆栈信息和页面生成的每个 SQL Server 查询的查询文本
• 数量、持续时间和每个 WCF 调用的调用堆栈信息
• URL 或计时器作业名称
• 当前用户
• 执行开始时间
• 包含在 SPMonitoredScope 中的代码的前面的任意统计信息(请参阅使用 SPMonitoredScope)
在每个请求或计时器作业结束时,前面的数据将输出到以下两个位置:
• ULS 日志 — 针对指定范围收集的所有统计信息始终记录到 ULS 日志。
• 开发人员面板 — 可从浏览器窗口中查看请求的性能统计信息。

更重要的是,我们还能将开发人员面板与自定义代码一起使用:
通过引入 SPMonitoredScope 类,开发人员可“包装”代码并在屏幕上显示该代码的使用统计信息。可使用此信息标识潜在的失败点或至少可标识未按预期执行的组件。
SPMonitoredScope 使用起来很简单。开发人员只需“包装”要监视的代码段即可。然后,在执行代码时,系统会将测量的统计信息写入 ULS 日志及开发人员仪表板中。这样,开发人员和系统管理员便可立即使用有关组件以及组件所在页面的信息。
下面是包装代码的示例

using (new SPMonitoredScope(“My Scope Name”))
{
   doSomeWork();
}

这正是本题需要的答案,你可以很快地定位到选项D就是答案。
选项A.B  均是试图使用ASP.NET传统的 Trace (追踪) 功能来追踪longCall的执行效率。 问题是,此功能并不是针对Developer Dashboard的。所以可以直接排除。
选项C.采用了Monitor类,而此类是基于.NET Framework提供同步对对象的访问的机制。与Sharepoint平台 Developer Dashboard并无关系。

所以本题目正确选项应该是D
参考:
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.utilities.spmonitoredscope.aspx
http://msdn.microsoft.com/zh-cn/library/bb386420(v=vs.100).aspx


Question 74
You plan to activate the Developer Dashboard.
You create a command line application that contains the following code segment. (Line numbers are included for reference only.)
01 SPWebService cs = SPWebService.ContentService;
02 cs.DeveloperDashboardSettings.DisplayLevel = SPDeveloperDashboardLevel.On;
You execute the application and discover that the Developer Dashboard fails to appear. You need to ensure that the application activates the Developer Dashboard.
What should you do?
A. Add the following line of code at line 03:
cs.Update();
B. Add the following line of code at line 03:
cs.DeveloperDashboardSettings.Update();
C. Change line 02 to the following code segment:
cs.DeveloperDashboardSettings.DisplayLevel = SPDeveloperDashboardLevel.Off;
D. Change line 02 to the following code segment:
cs.DeveloperDashboardSettings.DisplayLevel = SPDeveloperDashboardLevel.OnDemand;

解析:
 本题的大意就是想通过代码激活Developer Dashboard,但结果却看不到Developer Dashboard,想找出原因。
 先看看题干部分的代码都做了些什么工作:
01 SPWebService cs = SPWebService.ContentService; //获取一个SPWebService对象
02 cs.DeveloperDashboardSettings.DisplayLevel = SPDeveloperDashboardLevel.On; 获取SPWebService对象的DeveloperDashboardSettings子对象并设置此子对象的DisplayLevel状态为开启。
查阅MSDN资料可以看到DeveloperDashboardSettings对象的DisplayLevel可以分别设置为Off, On或 On Demand三种状态,分别表示Developer Dashboard是否关闭,开启或根据需要开启(在将开发人员面板设置为 OnDemand 模式时,页面的右上方会显示一个图标。用户可使用该图标来启用和禁用面板)。在这里在,题干的代码设置为开启,即:已经打开了Developer Dashboard。
  于是问题出来了:既然题干已经打开了Developer Dashboard,为什么我们还看不到它呢?
  如果我们想要通过编写代码的方式来打开仪表板,则可以参考下面的一段代码示例。
  (相关开启的文章请参见我的博文: http://www.cnblogs.com/wsdj-ITtech/archive/2011/12/01/2255740.html)

      SPDeveloperDashboardSettings settings =SPWebService.ContentService.DeveloperDashboardSettings;
      settings.DisplayLevel = SPDeveloperDashboardLevel.On;
      settings.Update();

  请注意,该代码需要在一个Web部件上运行。可以使用VS中新的可视化Web部件模板来创建该Web部件。还需要在代码中使用using语句来引用Microsoft.SharePoint.Administration,而是Web部件必须在Central Administration站点中运行,因为开发人员仪表板是场范围的设置。如果试图在普通SharePoint站点上运行该代码,则系统会抛出安全异常,这也是正确的默认行为。更重要的是,在对设置做了更改之后,要使更改在SharePoint中得以持久化,则需要调用Update方法(这就是本题问题所在,因为我们在设置了DisplayLevel为On后,并没有进一步运行Update方法,所以其状态的开启并没有更新到服务器场中的所有服务器上,于是当我们运行上述程序时,还是看不到Developer Dashboard)。
有了上面的描述,我们就知道答案应该是B了
选项A. 保存WebService的状态和更改到服务器场中的所有计算机上。
选项C.D 是分别设置了DisplayLevel的值为Off(开启)与OnDemand(按需开启)状态。
所以本题目正确选项应该是B
参考:
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.spdeveloperdashb
http://www.cnblogs.com/wsdj-ITtech/archive/2011/12/01/2255740.html

Question 75
You have the following event receiver. (Line numbers are included for reference only.)
01 public override void FieldDeleting(SPListEventProperties properties)
02 {
03   base.FieldDeleting(properties);
04  
05   if (properties.FieldName == "Status")
06   {
07    
08    
09   }
10 }
You need to cancel the operation and redirect the user to a custom error page if the name of the deleted field is Status.
Which code segments should you add at lines 07 and 08?
A. 04 properties.ReceiverData = "/_layouts/customErrorPage.aspx";
05 properties.Cancel = true;
B. 04 properties.RedirectUrl = "/_layouts/customErrorPage.aspx";
05 properties.Cancel = true;
C. 04 properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
05 properties.ReceiverData = "/_layouts/customErrorPage.aspx";
D. 04 properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
05 properties.RedirectUrl = "/_layouts/customErrorPage.aspx";

解析:
 本题是要在FieldDeleting 这个Event中判断FieldName的值,如果为”Status”,则取消删除并导向到报错页面。
  先看看Sharepoint事件接收器的相关概念:
事件对于 SharePoint 开发人员来说是非常有价值的工具。当用户执行的某个操作影响 SharePoint 网站时,您可捕获该操作并使用自定义代码对其进行响应。
事件接收器 只是指定 SharePoint 对象上发生触发操作时调用的方法。触发操作包括添加、更新、删除、移动、签入和签出等操作。侦听事件的 SharePoint 对象(即事件接收器宿主)包含网站集、网站、列表和工作流等对象。
本题就是一个针对List的Field的Deleting事件,传入的参数是SPListEventProperties 类对象,这个对象存放的是有关List操作事件的所有属性。我们来看看各选项涉及到的有关属性:
选项A. ReceiverData : 是一个String类型的对象,用于获取关于事件所包含的数据。好像与重定向URL无关。
       Cancel:  获取或设置是否取消当前的Event。此属性是可以用来取消当前的操作的的。但仅此操作将不会有任何错误信息输出。
   
 选项B.  RedirectUrl: SPListEventProperties 类对象没有此属性,所以此选项可以直接排除。
 选项c.  Status:  获取或设置当前俘获的Event的状态值。 此属性值可以用来重定向报错信息到Custom Error页面。 本题就需要此属性来达到重定向页面的目的。但本选项的问题出在ReceiverData属性上,此属性并不是用来设置重定向页面的URL的。
  选项D. 可以完成重定向的目的。但此选项没有看到Cancel事件的操作。一般而言,我们通过如下代码完成Cancel与重定向页面的操作:
 Properties.Cancel = true;
 properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
 properties.RedirectUrl = "/_layouts/CustomErrorPage/SiteCreationError.aspx";
 但在MSDN的有关资料上又找到如下代码

public override void WebMoving(SPWebEventProperties properties)
{
    properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
    properties.RedirectUrl = properties.Web.ServerRelativeUrl +
      "/_layouts/settings.aspx";
}

  即直接通过Status状态设置为CancelWithRedirectUrl也能貌似实现了Cancel并重定向的目的。
 Btw:网上还有人指出,对于上面的代码,如果你使用sharepoint 里的eventhandler,并在里面做了页面跳转,但是发现跳转失败的话,请注意,一定要先设置RedirectUrl,再设置Status。
即三行代码的顺序如下:
Properties.Cancel = true;
 properties.RedirectUrl = "/_layouts/CustomErrorPage/SiteCreationError.aspx"; //先此行
 properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl; //再此行
  但不论怎样,选项D相对于其它选项而言应该是本题的答案。
 
所以本题目正确选项应该是D
参考:
http://msdn.microsoft.com/en-us/library/ff408258.aspx
http://blogs.msdn.com/b/vssharepointtoolsblog/archive/2010/02/15/event-receiver-and-custom-error-page.aspx
http://msdn.microsoft.com/zh-cn/library/gg749858(v=office.14).aspx
http://msdn.microsoft.com/zh-cn/library/gg981880(v=office.14).aspx#UsingEventReceiversInSPFPart2_CancelingAndRedirecting
http://msdn.microsoft.com/en-us/library/gg981880(v=office.14).aspx


Question 76
You are creating an event receiver. The event receiver will have a field named Title and a field named Priority.
You write the following code segment for the event receiver. (Line numbers are included for reference only.)
01 public override void ItemUpdating(SPItemEventProperties prop)
02 {
02   base.ItemUpdating(prop);
03  
04  
05  
06  
07 }
You need to ensure that when the Title field is changed to include the word IMPORTANT, the Priority field is set to URGENT.
Which code segments should you add at lines 03, 04, 05, and 06?
A. 03 if (prop.AfterProperties["vti_title"].ToString().Contains("IMPORTANT"))
04 {
05   prop.AfterProperties["Priority"] = "URGENT";
06 }
B. 03 if (prop.AfterProperties["vti_title"].ToString().Contains("IMPORTANT"))
04 {
05   prop.ListItem["Priority"] = "URGENT";
06 }
C. 03 if (prop.BeforeProperties["vti_title"].ToString().Contains("IMPORTANT"))
04 {
05   prop.AfterProperties["Priority"] = "URGENT";
06 }
D. 03 if (prop.ListItem["Title"].ToString().Contains("IMPORTANT"))
04 {
05   prop.AfterProperties["Priority"] = "URGENT";
06 }

解析:
  本题还是关于Event Receiver的题。本题题意是:对一个Item进行更新,如果其Title这个Field的值要被改成包含有”IMPORTANT”单词的新值,那么,Priority这个Field的值就要被更新成”URGENT”。
重点在SPItemEventProperties类对象的相关属性上。
 AfterProperties: 返回值是一个String/Value键值对形式的Hash表,用以表示在事件发生后(after the event occurred) SPItem 对象的各个Field上的值。 也即:新的值
 BeforeProperties: 返回值是一个String/Value键值对形式的Hash表,用以表示在事件发生前(before the event occurred) SPItem 对象的各个Field上的值。也即:更改前的值.
  prop.ListItem["FieldName"]: 取得的是更改前的值。
  选项A.本题的答案
  选项B.错在它修改的是Item修改前的值,而prop.AfterProperties["Priority"]所代表的新值没有更新,程序的操作最终还是会把prop.AfterProperties["Priority"]所存储的未修改的值写入进去,这样一样,值还是没有发生变化。所以应该把新值赋值给prop.AfterProperties["Priority"],这样在后续处理时,才会最终把这个新值修改进Item。因此,选项B的这种用法会产生问题。
   选项C.D 均是去判断老值是否包含”IMPORT”单词了。不符合题意的逻辑。
所以本题目正确选项应该是A
参考:
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spitemeventproperties.afterproperties.aspx

原文地址:https://www.cnblogs.com/wsdj-ITtech/p/3164236.html