zoukankan      html  css  js  c++  java
  • 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/

  • 相关阅读:
    科技巨头争抢的“超级账本”,到底是个什么组织?
    区块链结合教育,将给教育行业带来哪些变革?
    国家区块链战略开启,教育行业应对几何?
    区块链如何改变教育
    区块链技术在教育领域的应用模式与现实挑战
    知乎-区块链技术运用于教育有多少种可能?
    区块链+教育,让教育行业充满希望
    教育区块链应用案例【2019】
    区块链在教育行业的落地应用现状介绍
    PowerShell 搜索文件编码格式
  • 原文地址:https://www.cnblogs.com/ITHelper/p/1634957.html
Copyright © 2011-2022 走看看