Sharepoint学习笔记—习题系列--70-573习题解析 -(Q97-Q99)
Question 97
You have a list named Projects that contains a column named ClassificationMetadata.
You need to create a Web Part that updates the ClassificationMetadata value to NA for each item in the Projects list.
You write the following code segment. (Line numbers are included for reference only.)
01 foreach (SPListItem currentItem in SPContext.Current.Web.Lists["Projects"].Items)
02 {
04 }
Which code segment should you add at line 03?
A. currentItem["ClassificationMetadata"] = "NA";
B. currentItem.Fields["ClassificationMetadata"].DefaultFormula = "NA";
C. currentItem.Fields["ClassificationMetadata"].DefaultValue = "NA";
D. currentItem["Value"] = "ClassificationMetadata/NA";
解析:
本题想要达到的目的是通过代码去更新Projects列表中的ClassificationMetadata字段的值。采用的是遍历所有SPListItem的方法。对字段值的操作是基于获取的SPListItem对象进行的。
直接分析各选项:
A. currentItem["ClassificationMetadata"] = "NA";//本题答案。修改Item的” ClassificationMetadata”字段的值为”NA”
B. currentItem.Fields["ClassificationMetadata"].DefaultFormula = "NA";//获取名为"ClassificationMetadata"的字段,并设置此字段的默认公式,此用法只适用于计算字段。
C. currentItem.Fields["ClassificationMetadata"].DefaultValue = "NA"; //获取名为"ClassificationMetadata"的字段,并设置此字段的默认值为”NA”。也就是说在新添加一个Item时,如果不给此字段赋值,则默认值为”NA”。
D. currentItem["Value"] = "ClassificationMetadata/NA"; //设置Item的”Value”字段的值为” ClassificationMetadata/NA”
所以本题目正确选项应该是A
参考:
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistitem.aspx
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfield.aspx
Question 98
You create a Web Part that queries a list.
The Web Part contains the following code segment. (Line numbers are included for reference only.)
01 protected override void Render(HtmlTextWriter writer)
02 {
03 SPUserToken spInToken = GetTheContext(SPContext.Current.Site);
04 using (SPSite aSite = new SPSite(curSiteCtx.ID, spInToken))
05 {
06
07 }
08 }
09 private SPUserToken GetTheContext(SPSite nWeb)
10 {
11 nWeb.CatchAccessDeniedException = false;
12 SPUserToken spToken = null;
13 try
14 {
15 spToken = nWeb.SystemAccount.UserToken;
16 }
17 catch (UnauthorizedAccessException generatedExceptionName)
18 {
19
20 }
21 return spToken;
22 }
You need to ensure that users without permissions to the list can view the contents of the list from the Web Part.
Which code segment should you add at line 19?
A. SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite eSite = new SPSite(nWeb.ID))
{
spToken = nWeb.SystemAccount.UserToken;
}
}
B. SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite eSite = new SPSite(nWeb.ID))
{
spToken = SPContext.Current.Web.CurrentUser.UserToken;
}
}
C. spToken = nWeb.RootWeb.AllUsers[SPContext.Current.Web.Name].UserToken; D. spToken = nWeb.RootWeb.AllUsers[WindowsIdentity.GetCurrent().Name].UserToken;
解析:
本题想要实现在一个WebPart的后台代码中让没有访问权限的用户也能看到某列表的内容。
很显然属于提升权限的题目,也就自然关联到RunWithElevatedPrivileges。
选项A,B都使用了RunWithElevatedPrivileges方法,但是注意看
09 private SPUserToken GetTheContext(SPSite nWeb)
10 {
11 nWeb.CatchAccessDeniedException = false;
12 SPUserToken spToken = null;
13 try
14 {
15 spToken = nWeb.SystemAccount.UserToken;
16 }
17 catch (UnauthorizedAccessException generatedExceptionName)
18 {
19
20 }
21 return spToken;
22 }
此段代码是要返回SPUserToken,此SPUserToken决定了操作的权限。选项B返回的当前用户的SPUserToken,显然属于白忙一场。选项A获取了SystemAccount的SPUserToken真正达到了目的。所以选项A是本题的答案。
选项C. spToken = nWeb.RootWeb.AllUsers[SPContext.Current.Web.Name].UserToken; //取得的是用户名为当前Web的Name的用户的SPUserToken,逻辑有点离谱了。
选项D. spToken = nWeb.RootWeb.AllUsers[WindowsIdentity.GetCurrent().Name].UserToken;
//取得的是用户名为当前 Windows 用户的 WindowsIdentity 对象的Name的SPUserToken,显然也不正确。
所以本题目正确选项应该是A
参考:
http://msdn.microsoft.com/zh-cn/library/sfs49sw0.aspx
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb_members.aspx
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx
Question 99
You create a Web Part that programmatically updates the description of the current SharePoint site.
The Web Part contains the following code segment. (Line numbers are included for reference only.)
01 SPSecurity.RunWithElevatedPrivileges(delegate()
02 {
03 SPSite currSite = SPContext.Current.Site;
04 SPWeb currWeb = SPContext.Current.Web;
05 using (SPSite eSite = new SPSite(currSite.ID))
06 {
07 using (SPWeb eWeb = eSite.OpenWeb(currWeb.ID))
08 {
09 eWeb.AllowUnsafeUpdates = true;
10 currWeb.Description = "Test";
11 currWeb.Update();
12 eWeb.AllowUnsafeUpdates = false;
13 }
14 }
15 });
Users report that they receive an Access Denied error message when they use the Web Part. You need to ensure that all users can use the Web Part to update the description of the current site.
What should you do?
A. Remove lines 09 and 12.
B. Remove lines 10 and 11.
C. Change lines 10 and 11 to use the eWeb variable.
D. Change lines 09 and 12 to use the currWeb variable.
解析:
本题是想要在一段代码实现更新当前Sharepoint Site的描述信息,结果收到了”Access Denied”报错。
原因很简单,我们在如下代码
07 using (SPWeb eWeb = eSite.OpenWeb(currWeb.ID))
08 {
09 eWeb.AllowUnsafeUpdates = true;
10 currWeb.Description = "Test";
11 currWeb.Update();
12 eWeb.AllowUnsafeUpdates = false;
13 }
中操作的都是eWeb对象,并在第09行打开了AllowUnsafeUpdates设置,以允许对eWeb对象的更新保存到数据库,但在第10,11行却跳回到了currWeb对象,所以选项C是正确的纠错方法。
有人会问,那么我直接使用currWeb来进行更新为什么不可呢?
我们知道RunWithElevatedPrivileges方法通过接受一个委托(delegate)参数, 来添加一个需权限提升后执行的方法的引用。 当我们通过调用RunWithElevatedPrivileges提升在SharePoint上下文中的权限后,我们必须接着创建一个SPSite和SPWeb类的实例。切记不能通过Microsoft.SharePoint.SPContext.Current属性来获取这些对象。因为这些对象都是通过当前用户的安全上下文创建的。本例题干部分的代码展示了一种很好的方法来在权限提升后得到这些对象。另外这样写还可以保证在Using语句外这些对象可以通过调用Dispose方法很好的被释放。
所以本题目正确选项应该是C
参考:
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.allowunsafeupdates.aspx
Sharepoint学习笔记—习题系列--70-573习题解析 -(Q97-Q99)的更多相关文章
- Sharepoint学习笔记—ECM系列—文档列表的Metedata Navigation与Key Filter功能的实现
如果一个文档列表中存放了成百上千的文档,想要快速的找到你想要的还真不是件容易的事,Sharepoint提供了Metedata Navigation与Key Filter功能可以帮助我们快速的过滤和定位 ...
- Sharepoint学习笔记—ECM系列--文档集(Document Set)的实现
文档集是 SharePoint Server 2010 中的一项新功能,它使组织能够管理单个可交付文档或工作产品(可包含多个文档或文件).文档集是特殊类型的文件夹,它合并了唯一的文档集属性以及文件夹和 ...
- Sharepoint学习笔记—习题系列--70-576习题解析 --索引目录
Sharepoint学习笔记—习题系列--70-576习题解析 为便于查阅,这里整理并列出了70-576习题解析系列的所有问题,有些内容可能会在以后更新. 需要事先申明的是: 1. ...
- Sharepoint学习笔记—习题系列--70-573习题解析 --索引目录
Sharepoint学习笔记—习题系列--70-573习题解析 为便于查阅,这里整理并列出了我前面播客中的关于70-573习题解析系列的所有问题,有些内容可能会在以后更新, ...
- Deep Learning(深度学习)学习笔记整理系列之(五)
Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...
- Deep Learning(深度学习)学习笔记整理系列之(八)
Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...
- Deep Learning(深度学习)学习笔记整理系列之(七)
Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...
- Deep Learning(深度学习)学习笔记整理系列之(六)
Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...
- Deep Learning(深度学习)学习笔记整理系列之(四)
Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...
- Deep Learning(深度学习)学习笔记整理系列之(三)
Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...
随机推荐
- C#高级编程(第8版)
http://spu.jd.com/11328513.html 第1章 .NET体系结构1.1 C#与.NET的关系1.2 公共语言运行库1.2.1 平台无关性1.2.2 提高性能1.2.3 语言的互 ...
- ECMAScript5新增对象语法糖getter和setter
在新的ECMAScript5中新添加了两个语法糖,这两个语法糖是这样的. var obj = (function(){ var num = 10; return { get n(){ return n ...
- publishing failed with multiple errors
背景: 1.使用maven package工程 2. 在eclipse中添加server运行时 publishing failed with multiple errors resource is o ...
- 编码神器——Sublime Text 包管理工具及扩展大全
Sublime Text 是程序员们公认的编码神奇,拥有漂亮的用户界面和强大的功能,例如代码缩略图,多重选择,快捷命令等.还可自定义键绑定,菜单和工具栏.Sublime Text 的主要功能包括:拼写 ...
- iOS-Debug调试
转载:http://www.cnblogs.com/Leo_wl/p/4423922.html
- SQL Server里PIVOT运算符的”红颜祸水“
在今天的文章里我想讨论下SQL Server里一个特别的T-SQL语言结构——自SQL Server 2005引入的PIVOT运算符.我经常引用这个与语言结构是SQL Server里最危险的一个——很 ...
- C#语法糖之 session操作类 asp.net
用法: //声名一个数据集合 var listString = new List<string>() { "a", "b", "c&quo ...
- mysql线上一些隐患查询sql
开发写了几个语句,觉得查询结果跟逻辑有点不相符,就拿到这里一起分析了下. 语句如下: select tp.title, tp.amount, ifnull( ) as aInvestAmount, i ...
- 重构第5天:提升字段(Pull Up Field)
理解:提升字段和前面讲解的方法提公很类似,可以说方式都是一样的.就是把继承类中经常用到的字段,提出来 放到基类中,达到通用的目的.提高代码重用性和可维护性. 详解:如下重构前的代码: using Sy ...
- 【C#进阶系列】07 常量和字段
常量 常量总是被视为静态成员. 常量其实可以不限于基元类型,但是必须初始化为null.(我觉得这个点知道和不知道都一样,我已经自动从脑海中忽略了.很多时候在我这个人眼中,艰涩的代码和垃圾代码,其实没有 ...