MOSS: SPListItem.Update() throws error Operation is not valid due to the current state of the object...

问题:

在SPSecurity.RunWithElevatedPrivileges 代码块中执行SPListItem.Update(),

SPSecurity.RunWithElevatedPrivileges

                (

                    () =>

                    {

                        using (SPSite site = new SPSite("web url"))

                        {

                            using (var tempWeb = site.OpenWeb())

                            {

                                var list = tempWeb.Lists["mylist"];

                                var Item = list.Items[0];

                                Item["Title"] = "new Title";

                                Item.Update();

                            }

                        }

                    }

                ); 

将会得到如下错误:Operation is not valid due to the current state of the object.

 

 解决此问题有两个办法:

1。Item.Update();放到SPSecurity.RunWithElevatedPrivileges语句块的外面。

 

SPListItem Item = null;                

      SPSecurity.RunWithElevatedPrivileges

                (

                    () =>

                    {

                        using (SPSite site = new SPSite("web url"))

                        {

                            using (var tempWeb = site.OpenWeb())

                            {

                                var list = tempWeb.Lists["mylist"];

                                Item = list.Items[0];                               

                            }

                        }

                    }

                );

      Item["Title"] = "new Title";

      Item.Update();

 详情可参考老外的原文:http://littletalk.wordpress.com/2009/04/03/moss-splistitemupdate-throws-error-operation-is-not-valid-due-to-the-current-state-of-the-object/

 

2. 第二个方法是在new SPSite(),SPWeb时不能Hard Code URL类似于以上的代码,

应该使用如下方式:

        var webContext = SPContext.Current.Web; 

        SPSecurity.RunWithElevatedPrivileges

                (

                    () =>

                    {

                        using (SPSite site = new SPSite(webContext.Site.ID))

                        {

                            using (var tempWeb = site.OpenWeb(webContext.ID))

                            {

                                var list = tempWeb.Lists["mylist"];

                                var Item = list.Items[0];

 

                                Item["Title"] = "new Title";

                                Item.Update();

                            }

                        }

                    }

                );

但是此方法有一个问题:在非Web应用程序的中,如控制台程序,以上代码将出现错误,因为SPContext.Current.Web为Null。

 详情可参考老外的原文:http://vspug.com/ssa/2007/11/25/operation-is-not-valid-due-to-the-current-state-of-the-object/

 

转载于:https://www.cnblogs.com/ITHelper/archive/2009/12/29/1634957.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值