tag:blogger.com,1999:blog-7258322562721325894.post7611702518068786731..comments2023-08-17T13:27:47.119+03:00Comments on How To Code: MOSS: SPListItem.Update() throws error Operation is not valid due to the current state of the object.Jussi Palohttp://www.blogger.com/profile/00392327346825133905noreply@blogger.comBlogger40125tag:blogger.com,1999:blog-7258322562721325894.post-54765685308199483572011-10-14T13:58:10.877+03:002011-10-14T13:58:10.877+03:00the exact reason the SPWEB in Jussi code created o...the exact reason the SPWEB in Jussi code created outside the delegate.<br /><br />Read this :<br />An SPSite object created outside the delegate can be referenced inside the delegate, however, the methods and property assessors of the object run with the privileges of the user context in which the objects were created, not with the elevated privileges. The same point applies to SPWeb objects and any other objects. You must create new objects inside the delegate if you need to execute the members of the objects with elevated privileges. If the new object must represent the same persisted entity as an object created outside the delegate, then you must reference identification information from the externally created object and use it to create the new object within the delegate. For example, if web is a reference to an SPWeb object created before the call to RunWithElevatedPrivileges, then the following code shows you would use the ID of its parent SPSite object to construct a new SPSite object.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7258322562721325894.post-55535325254897062902011-10-14T13:56:12.244+03:002011-10-14T13:56:12.244+03:00http://msdn.microsoft.com/en-us/library/microsoft....http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspxAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-7258322562721325894.post-16446509089116471482011-10-14T13:35:26.385+03:002011-10-14T13:35:26.385+03:00The exact reason that the SPWEB object created out...The exact reason that the SPWEB object created outside the delegate<br /><br />read this from Microsoft :<br /><br />An SPSite object created outside the delegate can be referenced inside the delegate, however, the methods and property assessors of the object run with the privileges of the user context in which the objects were created, not with the elevated privileges. The same point applies to SPWeb objects and any other objects. You must create new objects inside the delegate if you need to execute the members of the objects with elevated privileges. If the new object must represent the same persisted entity as an object created outside the delegate, then you must reference identification information from the externally created object and use it to create the new object within the delegate. For example, if web is a reference to an SPWeb object created before the call to RunWithElevatedPrivileges, then the following code shows you would use the ID of its parent SPSite object to construct a new SPSite object. <br /><br />http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspxAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-7258322562721325894.post-41387762210874011452011-09-02T16:37:33.237+03:002011-09-02T16:37:33.237+03:00Thanks ! Simple workaround.
Though I do not under...Thanks ! Simple workaround. <br />Though I do not understandthe exact reason. And I guess whenwe call SPContext.Current before this, we do not face any issues. If someone can let me know why we have such problem it would be great.<br /><br />ThanksAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-7258322562721325894.post-8901264848471753632011-04-06T17:49:19.764+03:002011-04-06T17:49:19.764+03:00This is a bug in SharePoint which is corrected in ...This is a bug in SharePoint which is corrected in SharePoint 2010. One work-around is to get SPContext.Current before elevating privileges. I don't like the way you do it in your post. You work around it but do not know why or how it works. Also, your code becomes harder to read. Try using reflector on SPControl.SPWebEnsureSPControl and SPListItem.AddOrUpdateItem and you may understand better what is happening.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7258322562721325894.post-32286039228442359362011-03-10T16:02:56.635+02:002011-03-10T16:02:56.635+02:00Thanks for a post. It was helpful :)Thanks for a post. It was helpful :)Anonymoushttps://www.blogger.com/profile/08877119160241745765noreply@blogger.comtag:blogger.com,1999:blog-7258322562721325894.post-19084983216659608212011-01-08T13:54:35.313+02:002011-01-08T13:54:35.313+02:00Thanks to Stefan.
You saved my day.Thanks to Stefan.<br />You saved my day.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7258322562721325894.post-58936321185086979802010-12-19T09:27:15.700+02:002010-12-19T09:27:15.700+02:00@Jussi
It would work inside SPSecurity.RunWithElev...@Jussi<br />It would work inside SPSecurity.RunWithElevatedPriveleges<br /><br />if you app-pool account has the permission to do that.Nikhil Doomrahttps://www.blogger.com/profile/06324029764305548684noreply@blogger.comtag:blogger.com,1999:blog-7258322562721325894.post-46269309784693881712010-10-25T10:44:21.387+03:002010-10-25T10:44:21.387+03:00@Nikhil: Yes, that is how it should be done, but i...@Nikhil: Yes, that is how it should be done, but it just won't work like that if you use SPListItem.Update inside SPSecurity.RunWithElevatedPrivileges.Jussi Palohttps://www.blogger.com/profile/04625491599540982394noreply@blogger.comtag:blogger.com,1999:blog-7258322562721325894.post-41994102243800144832010-10-25T09:30:09.963+03:002010-10-25T09:30:09.963+03:00This is not a work around. Instead this is the way...This is not a work around. Instead this is the way you should write a code.<br /><br />When you are instantiating SPWeb object it opens the web with the current credentials.<br /><br />So, when you call SPListItem.Update() method it uses the privileges of that user, even if you are writing code inside SPSecurity.RunWithElevatedPriveleges()<br /><br />So, correct method is when you use <br />SPSecurity.RunWithElevatedPriveleges() you should instantiate SPWeb object inside the delegate of SPSecurity.RunWithElevatedPriveleges(). For ex:<br /><br />SPSecurity.RunWithElevatedPrivileges(delegate()<br /> {<br /> try<br /> {<br /> using(SPSite site = new SPSite(SPContext.Current.Site.Id)){<br /> using(SPWeb web = site.OpenWeb()){<br /> //your code here.<br /> }<br /> }<br /> }<br /> catch{<br /> }<br /> finally{}<br /> });Nikhil Doomrahttps://www.blogger.com/profile/06324029764305548684noreply@blogger.comtag:blogger.com,1999:blog-7258322562721325894.post-81836936112122697522010-10-10T10:39:42.858+03:002010-10-10T10:39:42.858+03:00Thanks for providing the solution.
Great.I was st...Thanks for providing the solution.<br /><br />Great.I was struggling with this for 2 hours.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7258322562721325894.post-42960241637929508512010-09-01T17:13:02.083+03:002010-09-01T17:13:02.083+03:00Great approach to the issue and very simple to imp...Great approach to the issue and very simple to implement. Thanks a lot.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7258322562721325894.post-15891611550434057152010-07-23T12:36:11.859+03:002010-07-23T12:36:11.859+03:00Try this method of security elevation. (Andrew Mai...Try this method of security elevation. (Andrew Maisey)<br /><br />SPSite objInitialSite = new SPSite("http://site/");<br /> SPUser objUser = objInitialSite.SystemAccount;<br /><br /> objInitialSite.Dispose();<br /> //SPSecurity.RunWithElevatedPrivileges(delegate<br /> //{<br /> using (SPSite site = new SPSite("http://site/", objUser.UserToken))<br /> {<br /> using (SPWeb web = site.OpenWeb())<br /> {<br /> web.AllowUnsafeUpdates = true;<br /> SPList list = web.Lists["Suppliers"];<br /> SPListItem newItem = list.Items.Add();<br /><br /> newItem["Supplier Name"] = txtSupplierName.Text;<br /> newItem["Category"] = cboCategory.SelectedItem.ToString();<br /> newItem["SAP Vendor Number"] = txtSAPVendorNo.Text;<br /> newItem["SAP Man Vendor Number"] = txtSAPManVendorNo.Text;<br /> newItem["Website"] = txtWebsite.Text;<br /> newItem["Notes"] = string.Empty;<br /> newItem["Link"] = "http://is/";<br /> newItem["Buyer"] = cboBuyer.SelectedItem.ToString();<br /><br /> newItem.Update();<br /> web.AllowUnsafeUpdates = false;<br /> }<br /> }Unknownhttps://www.blogger.com/profile/13388316510375266737noreply@blogger.comtag:blogger.com,1999:blog-7258322562721325894.post-63383570199651634422010-04-29T18:31:59.501+03:002010-04-29T18:31:59.501+03:00Thanks for posting this. It saved a lot of time..
...Thanks for posting this. It saved a lot of time..<br />-VaniKadambarihttps://www.blogger.com/profile/05523581417896936382noreply@blogger.comtag:blogger.com,1999:blog-7258322562721325894.post-37566079135071946392010-02-22T13:57:56.068+02:002010-02-22T13:57:56.068+02:00THANKS. My Problem was, that the Code was running ...THANKS. My Problem was, that the Code was running from an application page, but not from an own webservice. Your workaround is working for both. THANKS!!!Hollinoreply@blogger.comtag:blogger.com,1999:blog-7258322562721325894.post-69584116938996308212010-01-21T18:18:20.268+02:002010-01-21T18:18:20.268+02:00Actually there is a much easier solution:
It seem...Actually there is a much easier solution:<br /><br />It seems to me that RunWithElevatedPrivileges() has a bug such that it does NOT use the Context of the new user. <br />You can force it to do so by simply setting the context to null, like here:<br /><br /> SPSecurity.RunWithElevatedPrivileges(delegate()<br /> {<br /> HttpContext.Current = null;<br /> using (SPSite site = new SPSite(targetWeb))<br /> {<br /> using (SPWeb web = site.OpenWeb())<br /> {<br /> SPList targetlist = web.Lists[targetList];<br /> foreach (ItemType item in import.item)<br /> {<br /> ....<br /> }<br /> }<br /> }<br /> });<br /><br />best regards,<br />Stefan MaderAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-7258322562721325894.post-27095175613360055342009-11-18T21:45:42.419+02:002009-11-18T21:45:42.419+02:00Thank you very much..
It worked for me !!
You s...Thank you very much..<br /><br />It worked for me !! <br /><br />You saved my day !!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7258322562721325894.post-67622468337300200002009-11-04T11:32:31.149+02:002009-11-04T11:32:31.149+02:00Thanks for saving my time and it was really great ...Thanks for saving my time and it was really great work.Aamir Bashirhttps://www.blogger.com/profile/17567972513435463601noreply@blogger.comtag:blogger.com,1999:blog-7258322562721325894.post-41345949168389877392009-09-16T13:19:21.784+03:002009-09-16T13:19:21.784+03:00It clearly shouldn't work this way, but it doe...It clearly shouldn't work this way, but it does. Fantastic help, many thanksphil@uclogic.comhttps://www.blogger.com/profile/11490670803307331752noreply@blogger.comtag:blogger.com,1999:blog-7258322562721325894.post-48246459917080494702009-08-25T18:15:02.500+03:002009-08-25T18:15:02.500+03:00Hello There,
I'm in need of help. I'm get...Hello There,<br /><br />I'm in need of help. I'm getting the same error when I want to deploy the sharepoint project! and I can't have access to my SharePoint site!<br />Can you help me in this regard? what should I do??<br /><br />please email me at s0lmzz@yahoo.comAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-7258322562721325894.post-52960307584314473592009-07-10T19:59:44.028+03:002009-07-10T19:59:44.028+03:00Excellent!!
Thank you for this great workarround. ...Excellent!!<br />Thank you for this great workarround. It works!Filhos, Laços e Abraçoshttps://www.blogger.com/profile/15591892462341578290noreply@blogger.comtag:blogger.com,1999:blog-7258322562721325894.post-43206334851150224652009-05-21T21:20:05.067+03:002009-05-21T21:20:05.067+03:00The magic of the Internet and Jussi.
Thanks.The magic of the Internet and Jussi.<br /><br />Thanks.Tanyanoreply@blogger.comtag:blogger.com,1999:blog-7258322562721325894.post-5819683547945168792008-12-14T05:02:00.000+02:002008-12-14T05:02:00.000+02:00wow. this is in absolutely none of the books and w...wow. this is in absolutely none of the books and when i first saw it i thought it wouldnt work. except it does and it fixed my problem. <BR/><BR/>yew da man.....homerhudsonhttps://www.blogger.com/profile/07374817558769207593noreply@blogger.comtag:blogger.com,1999:blog-7258322562721325894.post-497189550179834562008-10-06T22:20:00.000+03:002008-10-06T22:20:00.000+03:00Great solution!!!Great solution!!!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7258322562721325894.post-51252844980343799872008-08-21T18:06:00.000+03:002008-08-21T18:06:00.000+03:00You rock man..Spent one full day to figure out why...You rock man..Spent one full day to figure out why the list Title was not getting updated. List.Update somehow was not working inside the "RunWithElevatedPrevilages" and your code solved my problem.Anonymousnoreply@blogger.com