Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Michael Friedrich 20 posts 97 karma points
    Feb 21, 2011 @ 17:18
    Michael Friedrich
    0

    Error if create an document and call doc.save()

    I try to create a document in a user control. The code to create the item looks like this:

    Dim td As DocumentType = DocumentType.GetByAlias("eventItem")
    Dim author As User = User.GetUser(0)
    Dim doc As Document = Document.MakeNew(txtEventName.Text, td, author, 1119)
    doc.getProperty("bodyText").Value = rteDescription.Content
    doc.getProperty(
    "startDate").Value = txtDate.Text
    doc.getProperty(
    "showOnHomepage").Value = False
    doc.Publish(author)
    umbraco.
    library.UpdateDocumentCache(doc.Id)

    The documenttype alias "eventItem" is configured to be the "itemDocType". The newly created document is now sorted to the create date rather than the configured itemDateProperty (witch should be the "startDate" Property).

    If I cretae an document in the umbraco backend all is working fine. If I try to call the document objects .save() methode the following error occurs:

    [NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.]
       InfoCaster.Umbraco.DateFolders.DateFolders.Document_AfterSave(Document doc, SaveEventArgs e) +1216
       umbraco.cms.businesslogic.web.SaveEventHandler.Invoke(Document sender, SaveEventArgs e) +0
       umbraco.cms.businesslogic.web.Document.FireAfterSave(SaveEventArgs e) +76
       umbraco.cms.businesslogic.web.Document.Save() +201
       MemberAddEvent.cmdSave_Click(Object sender, EventArgs e) in ...\MemberAddEvent.ascx.vb:30
       System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
       System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
       System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

     

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Feb 21, 2011 @ 18:32
    Jan Skovgaard
    0

    Hi Michael

    I'm not sure why the above happens but have you made sure that both the .dll files and usercontrols have been copied over into your site?

    Maybe this wiki-entry can help? http://our.umbraco.org/wiki/reference/api-cheatsheet/creating-a-document

    /Jan

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Feb 21, 2011 @ 20:20
    Stefan Kip
    0

    @Jan; this is an error happening in my DateFolders package, not just an umbraco bug ;-)

    @Michael; I don't know what's wrong. The best way to fix this is by including the DateFolders source instead of the binary in your project and use Visual Studio to debug. The debugger will kick in when an error occurs. That way you can inspect the error and find out what object is null.
    When you do find out, please post the problem more specifically here please :-)

  • Michael Friedrich 20 posts 97 karma points
    Mar 03, 2011 @ 22:44
    Michael Friedrich
    0

    Thanks for the hits, I will try to solve the problem this way and poste here if I find some more infos.

  • Michael Friedrich 20 posts 97 karma points
    Mar 08, 2011 @ 18:24
    Michael Friedrich
    0

    I could start to debug my problem. In the sub Document_AfterSave() the last three lines are causing my problem:

    doc = new Document(doc.Id);
    BasePage .Current.ClientTools.
        ChangeContentFrameUrl(string.Concat("editContent.aspx?id=", doc.Id));

    What ist the doc = new Document(doc.Id) good for? As I understand, it creates/loads the same document again? Does this make any sense? After this line, I get the NullReference Exeption if it tries to get some value of the object (doc.Id in next line).

    If I'm correct, I don't need last command because my document is created by code and not in the backend, so set any URL in the backend makes no sense for me.

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Mar 08, 2011 @ 19:12
    Stefan Kip
    0

    I'm not sure anymore why I'm reloading the document... Should have an explanation :P

    Anyway, you can surround that part with a try/catch block or do fix it the nice way by checking for null values.

  • GFoley83 4 posts 24 karma points
    Dec 03, 2012 @ 04:27
    GFoley83
    0

    I'm having the exact same problem Michael; setting the date using the Document API throw an error on save/publish.
    Has anyone come up with a solution for this?

    ** Update **

    Seems like the DateTime is still actually getting added to the node, despite the error. Swallowing the exception does the trick. Only saw that the solution was mentioned above. 

    Anyway, the following code works for me (as of Umbraco 4.11.1):

    var umDoc = Document.MakeNew(library.StripHtml(heading), dt, author, mediaReleasesNode.Id);

    var summary = library.StripHtml(library.TruncateString(strPTags, 300, "..."));

    umDoc.SetProperty("bodyText", strPTags);
    umDoc.SetProperty("newsSummary", summary);
    umDoc.SetProperty("newsImage", mediaImageId);

    try{
    umDoc.SetProperty("newsDate", newsDate);
    }
    catch (Exception e){ 
    // ignore 
    }

    umDoc.Publish(author);
    library.UpdateDocumentCache(umDoc.Id);

     

  • jacob phillips 130 posts 372 karma points
    Jun 07, 2013 @ 02:11
    jacob phillips
    0

    Thanks GFoley83, I had the same issue too in Umbraco 4.9.1. For me it looks like the error is being thrown when I do a .save()

                    var displayDate = new DateTime(year, month, day, hour, minute, second);
    
                    //to modify the document properties....
                    mdoc.getProperty("displayDate").Value = displayDate;
                    // After modifying the document, prepare it for publishing
                    mdoc.Publish(new umbraco.BusinessLogic.User(0));
    
                    try
                    {
                        mdoc.Save();      
                    }
                    catch (Exception e)
                    {
                        //do nothing, this is a "fix" per the creator of the datefolders package
                        // our.umbraco.org/projects/developer-tools/datefolders/bug-reports/17603-Error-if-create-an-document-and-call-docsave()
                        // result = e.StackTrace.ToString() + "<br /><br />" + e.Message;
                    }
Please Sign in or register to post replies

Write your reply to:

Draft