您的位置:首页 > 其它

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

2014-05-06 17:52 531 查看
Question 147
You have a Web application named WebApp1.
You have a Feature receiver named FeatureReceiver1. FeatureReceiver1 stores a connection string in the web.config file of WebApp1.
You need to ensure that when FeatureReceiver1 makes configuration changes to web.config, the changes are automatically replicated to all Web servers in the farm.
Which class should you use in FeatureReceiver1?
A. SPDiagnosticsService
B. SPPersistedObject
C. SPWebConfigModification
D. WebConfigurationManager

解析:
本题题意是:你有一个Web Application, 一个Feature Receiver (FeatureReceiver1),此FeatureReceiver1保存了一个配置信息(数据库连接字符串)在Web.config文件中,现在你想要实现的效果是:如果你在FeatureReceiver1中对Web.config保存的那个配置信息作了修改,那么你希望这个修改会传播到整个服务器场的所有Web Server上。
先来了解一下Web.config及其修改。
在Sharepoint中我们经常需要修改 web.config 文件,才能使我们的解决方案在 Microsoft SharePoint Foundation 环境中有效。例如,可能需要添加一个 SafeControl 项,注册 HttpModule,或自定义 SessionState,但是部署 SharePoint Foundation 将需要大量的 web.config 文件。例如,每个 Web 应用程序对应一个文件,并且每台前端 Web 服务器中包含每个这些文件的副本。
为了更加轻松地自定义 web.config 文件,SharePoint Foundation 提供了两种更改方式。
第一种方式是通过创建包含一组 web.config 修改的 XML 文件以”声明方式”注册 web.config 项。为此,您需要创建一个已部署到 %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\CONFIG 中的自定义 webconfig.XYZ.xml 文件,其中,XYZ 是您用于标识自己的解决方案的唯一 ID。
第二种方式是使用 SharePoint Foundation 对象模型来修改 web.config 设置。 即通过使用 Microsoft.SharePoint.Administration 命名空间的 SPWebConfigModification 类来实现,这使得您能够动态地对实体进行注册。这些修改保留在配置数据库中,在那里,它们充当一种虚拟 web.config,后者其实上充当 SharePoint Foundation Web 应用程序的 .config 文件堆栈的最后一层。在调用 SPWebService.ApplyWebConfigModifications 方法时,所做更改才会生效。
调用 ApplyWebConfigModifications 方法将安排一个计时器作业以在整个服务器场中部署更改【这句话说明了SPWebConfigModification类操作的范围是整个服务器场】。若要将 web.config 修改应用于特定的 Web 应用程序,请将此修改添加到 Web 应用程序的 web.config 修改集 (WebConfigModifications) 中。例如,您可以使用 oWebSite.Site.WebApplication.WebConfigModifications.Add(MyModification) 将 web.config 修改添加到特定网站的父 Web 应用程序中。即使是将 web.config 修改添加到单个 Web 应用程序,您也仍旧必须调用 ApplyWebConfigModifications。
通过上面的描述,我们可以定位选项C为本题的正解。
继续分析其它备选项提供的各个类,看看它们都提供了什么样的功能:
选项A. SPDiagnosticsService 在对 SharePoint 生成的消息进行疑难解答时,从 SharePoint ULS 日志读取会很有用。但开发人员真正需要的是,能够将自己的消息写入日志文件。Microsoft 通过使用 SPDiagnosticsService 类使对 SharePoint 2010 中的日志文件进行写入变得更加容易。所以此类是用于对Sharepoint日志文件进行操作的,和本题的要求相距离甚远。
选项B. SPPersistedObject 用于为对象提供自动序列化其状态值并持久保存以及在需要时获取前面所保存的值并反序列化的相关方法。也即,它属于定义自定义管理设置方面的类。在 Windows SharePoint Services 平台上构建应用程序时,您可能需要创建一个类以定义应用程序的自定义属性设置并提供用于存储这些设置的方法。Windows SharePoint Services 3.0 中的 Microsoft.SharePoint.Administration 命名空间引入了对象持久性,使用它可将派生自基础 SPPersistedObject 类的类保留到 Windows SharePoint Services 数据库中。若要创建保留的对象,您可以创建一个类,该类从 SPPersistedObject 或派生自 SPPersistedObject 的 Microsoft.SharePoint.Administration 命名空间中的某个类继承。此类可以定义一个用于存储 Windows SharePoint Services 数据库中的自定义属性设置的对象模型。例如,您可以创建通过某种方式使用 Windows SharePoint Services 的 Windows 服务,这将需要创建一个继承自 SPWindowsService 类的类,以便存储特定于 Windows 服务的设置。已编译的 DLL 将为客户提供一个可用于检索或保存属性的对象模型。Windows SharePoint Services 提供用于存储设置的方法和位置,并管理它们对服务器场中的所有服务器和过程(包括缓存刷新)的可用性。 在实际使用中,我们也可用它来保存用户的某些配置信息。很显然,此类与Web.config的操作没什么关系。
选项D. WebConfigurationManager 属于.NET中的传统概念,在.NET FrameWork中如果需要灵活的操作和读写配置文件并不是十分方便,一般都会在项目中封装一个配置文件管理类来进行读写操作。而在. NET2.0中使用ConfigurationManager 和WebConfigurationManager 类可以很好的管理配置文件,ConfigurationManager类在System.Configuration 中,WebConfigurationManager在System.Web.Configuration中。根据MSDN的解释,对于 Web 应用程序配置,建议使用System.Web.Configuration.WebConfigurationManager 类,而不要使用 System.Configuration.ConfigurationManager 类。

所以本题目正确选项应该是C
参考:
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.spwebconfigmodification.aspx
http://msdn.microsoft.com/zh-cn/library/gg512103(v=office.14).aspx

Question 148
You need to create a Web Part that will store and retrieve information for the current subsite.
Which object should you use?
A. SPContext.Current.Site.RootWeb.AllProperties
B. SPContext.Current.Site.RootWeb.Configuration
C. SPContext.Current.Web.Configuration
D. SPContext.Current.Web.Properties

解析:
本题试图获取当前子网站(subsite)的相关信息。
首先只要一看到SubSite就可以直接排除与Site相关的选项了【排除选项A.B】,因为Site代表的是网站集(Site Collection),而SubSite代表的是Web.
再来看选项C.D
选项C. SPContext.Current.Web.Configuration : 此属性返回是ID值(16位的整数值),代表网站定义配置信息(Configuration of Site Definition)的ID。
选项D. SPContext.Current.Web. Properties: 用于保存网站的相关设置信息,例如:Windows SharePoint Services的版本,本地语言等等。建议使用AllProperties属性来返回关于Web Site的所有设置信息,而Properties属性只是返回其中的部分信息。 .
所以本题目正确选项应该是D
参考:
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.properties.aspx
http://msdn.microsoft.com/zh-cn/library/microsoft.sharepoint.spweb_members(v=office.12).aspx
http://msdn.microsoft.com/zh-cn/library/microsoft.sharepoint.spweb.allproperties(v=office.12).aspx

Question 149
You have a SharePoint site that uses the default search settings.
You create a new Search Center.
You need to ensure that the new Search Center is the default Search Center for the site.
What should you modify?
A. the AllProperties collection of the site
B. the AllWebs collection of the site collection
C. the Cache property of the current HttpContext
D. the Session property of the current HttpContext

解析:
本题是想要用搜索中心(Search Center)来代替一个SharePoint site原来默认使用的Default Search Settings。
我们先来看看如何通过Sharepoint操作界面来实现本题的要求,即:选择某个Sharepoint Site的 Site Actions→Site Settings 进入到此网站设置信息页面,在Site Collection Administration栏,点击Search Settings就可以设置此Site的相关Search项了。
所以你可以看出,关于Search Settings设置是属于Site的相关设置信息,根据Question148的描述,此类信息是保存在AllProperties属性或Properties属性中的。所以选项A 为本题的答案。
再来看其它选项
选项B. the AllWebs collection of the site collection 返回某网站集包含的所有Webs对象。
选项C. the Cache property of the current HttpContext 获取当前应用程序域的 Cache 对象。
选项D. the Session property of the current HttpContext为当前 HTTP 请求获取 HttpSessionState 对象
所以本题目正确选项应该是A
参考:
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.allproperties.aspx
http://www.novolocus.com/2010/12/22/stupid-spweb-properties/

Question 150
You create a class that inherits the SPPersistedObject class. The class has a field named Field1.
You need to ensure that Field1 can be serialized and stored in the SharePoint configuration database.
Which two actions should you perform? (Each correct answer presents part of the solution. Choose two)
A. Add the default empty constructor.
B. Mark Field1 with the [Persisted] attribute.
C. Mark Field1 with the [Serializable] attribute.
D. Set the value of the SPWeb.AllowUnsafeUpdate property.
解析:
本题是关于如何应用SPPersistedObject类的题目。
我们知道SPPersistedObject类是用于为对象提供自动序列化其状态值并持久保存以及在需要时获取前面所保存的值并反序列化的相关方法。也即,它属于定义自定义管理设置方面的类。  在 Windows SharePoint Services 平台上构建应用程序时,您可能需要创建一个类以定义应用程序的自定义属性设置并提供用于存储这些设置的方法。
下面是一段实现示例:

[GuidAttribute("3B60B328-F4D6-4902-BEC6-E904179FA246")]
public class MyObject : SPPersistedObject
{
[Persisted]
private string location;

[Persisted]
private string field1;

public MyObject()
{
}
/*The default constructor must be specified for serialization.*/
public MyObject(string name, SPPersistedObject parent)
: base(name, parent)
{
;
}

public override string DisplayName
{
get
{
return this. field1;
}
}

public string Field1
{
get { return this. field1; }
set
{
if (this. field1== value)
{
return;
}

this. field1= value;
}
}

public string Location
{
get { return this.location; }
set { this.location = value; }
}
}


再来看SPPersistedObject类的进一步描述:
SPPersistedObject类包含了序列化实现代码。当创建一个用户自定义的Persisted对象时,你必须为其序列化定义一个默认的构造函数(Constructor)
所以选项A.B符合本题要求。
选项C. Mark Field1 with the [Serializable] attribute. 用于标识你的类、结构、代理或枚举为 [Serializable],意在说明此类可以被序列化。 一般是用于修饰整个类,而不是某个类内部的属性成员。如下示例:

using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

[Serializable]
public class BookRecord {
public String title;
public int asin;

public BookRecord(String title, int asin) {
this.title = title;
this.asin = asin;
}
}

public class SerializeObject {
public static void Main() {
BookRecord book = new BookRecord("title",123456789);
FileStream stream = new FileStream(@"book.obj",FileMode.Create);

BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(stream, book);
stream.Close();
}
}


选项D. Set the value of the SPWeb.AllowUnsafeUpdate property. 此属性是用来标识是否允许更新数据库的。 通常而言SharePoint 2010 是要阻止开发人员对 GET 请求执行状态更改操作的。例如,在使用 GET 获取列表项或 Web 属性时,不允许 Microsoft ASP.NET 页更新列表项或 Web 属性的内容。但如果您的功能设计强制对 GET 请求执行状态更改操作,则您可通过将当前 Microsoft.SharePoint.SPWeb 类的 AllowUnsafeUpdates 属性设置为 true 以禁用此检查。请记住,在执行操作后重置该属性,并使用 try-catch-finally 块以确保异常不会将该属性保持为 true。因为此项存在安全隐患,所以,微软通常建议尽量避免使用 AllowUnsafeUpdates。

所以本题目正确选项应该是A.B
参考:
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.sppersistedobject.aspx
http://www.chaholl.com/archive/2011/01/30/the-skinny-on-sppersistedobject-and-the-hierarchical-object-store-in.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: