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

Qeustion 8
You have a SharePoint site collection. The root Web of the site collection has the URL http://intranet.

You plan to create a user solution that will contain a Web Part. The Web Part will display the title of the root Web.

You write the following code segment for the Web Part. (Line numbers are included for reference only.)

01 SPSite currentSite = new SPSite("http://intranet");
02
03 Label currentTitle = new Label();
04 currentTitle.Text = currentSite.RootWeb.Title;

You add the Web Part to a page in the root Web and receive the following error message: "Web Part Error: Unhandled exception was thrown by the sandboxed code wrapper's Execute method in the partial trust app domain: An unexpected error has occurred."
You need to prevent the error from occurring.
What should you do?
A. Add the following line of code at line 02:
currentSite.OpenWeb();
B. Add the following line of code at line 02:
currentSite.OpenWeb("http://intranet");
C. Change line 01 to the following code segment:
SPSite currentSite = SPContext.Current.Site;
D. Change line 04 to the following code segment:
currentTitle.Text = currentSite.OpenWeb().Title;

解析:
 由报错信息” Unhandled exception was thrown by the sandboxed code wrapper's Execute method in the partial trust app domain: An unexpected error has occurred”可以判断这个错误是由于Sandbox Solution在受限操作方面产生的问题。
  首先来看 A. B选项,它们都使用了OenWeb()方法,此方法是返回一个SPWeb对象,所以选项A, B在语法上都是错误的(左侧应该定义一个SPWeb变量来承接右边的方法返回值)。
  选项C. 有些人认为SPSite对象是不允许在Sandbox solution中使用的,这种认识不全对,实事上,我们可以在Sandbox Solution中使用SPSite对象,但前提是此SPSite对象”必须”位于”当前”的Site Collection中(也即:此Site Collection是位于当前的Context下的),所以,我们只能使用下面的语句来在Sandbox Solution中获取SPSite对象:
  SPSite currentSite = SPContext.Current.Site;
  如果要使用类似于本题目通过URL方式来获取SPSite,则并不能保持你提供的URL与Webpart在同一个Site Collection中,所以就很可能跳出上面的错误。
   选项 D.很明显,如果错误是由于在Sandbox Solution中获取SPSite对象的方法引起的,那么,选项D并不能解决这个错误。
所以本题目正确选项应该是C

参考:

http://social.msdn.microsoft.com/forums/en-US/sharepointgeneralprevious/thread/e84a457e-068c-465d-95f8-db56ac130cda
http://msdn.microsoft.com/en-us/library/ms473155.aspx

 

Question 9
You need to disable the CriticalExceptionCount measure for all user solutions.
You write the following code segment. (Line numbers are included for reference only.)
01 SPUserCodeService userCode = SPUserCodeService.Local;
02 SPResourceMeasureCollection measures = userCode.ResourceMeasures;
03 SPResourceMeasure measure = measures["CriticalExceptionCount"];
04
05 measure.Update();

Which code segment should you add at line 04?
A. measure.AbsoluteLimit = 0;
B. measure.AbsoluteLimit = 1;
C. measure.ResourcesPerPoint = 0;
D. measure.ResourcesPerPoint = 1;

解析:
本题目主要涉及到Sandbox Solution的资源配额管理,SharePoint 2010严格监控沙盒解决方案中的代码的运行状况。每个网站集均受可配置的日常资源点 的最大数目的限制。这些点基于某种专有算法进行累计,该算法会考虑网站集中安装的沙盒解决方案对 14 类资源的使用。当一个网站集超出其允许的最大点数(默认情况下,该数设置为 300)时,该网站集中的所有沙盒解决方案都将终止 (请注意:是此网站集中”所有”的沙盒解决方案),且在剩余时间内再也无法运行。
 
  SharePoint 包含14个方面的指标(Metric或者ResourceMeasure)来设置相应的使用配额
1. AbnormalProcessTerminationCount
2. CPUExecutionTime
3. CriticalExceptionCount
4. InvocationCount
5. PercentProcessorTime
6. ProcessCPUCycles
7. ProcessHandleCount
8. ProcessIOBytes
9. ProcessThreadCount
10. ProcessVirtualBytes
11. SharePointDatabaseQueryCount
12. SharePointDatabaseQueryTime
13. UnhandledExceptionCount
14. UnresponsiveprocessCount

本题提到的就是第3个指标:CriticalExceptionCount
上面的每个指标( ResourceMeasure)都包含有 ResourcesPerPoint 属性。 例如: AbnormalProcessTerminationCount 的 ResourcesPerPoint 值为 1。 每次发生了AbnormalProcessTermination错误(异常终止沙盒解决方案错误),则添加 1个 磅值。 如果想增加终止了沙盒解决方案罚点可以将 ResourcesPerPoint 设置为 2。 如果您不关心此统计数据,您可以使用 0。
上面的每一个指标还定义有一个AbsoluteLimit属性,此属性用于限制Sandbox Solution可消耗的此指标限制量, 比如,“UnhandledExceptionCount (ResourcesPerPoint: 50  AbsoluteLimit :3)” 表示:每50个未捕获异常将消耗1个“点数”,而每天如果某个解决方案包由于未捕获异常的原因消耗了3个“点数”(也就是它发生了150次异常未捕获的情况),那么这个沙盒解决方案包将被直接中止运行(请注音:是此沙盒解决方案),即使网站集当前还有剩余“点数”。如果不关心此统计数据,则可设置它的ResourcesPerPoint属性为0。
所以,AbsoluteLimit的值只会影响单个解决方案,这点不像每天最大使用率。这个两个级别的配额,每日的配额和绝对的限制,一起工作共同保护场的健康。
 有了上面的描述,再回到本题就很明确了,A.B首先排除,因为它们都只针对单个Sandbox Solution的限制,本题需要针对for all user solutions,所以只能是设置属性ResourcesPerPoint为0
  所以本题目正确选项应该是C


Question 10
You created a custom ASPX page that updates a list. The page is deployed to the _layouts folder.
The page contains the following code segment. (Line numbers are included for reference only.)
01 <form id="Form1" runat="Server">
02 <asp:Button id="btnUpdate" runat="server" Text="Update"></asp:Button>
03 </form>
A user attempts to update the list by using the page and receives the following error message: "The security validation for this page is invalid".
You need to prevent the error from occurring.
Which control should you include in Form1?
A. EncodedLiteral
B. FormDigest
C. InputFormCustomValidator
D. UIVersionedContent

解析:
 本题目的主要意图是实现通过一个用户自定义的ASPX页面去更新一个List的内容,重点线索是报错信息” The security validation for this page is invalid”。
 针对此错误,网上可借鉴的措施有:
 1.设置SPWeb或SPSite对象的"AllowUnsafeUpdates" 为Ture. 但有人不太推荐使用此方法,因为此方法有时候并不起作用。其原因可能是用户在一个Function中定义了SPWeb或SPSite对象,但却在另一个Function中试图操作它们.当然,具体是否如此,我没试过。
  2.还有一个方法就是使用FormDigest控件。你可以把它放置到MasterPage中,也可以放置到某个单独的页面定义中。
为什么要使用FormDigest控件呢?
出于安全考虑,默认情况下,Microsoft SharePoint Foundation 不允许从 Web 应用程序进行发布以修改数据库内容(List的内容当然是保存在内容数据库Content Database里的),除非请求页上包括安全验证。Sharepoint可以通过在请求页上添加页面指令和 FormDigest 控件来更新单个网站或网站集的数据。在 ASPX 页上插入此控件会生成安全验证或消息摘要,以帮助防止在用户因被骗而发布数据到服务器时产生的攻击。安全验证特定于用户、网站和时间段,并在可配置的时间段后过期。当用户请求页时,服务器返回插入了安全验证的页。当用户随后提交表单时,服务器会验证安全验证是否更改。
  选项A. EncodedLiteral控件主要是用于实现向ASPX页面内注入Inline Code.
例如 :
<h1>
<SharePoint:EncodedLiteral runat="server" text="<%$Resources:wss,searchresults_pagetitle%>" EncodeMethod='HtmlEncode'/> : <% SPHttpUtility.HtmlEncode((Request.QueryString["k"].Length > 20) ? (Request.QueryString["k"].Substring(0, 20) + "...") : Request.QueryString["k"], Response.Output); %></h1>
  所以它需要实现的功能与本题目遇到的报错并没关系。

 选项 C :  InputFormCustomValidator
在 Microsoft.SharePoint.WebControls 命名空间里定义了若干验证控件用于验证用户的输入内容。这些控件如下:
• InputFormRequiredFieldValidator
• InputFormRangeValidator
• InputFormCompareValidator
• InputFormRegularExpressionValidator
• InputFormCheckBoxListValidator
• InputFormCustomValidator
InputFormCustomValidator允许用户自行定义验证的Function.

选项D. UIVersionedContent,此控件主要用于Sharepoint Fundation升级(如从Sharepoint2007升级到Sharepoint2010)后,针对不同版本的Fundation的内容呈现的。此控件也与本题的报错信息无关。
所以本题目正确选项应该是B


参考:
http://msdn.microsoft.com/zh-cn/library/ms472879.aspx

posted @ 2013-06-08 09:28  wsdj  阅读(674)  评论(0编辑  收藏  举报