none
Operation is not valid due to the current state of the object

    Question

  •  Operation is not valid due to the current state of the object.   at Microsoft.SharePoint.WebControls.SPControl.SPWebEnsureSPControl(HttpContext context)
       at Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(HttpContext context)
       at Microsoft.SharePoint.SPContext.get_Current()

    Getting error while uploading the document and then updating the item.

    SPFile oFileNew = folderFile.Add(strDestUrl, strFile, properties,true);
    SPListItem oListItem = oFileNew.Item;
    if (properties["FileCreatedDate"] != null)

    oListItem[
    "Date"] = properties["FileCreatedDate"];
    }
    oListItem.Update(); //Error

    Need help...

    Friday, December 05, 2008 1:40 PM

Answers

  • Adding the following code before updating the item has solved the problem. 

    if (HttpContext.Current != null)
    {
    if (HttpContext.Current.Items["HttpHandlerSPWeb"] == null)
    {
    HttpContext.Current.Items["HttpHandlerSPWeb"] = web
    }
    }
    web.AllowUnsafeUpdates =
    true;

    • Marked as answer by Shobha P Wednesday, December 10, 2008 10:20 AM
    Wednesday, December 10, 2008 10:20 AM

All replies

  • As you are clearly updating the document with code, this is not a question for the Admin forum (which it would be if you were getting an error when using the UI).

    Moving to the Programming forum.
    Friday, December 05, 2008 1:58 PM
  • Have you checked if properties["FileCreatedDate"] has valid data?
    Could you try testing if instead of executing that line executing this: oListItem["Title"] = "TestTitle";

    If this line works, you know the FileCreatedDate is either corrupt or in an invalid format.


    Hope this helps,

    Jeroen
    My blog on WSS / MOSS development is found at http://jebass.blogspot.com
    Friday, December 05, 2008 2:30 PM
  • properties["FileCreatedDate"] contains DateTime value.
    Friday, December 05, 2008 2:57 PM
  • Could you still try replacing

    oListItem["Date"] = properties["FileCreatedDate"];
    with
    oListItem["Title"] = properties["FileCreatedDate"].ToString(); // Or just "Sometext"


    It's a small effort, but it can reveal potential formatting issues.


    Regards,

    Jeroen
    My blog on WSS / MOSS development is found at http://jebass.blogspot.com
    Friday, December 05, 2008 3:10 PM
  • Adding the following code before updating the item has solved the problem. 

    if (HttpContext.Current != null)
    {
    if (HttpContext.Current.Items["HttpHandlerSPWeb"] == null)
    {
    HttpContext.Current.Items["HttpHandlerSPWeb"] = web
    }
    }
    web.AllowUnsafeUpdates =
    true;

    • Marked as answer by Shobha P Wednesday, December 10, 2008 10:20 AM
    Wednesday, December 10, 2008 10:20 AM
  • Hi Thanks a lot i tried this option and it worked perfect for me.
    yes but i used oWeb.AllowUnsafeUpdates = true; at many places.

    SPSite

    oSite = new SPSite(SiteUrl);

     

    SPWeb oWeb = oSite.OpenWeb();
    oWeb.AllowUnsafeUpdates = true;

     

    SPSecurity.RunWithElevatedPrivileges(delegate()

    {

     

    try

    {

    oWeb.AllowUnsafeUpdates =

    true;

     

    SPList oList = oWeb.Lists["MYLIST"];

    oWeb.AllowUnsafeUpdates =

    true;

     

    SPListItem oItem = oList.Items.Add();

    oWeb.AllowUnsafeUpdates =

    true;

    oItem[

    "PAGEURL"] = Pageurl;

    oItem[

    "RATING"] = Rating;

    oItem[

    "LOGINNAME"] = LoginName;

    oItem[

    "COMMENTS"] = "SOME COMMENTS";

    oWeb.AllowUnsafeUpdates =

    true;

     

    if (HttpContext.Current != null)

    {

     

    if (HttpContext.Current.Items["HttpHandlerSPWeb"] == null)

    {

     

    HttpContext.Current.Items["HttpHandlerSPWeb"] = oWeb;

    }

    }

    oItem.Update();

     

     

    }

     

    catch (Exception ex)

    {

    Response.Write(ex.Message.ToString());

    }

    });

    Wednesday, December 16, 2009 10:13 AM