技术笔记: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


参考:

相关文章
|
2月前
|
人工智能 编解码 语音技术
文案变成视频怎么做?先转成镜头表:提示词模板与9项校验清单
本文回答:文案变成视频怎么做——把一段文案转成镜头表的规则(文案句子怎么改成看得见的动作、每镜时长怎么估、旁白和画面怎么分轨),一份可直接复制的「文案 → 镜头表」提示词模板和示例输出,9 项校验清单,镜头表字段怎么落进生成提示词,以及几种做法里镜头表由谁来出。
|
27天前
|
人工智能 弹性计算 API
阿里云双十一活动百炼 Token Plan 完整手册,功能、报价、选购方案、Credits 与 AI 订阅体系
综合来看,2026年阿里云百炼Token Plan以一个固定订阅统一计量模型调用、工具集成与Agent编排,个人版从Lite 39元到Pro 499元、团队版从标准150元到尊享1398元,档位梯度清晰,覆盖从个人开发到企业团队的全场景。趁双十一的价格不变加量升级、新增Harness权益、88折、旗舰模型上新与组合套餐优惠,个人和企业都能以更低成本获得稳定的AI调用能力。建议先免费额度完成功能验证,确认调用量后按档位订阅Token Plan,一个Key绑定Qwen Code、Qoder CN等多款工具,叠加夜间特惠与组合套餐,把AI编程、智能体与多模型调用成本压到最低,完成从免费体验到订阅落地
150 0
|
2月前
|
人工智能 自然语言处理 文字识别
阿里云3组AI免费权益:新用户0成本领7000万tokens、500元算力券,试用30+款产品与100+Agent‌
本文梳理阿里云面向新用户的 AI 免费权益,分两类。一是大模型 tokens 免费额度,含"7000 万 tokens"组合权益,经免费试用页或体验馆领取,有效期 90 天,仅限华北 2(北京)地域,只抵扣实时推理费用,可开启"用完即停"防扣费。二是 AI 产品免费试用,覆盖 PAI、NLP、视觉智能、GPU 等超 30 款产品,仅限产品新用户,额度时长各异。两类可并行使用,领取前需确认资格并及时评估续费或关停。
|
5月前
|
人工智能 自然语言处理 运维
2026企业AI客服系统建设费用:选型策略真实成本解析
本文解析2026年AI客服系统费用新构成:基础订阅、大模型Token算力、定制集成三大板块;结合瓴羊Quick Service真实数据与星巴克等标杆案例,提供选型指南、费用参考及四步落地法,助力企业降本增效。(239字)
|
7月前
|
SQL 存储 数据库
SQL Server Management Studio (SSMS) 22.6 发布 - 微软数据库管理工具
SQL Server Management Studio (SSMS) 22.6 - 微软数据库管理工具
417 0
SQL Server Management Studio (SSMS) 22.6 发布 - 微软数据库管理工具
|
7月前
|
人工智能 弹性计算 运维
2026年阿里云新用户每日秒杀优惠活动入口、配置与价格、活动规则参考
阿里云2026年面向新用户推出的每日秒杀活动,包含两大核心入口:一是"开年焕新"活动页,提供2核2G轻量服务器38元/年(宝塔面板镜像)和2核4G配置9.9元/月或199元/年(OpenClaw镜像);二是分钟级部署OpenClaw活动页,同样2核4G配置,预装Hermes或OpenClaw镜像,适合AI智能体开发。秒杀每天10:00和15:00两场,限新用户抢购,中国大陆地域可用。未抢到者可选68元/年新用户专属价,或99元/年经济型e实例、199元/年u1实例等长效特惠方案作为替代,均享新购续费同价,活动截止2027年3月31日。
|
JSON 安全 API
12306旅游产品数据抓取:Python+API逆向分析
12306旅游产品数据抓取:Python+API逆向分析
|
10月前
|
编解码 缓存 安全
Python批量压缩图片:节省存储空间的实用脚本
本文介绍如何用Python(Pillow库)编写50行内批量图片压缩脚本:支持尺寸缩放、质量调节、透明通道处理与进度反馈;兼顾隐私安全、高度定制与多线程加速,轻松将4K/RAW图压缩90%以上,适用于电商、旅行、设计等场景。(239字)
1047 6
|
前端开发 计算机视觉 Python
浅蓝色代表什么颜色?——Python中的颜色表示与处理
本文介绍了浅蓝色在计算机图形和Web开发中的表示方法,包括RGB、十六进制和HSL三种常见格式,并详细说明了如何使用Python的Pillow和colorsys库来处理和转换这种颜色,最后给出了生成浅蓝色背景的CSS代码示例。
1978 6
条件变量函数pthread_cond_timedwait实现业务场景
条件变量函数pthread_cond_timedwait实现业务场景
816 0