zoukankan      html  css  js  c++  java
  • Sharepoint学习笔记—习题系列--70-573习题解析 -(Q57-Q59)

    Question 57
    You update a solution validator.
    You need to ensure that all SharePoint solutions are validated the next time the solutions are executed.
    What should you do?
    A. Modify the Guid attribute of the solution validator.
    B. Deactivate and activate all of the installed solutions.
    C. Modify the Signature property of the solution validator.
    D. In the Feature that deploys the solution validator, modify the Version attribute of the Feature element.

    解析:
      本题题意是:你升级了一个Solution Validator 并想让它在你下一次运行Solution时生效,那么你该如何做呢?
       Sharepoint场管理员可以在场中部署Solution Validator,当向Solution Gallery中Upload某个Solution时,这些Validator就会运行。如果某个Solution没有通过Validation(即Validation Fail),那么这个Solution就不能被激活。如果一个Validator是在Solution被激活后才添加到场中的,那么这个Validator就会在Solution下一次被重新执行时被调用。如果一个Validator进行了升级,那么Solutions将会在下一次被重新执行时调用这个升级后的Validator。
    我们通常通过编写一个继承自SPSolutionValidator(在命名空间namespace Microsoft.SharePoint.UserCode下) 的类来实现Solution Validator。一旦编写成功,我们还需要把它加入到SPUserCodeService SolutionValidators collection中,这一步通常需要我们编写一个Farm级别的Feature或使用PowerShell Script来完成。
    如果要升级你的Solution Validator,你需要修改SPSolutionValidator.Signature属性,此属性存放的就是Solution Validator的版本号以及它的State Hash值。Solution Validator的构造函数将会使用到这个属性来构造Solution Validator。

    下面是一段Solution Validator的实现代码

    [Guid("3014EB40-A067-4A21-BB83-B787229A7FC1")] //每个Validator都必须有一个唯一的Guid值以标识此Validator
    class WinsmartsSolutionValidator : SPSolutionValidator
    {
    private const string validatorName = "Winsmarts Solution Validator";
    public WinsmartsSolutionValidator() { }
    public WinsmartsSolutionValidator(SPUserCodeService userCodeService) //新建了一个构造函数以Override原来的构造函数
    : base(validatorName, userCodeService)
    {
     //在此构造函数中给Signature属性赋值
    this.Signature = 1111; //如果你要升级Validator,那么你就必须要修改Signature值
    }
    public override void ValidateSolution(SPSolutionValidationProperties properties)
    {
    base.ValidateSolution(properties);
    // 在此处写入Validator处理逻辑
    properties.Valid = false;
    properties.ValidationErrorMessage = "The uploaded solution is invalid";
    properties.ValidationErrorUrl = "/_layouts/SolutionValidator/WinsmartsErrorPage.aspx";
    }
    public override void ValidateAssembly(SPSolutionValidationProperties properties,
    SPSolutionFile assembly)
    {
    base.ValidateAssembly(properties, assembly);
    properties.Valid = true;
    }
    }

      现在我们分析各个选项:
    选项A. Guid 即SPSolutionValidator.ID,也即SPSolutionValidator的唯一标识名。此属性显然与Solution Validator的升级操作没直接关系,它只是一个标识。
    选项B. Deactivate and activate all of the installed solutions.本选项所作的操作是停止和重启所有的Solution。此选项具有一定的迷惑性。事实上这是不需要的,正如我们上面所说,如果一个Validator进行了升级,那么Solutions将会在下一次被重新执行时调用这个升级后的Validator。请注意:是重新执行(excute),而不是重新激活(activate)。也就是说,Solution一直是处激活状态的,但它却不一定一直处于被执行的状态。
    选项C. Signature property of the solution validator. 指的是SPSolutionValidator.Signature这个属性,此属性存放的就是Solution Validator的版本号以及它的State Hash值。
    选项D. In the Feature that deploys the solution validator, modify the Version attribute of the Feature element. 选项D只是涉及到Feature的版本升级,而Solution Validator的升级必须要修改SPSolutionValidator.Signature属性。
    所以本题目正确选项应该是C


    参考:
    http://msdn.microsoft.com/zh-cn/library/microsoft.sharepoint.usercode.spsolutionvalidator.signature


    Question 58
    You are creating a Web Part. The Web Part will be used in a SharePoint subsite that has the URL http://www.contoso.com/hr.
    You need to ensure that the Web Part activates a Feature in the subsite without causing a memory leak.
    Which code segment should you use?
    A. SPFeatureCollection featuresCollect = SPContext.Current.SiteFeatures;
    featuresCollect.Add(new Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"), true);
    B. SPFeatureCollection featuresCollect = SPContext.Current.WebFeatures;
    featuresCollect.Add(new Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"), true);
    C. SPSite web = new SPSite("http://www.contoso.com/hr");
    SPFeatureCollection featureCollect = web.Features;
    featureCollect.Add(new Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"), true);
    D. SPWeb web = new SPSite("http://www.contoso.com/hr").OpenWeb();
    SPFeatureCollection featureCollect = web.Features;
    featureCollect.Add(new Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"), true);

    解析:
     我们知道Feature是包括了 Microsoft SharePoint Foundation 中功能的一项或多项扩展的容器。功能包含 Feature.xml 文件和一个或多个元素文件。这些 XML 文件也称为功能定义。
       若干个功能聚合在一起就组成了功能集合,功能集合存储在 SPFeatureCollection 对象中。可通过使用 SPWebService、SPWebApplication、SPSite 和 SPWeb 对象的 Features 属性来访问 SPFeatureCollection。如果功能出现在集合中,则该功能已在指定的范围内激活。(此句就是选项A.B所做的事情:即声明一个SPFeatureCollection变量,然后对其赋值,选项A给featuresCollect赋予了Site(网站集)级别的功能集。选项B给featuresCollect赋予了Web(或subsite 即网站)级别的功能集。然后选项A.B再使用SPFeatureCollection类的Add方法添加一个新的Feature到这个功能集合对象中。所以指定的Feature被Add方法添加到对应的SPFeatureCollection功能集合对象中时,也就意味着此Feature在功能集合对象所对应的范围内被激活了,也即:选项A添加的Feature在网站集(Site)级别被激活,选项B添加的Feature则在网站(Web)级别被激活了。
       至于本题所要求的不要造成内存泄漏问题,这个是通过SPContext来实现的。因为在Sharepoint项目中,需要你关注的引起内存泄漏问题的对象通常是你在代码中Create的对象,至于SPContext它不是由你Create的,系统会自动对它进行Dispose。所以不会造成所谓的内存泄漏问题。如果你强行去Dispose此类不是由你Create的对象,反而会造成潜在的报错,例如报错:” Trying to use an SPWeb object that has been closed or disposed and is no longer valid.”
       所以,通过上面的分析,我们就可以认为选项B是本题的答案了。
       选项C. D是错误的用法,根据微软的文档描述,SPFeatureCollection的正确获取Features的方法应该是:
     “Use the SiteFeatures or WebFeatures property of the SPContext class to get the collection of activated Features for the current site collection or site.”  即:通过SPContext的SiteFeatures 或者WebFeatures属性来获取当前上下文中所激活的功能集。强调是要通过”当前上下文SPContext”去获取。而选项C.D则是试图从一个新创建的SPSite对象中获取
      而且选项C. D 中new SPSite("Absolute_URL")中的Absolute_URL使用的是Subsite的URL,而不是Site的URL,所以也会出错。
    所以本题目正确选项应该是B

    参考:
    http://msdn.microsoft.com/zh-cn/library/ee537350(v=office.14).aspx
    http://msdn.microsoft.com/zh-cn/library/ms466305(v=office.14).aspx
    http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spcontext.webfeatures.aspx
    http://msdn.microsoft.com/zh-cn/library/microsoft.sharepoint.spsite.aspx


    Question 59
    You create a Web Part that takes three values from three text boxes and creates a new SharePoint site when you click a button named CreateNewSite.
    The Web Part contains the following code segment.
    protected void CreateNewSite_Click(object sender, EventArgs e)
    {
        SPSite site = SPContext.Current.Site;
        SPWeb web = site.AllWebs.Add(SiteNameTextBox.Text, SiteTitleTextBox.Text, SiteDescriptionTextBox.Text, 0, SPWebTemplate.WebTemplateSTS, false, false);
    }

    You test the Web Part and it works properly.
    When another user attempts to use the Web Part to create a new site, he receives the following error message: "Error: Access Denied."
    You need to ensure that users can use the Web Part to create new sites.
    What should you do?
    A. Add web.Update() after the code segment.
    B. Add web.ValidatesFormDigest() after the code segment.
    C. Run the code segment inside a SPSecurity.RunWithElevatedPrivileges delegate.
    D. Add the following code after the code segment:
    SPUser currentUser = System.web.CurrentUser;
    web.Users.Add(currentUser.LoginName, currentUser.Email, currentUser.Name, "");

    解析:
      本题描述那么多,其实就是说想通过用户点击Button来创建一个Site,而创建Site时需要相应的参数,这些参数从Text Box中获取,也即参数值由用户输入。
    遇到的主要问题是:一个用户创建成功(说明代码在实现功能上没有问题),而另一个用户则遇到了”Access Denied”报错。所以,很显然,本题的答案就在于解决”Access Denied”报错上。
       根据报错信息,可以很容易判断这是一个由于操作权限不足引起的错误。所以解决问题的方法理所当然的集中在如何获取足够的权限上。
       分别分析各选项:
    选项A. web.Update()实现的是把对Web Site的更改写入到对应的数据库中,完成的是对SPWeb对象的更新。所以与本题无关(本题是想新创建一个SPWeb)。
    选项B. Add web.ValidatesFormDigest()方法。我们知道: 出于安全考虑,默认情况下,Microsoft SharePoint Foundation 不允许从 Web 应用程序进行发布以修改数据库内容(如List内容,List的内容当然是保存在内容数据库Content Database里的),除非请求页上包括安全验证。Sharepoint可以通过在请求页上添加页面指令和 FormDigest 控件来更新单个网站或网站集的数据。在 ASPX 页上插入此控件会生成安全验证或消息摘要,以帮助防止在用户因被骗而发布数据到服务器时产生的攻击。安全验证特定于用户、网站和时间段,并在可配置的时间段后过期。当用户请求页时,服务器返回插入了安全验证的页。当用户随后提交表单时,服务器会验证安全验证是否更改。 而本选项的web.ValidatesFormDigest()方法就是用来Validate此页面上的FormDigest 控件的。 FormDigest问题针对的是整个页面,而不是针对不同的用户。所以,本选项与权限提升无关。
    选项C. Run the code segment inside a SPSecurity.RunWithElevatedPrivileges delegate. 这正是用来提升用户权限的工具。一般情况下我们使用VS2010开发工具开发的SharePoint项目,在部署到服务器上之后都会以当前账户来运行编写的代码所调用的资源。但是又一种情况发生了,SharePoint2010中有许多的网站、列表、文档库等资源,但并不是每个当前用户都是可以访问到的。例如:一个存储每个员工薪资的列表,这个列表肯定不会让公司内所有人员都能看到的,只能让少部分人去访问,但是如果在开发的一段代码中调用了这个列表的资源,而当前登录系统的这个账户却不能访问这个列表,因此如果这个账户运行了这段代码,就会得到一个禁止访问的错误信息。为了解决这样的问题,我们可以采取权限提升的方法强制这段代码以”管理员账户身份”运行。需要注意的 SPSecurity. RunWithElevatedPrivileges的运行必需在一个新的上下文中执行,意思是说必需要New一个新的SPSite与SPWeb才可以。
    所以此选项就是本题的答案。
    选项D. 完成的是添加一个新的SPUser到Web中,很显然这种操作与提升权限没有关联。
    所以本题目正确选项应该是C

    参考:
    http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx
    http://msdn.microsoft.com/zh-cn/library/microsoft.sharepoint.spweb_methods(v=office.12).aspx

  • 相关阅读:
    linux系统下MySQL表名区分大小写问题
    linux下查看Mysql默认编码、修改默认编码
    mysql的下载及安装(windows)
    数据库进阶
    mysql练习
    数据库基础
    jQuery
    JavaScript 基础学习(二)
    344.Reverse String
    计蒜客课程数据结构(顺序表)
  • 原文地址:https://www.cnblogs.com/wsdj-ITtech/p/3137166.html
Copyright © 2011-2022 走看看