zoukankan      html  css  js  c++  java
  • MOSS: SPSecurity.RunWithElevatedPrivileges提升权限来新增列表条目示例

    在前面的文章中有说到,需要对WSS进行高级别的操作时,我们可以使用SPSecurity.RunWithElevatedPrivileges来提高操作权限级别!

    直接使用--SPSecurity.RunWithElevatedPrivileges(delegate() {代码});提高权限将是让代码以sharepoint\system所具有的权限来执行!

    现在让我们来做一个示例:

    MOSS服务器: http://server01/

    该服务器上有一个自定义列表: TEST

    该列表只有一个列: Title

    OK,准备好了, 我需要将我的程序做成WebServices供其它程序调用!

    WebServices功能为: 简单的往列表中新增一个条目.

    好了,开始写代码:

    WebServices项目的OPList.asmx.cs:

    using System;
    using System.Data;
    using System.Web;
    using System.Collections;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    using System.ComponentModel;

    using Microsoft.SharePoint;

    namespace WebService
    {
        /// <summary>
        /// OPList 的摘要说明
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [ToolboxItem(false)]
        public class OPList : System.Web.Services.WebService
        {

            [WebMethod]
            public string HelloWorld()
            {
                return "Hello World";
            }

            [WebMethod]
            public string NewItem()
            {
                string retVal = string.Empty;

                try
                {

                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        using (SPSite site = new SPSite("http://Server01/"))
                        {
                            using (SPWeb web = site.RootWeb)
                            {
                                SPList list = web.Lists["TEST"];
                                SPListItem item = list.Items.Add();
                                item["Title"] = string.Format("Test at {0}", DateTime.Now.ToString());
                                item.Update();
                            }
                        }
                        retVal = "执行成功!";
                    });
                }
                catch (Exception ex)
                {
                    retVal += ex.Message;
                }

                return retVal;
            }
        }
    }

    这段代码很简单, 只包括一个HelloWorld()和一个添加列表条目的NewItem().

    好了,代码完成! 这段代码不管从语法上来说, 还是从功能上说都是完整而且正确的!

    做到这一点, 我们需要在另一个项目中引用这个Webserveices以希望它能工作!

    (另一项目略,因为很简单,只是引用,其后调用NewItem()就可以了)

    OK.

    看一下执行结果: 很遗憾, HelloWorld()能正确执行, 但NewItem()却报错,大致错误意思是"操作在该范围内为无效操作";

    看代码, 我们几乎找不到任何错误的写法!

    可为什么呢? 起先怎么也找不到出问题的原因!

    分析: 我们知道SPSecurity.RunWithElevatedPrivileges,这个是需要在new SPSite(...)的时候才会去提升权限, 可以看出提升的权限是对SPSite, SPWeb的!

    于是我将WebServices代码改写如下:

    using System;
    using System.Data;
    using System.Web;
    using System.Collections;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    using System.ComponentModel;

    using Microsoft.SharePoint;

    namespace WebService
    {
        /// <summary>
        /// OPList 的摘要说明
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [ToolboxItem(false)]
        public class OPList : System.Web.Services.WebService
        {

            [WebMethod]
            public string HelloWorld()
            {
                return "Hello World";
            }

            [WebMethod]
            public string NewItem()
            {
                string retVal = string.Empty;

                try
                {
                    SPSite site = null;
                    SPWeb web = null;

                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        site = new SPSite("http://Server01/");
                        web = site.RootWeb;
                    });

                    SPList list = web.Lists["TEST"];
                    SPListItem item = list.Items.Add();
                    item["Title"] = string.Format("Test at {0}", DateTime.Now.ToString());
                    retVal += web.CurrentUser.Name;
                    item.Update();


                    retVal += "执行成功";
                }
                catch (Exception ex)
                {
                    retVal += ex.Message;
                }

                return retVal;
            }
        }
    }

    此段代码,只是把对List的操作移到SPSecurity.RunWithElevatedPrivileges();外面!

    再运行我们的测试项目!

    界面上见到了久违的"执行成功"! 再检查一个TEST列表,也是多出了一个Item!

    到此, 程序正常运行!

    附:

    不知道什么原因, 在SPSecurity.RunWithElevatedPrivileges();代码段内,可以做读取操作, 比如SPListItem item = list.Items[0];这些操作完全是可以的!但我们的item.Update()却无法执行!所以我们只能将其移到SPSecurity.RunWithElevatedPrivileges();外来执行! 

  • 相关阅读:
    图片的存储
    【已解决】如图,说我磁盘不够,看到var目录下有的个隐藏文件夹占了46G,不知道怎么删除
    Java 中时间处理SimpleDateFormat 中HH和hh的区别
    api的日志
    网页报警提示 This page includes a password or credit card input in a non-secure context. A warning has been added to the URL bar. For more information, see https://goo.gl/zmWq3m.
    Springboot项目中的favicon
    页面Header自适应屏幕
    Vue 日期下拉框
    VUE 单选下拉框Select中动态加载 默认选中第一个
    Vue 循环 [Vue warn]: Avoid using non-primitive value as key
  • 原文地址:https://www.cnblogs.com/llbofchina/p/1308538.html
Copyright © 2011-2022 走看看