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();外来执行! 

原文地址:https://www.cnblogs.com/llbofchina/p/1308538.html