技术笔记:Sharepoint学习笔记—习题系列

简介: 技术笔记:Sharepoint学习笔记—习题系列

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 {


03


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


参考:


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 try14 {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


参考:


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 1//代码效果参考:http://hnjlyzjd.com/hw/wz_24675.html

0 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


参考:

相关文章
|
存储 安全 Windows
PowerShell系列(六):PowerShell脚本执行策略梳理
【2月更文挑战第1篇】PowerShell 脚本执行策略用于控制何时以及何种方式执行 PowerShell 脚
|
11月前
|
存储 Python
Python自动化脚本编写指南
【10月更文挑战第38天】本文旨在为初学者提供一条清晰的路径,通过Python实现日常任务的自动化。我们将从基础语法讲起,逐步引导读者理解如何将代码块组合成有效脚本,并探讨常见错误及调试技巧。文章不仅涉及理论知识,还包括实际案例分析,帮助读者快速入门并提升编程能力。
847 2
|
12月前
|
监控 关系型数据库 MySQL
PowerShell 脚本编写 :自动化Windows 开发工作流程
PowerShell 脚本编写 :自动化Windows 开发工作流程
468 0
|
12月前
|
C语言
C语言指针(3)
C语言指针(3)
53 1
|
敏捷开发 数据可视化 安全
如何选择低代码开发平台?必看注意事项揭秘!
低代码开发平台和零代码开发平台是近几年时兴的一种新的程序开发方法。该模式的特征是可以使用用户界面、拖拽操作等方式快速构建应用软件软件,从而减少开发者的学习标准,使每个人都能变成开发者。
231 0
|
运维 开发工具 Windows
PowerShell系列(五):PowerShell通过脚本方式运行笔记
【1月更文挑战第7天】方便迁移,比如在之前工作经验积累下来的运维脚本,可以保存下来。如果业务场景用的到的话,直接文件拷贝过来就可以运行。
Springboot整合之Shiro和JWT技术实现无感刷新4
Springboot整合之Shiro和JWT技术实现无感刷新4
|
存储 Java 编译器
C 语言注释和变量详解
C语言中可以使用注释来解释代码并使其更具可读性。它还可以在测试替代代码时防止执行。
208 0
java202304java学习笔记第五十九天员工管理-ssm-spring配置文件-依赖注入1
java202304java学习笔记第五十九天员工管理-ssm-spring配置文件-依赖注入1
96 0
|
弹性计算 运维 云计算
阿里云重磅发布云上自动化利器——运维编排OOS
阿里云运维编排是一个全面的,免费的云上自动化运维平台,提供了运维任务的管理和执行。典型的使用场景包括:事件驱动运维,批量操作运维,定时运维任务,跨地域运维等,OOS特别为一些重要的运维场景提供了审批,通知等功能。
84587 0