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

Question 81
You need to create a Web Part that creates a copy of the out-of-the-box Contribute permission level.
Which code segment should you implement in the Web Part?
A. SPRoleDefinition myRole = new SPRoleDefinition();
myRole.Name = "Contribute";
SPContext.Current.Web.RoleDefinitions.Add(myRole);
B. SPRoleDefinition myRole = new SPRoleDefinition(SPContext.Current.Web.RoleDefinitions["Contribute"]);
myRole.Name = "MyContribute";
SPContext.Current.Web.RoleDefinitions.Add(myRole);
C. SPRoleDefinition myRole = new SPRoleDefinition(SPContext.Current.Web.RoleDefinitions["MyContribute"]);
myRole.Description = "Contribute";
SPContext.Current.Web.RoleDefinitions.Add(myRole);
D. SPRoleDefinition myRole = new SPRoleDefinition(SPContext.Current.Web.RoleDefinitions["MyContribute"]);
myRole.Name = "Contribute";
SPContext.Current.Web.RoleDefinitions.Add(myRole);

解析:
 本题是想要实现复制OOTB的Contribute Permission Level对象。
  Sharepoint权限系统采用 权限——角色 ——对象——关联 的概念,即:对于一个对象(如:SPWeb,SPList,SPListItem),可以通过添加某个已经包含了角色(SPRoleDefinition)的关联(SPRoleAssignment)而更改该对象的权限,而角色(SPRoleDefinition)则是提前通过SPWeb.RoleDefinitions添加到网站内的,注意:只有SPWeb有此属性可以添加角色定义,其他对象如SPSite,SPList,SPListItem均不能添加角色定义,当然他们也不需要。
 相关类:
用户或用户组:SPPrincipal 扩展了两个子类:SPUser和SPGroup
权限基类:SPBasePermissions,通过或计算合成一个自定义的权限,注意:需要编辑权限的前提是有查看权限;
角色类:SPRoleDefinition,采用web.RoleDefinitions.Add(definition)的方式添加角色;
关联类:对于某个SPPrincipal添加一个SPRoleDefinition的角色,然后再将该关联添加到对象上去,注意:在未将该关联添加到对象上时,该关联都无效。

本题的四个选项都在开始创建了一个SPRoleDefinition对象,但它们采用了不同的构造参数,如下:
SPRoleDefinition():初始化一个新的不含任何Permission的SPRoleDefinition对象实例。 
SPRoleDefinition(SPRoleDefinition):初始化一个新的SPRoleDefinition对象实例,此对象实例拷贝自另一个已经定义好了的SPRoleDefinition对象。 
由上面的描述,我们就可以直接排除选项A了。
再来看SPContext.Current.Web.RoleDefinitions[“参数”]中的参数定义,本题要求Copy自out-of-the-box Contribute permission level,即系统默认的”Contribute”权限。只有选项B采用这个【”Contritute”】参数,而选项C.D都是Copy自用户自定义的Contribute权限。所以,只有选项B符合本题要求。
各选项的后面两句:
myRole.Name = "MyContribute";//给那个新创建的SPRoleDefinition对象命名
SPContext.Current.Web.RoleDefinitions.Add(myRole); //添加此SPRoleDefinition对象到当前Web中。 

所以本题目正确选项应该是B
参考:
http://technet.microsoft.com/zh-cn/library/cc262690.aspx
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.sproledefinition.aspx


Question 82
You need to create a Web Part that verifies whether a user who accesses the Web Part page is a member of a group named Group1.
Which code condition should you use?
A. SPContext.Current.Web.Groups["Group1"].ContainsCurrentUser;
B. SPContext.Current.Web.SiteUsers[SPContext.Current.Web.CurrentUser.ID].Groups["Group1"] != null;
C. SPContext.Current.Web.SiteUsers[SPContext.Current.Web.CurrentUser.ID].Groups["Group1"] == null;
D. SPContext.Current.Web.Users["Group1"].IsDomainGroup;

解析:
本题是想验证当前用户是否属于Group1的成员。
选项B.C是想判断指定的Group对象是否为空。选项D则想判断指定的名为Group1的User是否是域内的组。只有选项A是判断Group1的成员中是否有当前User。
所以本题目正确选项应该是A


参考:
http://msdn.microsoft.com/zh-cn/library/ms479018(v=office.12).aspx
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spgroup.containscurrentuser.aspx


Question 83
You have a SharePoint list named Assets that contains 1,000,000 items. The list contains a column named Urgent. Approximately 100 items have a value of True in their Urgent column.
You use the following line of code to retrieve the Assets list.
SPList assetsList = currentWeb.Lists["assets"];
You need to retrieve all of the items in the list that have a value of True in their Urgent column. You must retrieve the items in the minimum amount of time.
What should you do?
A. Iterate through the assetsList.Items collection.
B. Iterate through the assetsList.Fields collection.
C. Call assetsLists.GetItems and specify the SPQuery parameter.
D. Call assetsList.Items.GetDataTable() and retrieve DataRowCollection.

解析:
 本题是想让你用最快的速度从一个含有大量数据的列表中检索出指定某个字段为特定值的所有Items。
  我们可以采用多种办法从Sharepoint中提取数据。比如:
1.先获取SPList对象再使用For/Freach方法遍历SPListItems
2.或者也可以先创建一个CAML查询然后传递给SPQuery对象由它负责从Sharepoint中快速提取符合过滤条件的数据
3.还可以使用 LINQ to SharePoint 提供程序来进行列表的查询。
  对于第1种方法,当然也是效率最差的方法,因为它要处理每一个Items,所以效率自然不高。
 对于第2种方法,它要求开发人员集中分析 XML 片段,以便采用称为协作应用程序标记语言 (CAML) 的语言参数化查询,此方法效率很快,是我们通常采用的方法。
 对于第3种方法, 通过使用 LINQ to SharePoint 提供程序,您可以并且仅可以查询 SharePoint Foundation 列表数据。您可使用 LINQ 语法编写查询并将结果分配到一个返回对象。第一次执行查询,将会枚举返回对象。此时,LINQ to SharePoint 提供程序会将此查询转换为一个 CAML 查询,然后像处理任何其他 CAML 查询一样处理它。
  其中第2,3种有比较(CAML 对比 LINQ):
CAML 查询的主要优点是,它无需将查询从 LINQ 语法转换为 CAML 语法,因而可能会存在性能优势。
使用 LINQ 查询的主要优点包括:
可以使用集成到 C# 和 Microsoft Visual Basic 中的 LINQ 语法和 LINQ 关键字。相比之下,CAML XML 容易出错误,而且仅适用于 SharePoint Foundation。
通过使用 LINQ to SharePoint 提供程序,您使用的是强类型的列表项对象。在 SharePoint Foundation 客户端对象模型中,任意两个 SPListItem 对象都是相同的类型,即使它们表示完全不同类型的列表中的项。但是,LINQ to SharePoint 提供程序使用实体类,并且将为网站中任何列表上所使用的每个内容类型包含一个类。例如,Announcements 列表中的项是 Announcement 类型的对象,而 Tasks 列表上的项是 Task 类型的对象。
  现在回到本题,分析各选项:
选项A. B.C  显然都是采用的遍历的方法 ,其中选项A是返回List的所有Items并遍历。选项B是返回List的所有Fields,并遍历。选项 D是以DataTable的方式返回所有的List Items,并遍历。

选项C.采用的是SPQuery的方法,比较高效,也是我们常用的方法。 
所以本题目正确选项应该是C
参考:
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spquery.aspx

Question 84
You create a Web Part.
The Web Part contains a grid view named GridView1 and the following code segment. (Line numbers are included for reference only.)
01 IntranetDataContext dc = new IntranetDataContext("http://intranet");
02 MyGridView.DataSource = from announce In dc.Announcements _ ;
03
04 Select announce
IntranetDataContext is a LINQ context.
You need to ensure that GridView1 only displays items from Announcements that have an expiry date that is greater than or equal to the current date.
What should you do?
A. Change line 04 to the following code segment:
Select Not announce.Expires.HasValue
B. Change line 04 to the following code segment:
Select announce.Expires.Value.CompareTo(DateTime.Now) >= 0
C. Add the following line of code at line 03:
Where announce.Expires.Value.CompareTo(DateTime.Now) >= 0 _
D. Add the following line of code at line 03:
Order By announce.Expires.Value.CompareTo(DateTime.Now) >= 0 _

解析:
 本题实质是考LINQ的表达语法,
LINQ 是编程语言 C# 和 Microsoft Visual Basic .NET 的一个功能。编译器是 Visual Studio 附带的。LINQ 将类似 SQL 的语法和词汇添加到每种语言,它们可用于查询数据源。但有别于其他具有不同数据源类型的语言和查询语法的是,原则上,LINQ 可用于查询任何数据源,无论它是什么。
若要能够使用 LINQ 访问数据源,需要做的就是为该数据源创建 LINQ 提供程序。
LINQ to SharePoint 提供程序是在 Microsoft.SharePoint.Linq 命名空间中定义的。它将 LINQ 查询转换为协作应用程序标记语言 (CAML) 查询。开发人员无需再了解如何编写 CAML 查询。LINQ 查询可在服务器代码中使用。
下面是一段Linq for Sharepoint的样例:

// Get DataContext from page context
DataContext data = new DataContext(SPContext.Current.Web.Url);

// Get the SharePoint list
EntityList<Customer> Customers = data.GetList<Customer>("Customers");

// Query for customers from London
var londonCustomers = from customer in Customers
                      where customer.City == "London"
                      select customer;

foreach (var londonCust in londonCustomers)
{
    Console.Writeline("id = {0}, City = {1}", 
                      londonCust.CustomerId, 
                      londonCust.City);
}

  参照样例可以看出,题干部分少了Where部分,即选项C部分。
你还可以参考我的与Linq For Sharepoint相关的博文:
http://www.cnblogs.com/wsdj-ITtech/archive/2011/11/03/2232530.html
http://www.cnblogs.com/wsdj-ITtech/archive/2011/11/06/2232912.html
http://www.cnblogs.com/wsdj-ITtech/archive/2011/11/08/2233008.html

所以本题目正确选项应该是C
参考:

http://msdn.microsoft.com/en-us/library/ee535491.aspx

转载于:https://www.cnblogs.com/wsdj-ITtech/p/3164239.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值