Copied to clipboard

Flag this post as spam?

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


  • Max Mumford 266 posts 293 karma points
    Sep 13, 2011 @ 17:23
    Max Mumford
    0

    New document xml not updating

    Hi all,

    After creating a new document according to the reference doc below:

    http://our.umbraco.org/wiki/reference/api-cheatsheet/publishing-and-republishing

    and publishing / updating the xml with the following code:

     

    //Get the document by it's ID
    Document d = new Document(1234);

    //Mark it ready for publishing
    d
    .Publish(new umbraco.BusinessLogic.User(0));

    //Tell the runtime to update the document cache
    umbraco
    .library.UpdateDocumentCache(d.Id);

    Then trying to access this new document results in the document not being found because it is not in the XML.

    If I add the following code after  the code above:

    Server.ScriptTimeout = 100000;
    umbraco.cms.businesslogic.web.Document.RePublishAll();
    umbraco.library.RefreshContent();

    It works.

    (Umbraco 4.7)

    Any ideas why this is happening?

    Thanks,

    Max. 

     

  • Jesper Hauge 298 posts 487 karma points c-trib
    Sep 13, 2011 @ 17:26
    Jesper Hauge
    0

    Yes - this is a well known behaviour.

    You can find an explanation in my answer to this question:

    http://our.umbraco.org/forum/developers/api-questions/18076-Creating-document-through-API-does-not-update-config-correct

    Regards
    Jesper Hauge 

  • Max Mumford 266 posts 293 karma points
    Sep 13, 2011 @ 17:51
    Max Mumford
    0

    Thanks for the reply. I have read through the other thread (Didn't even remember writing my own posts!) and it threw some light on the situation, however I should say that I am trying to access the new document from a completely different page. According to the post you linked me to, that shouldnt cause a problem:

    So doing a Document.Publish() in some server side code, like in a usercontrol or something like that, and then showing a page that contains xslt macros, shouldn't cause any problems

     

    Here is exactly what I am doing:

    on the document "createnewnode" I have a usercontrol with the following code:

    DocumentType dt = DocumentType.GetByAlias("documenttype");
    String docName = "test";
    Document doc = Document.MakeNew(docName, dt, new User(0), 1134); 
    doc.Publish(new User(0));
    doc.getProperty("x").Value = strx;
    doc.getProperty("y").Value = stry;
    umbraco.library.UpdateDocumentCache(doc.Id);

     

    Then later, the user navigates to a document called "getnodeinfo" with a usercontrol with the following code:

    XPathNodeIterator getNodes = umbraco.library.GetXmlNodeByXPath("//documenttype[@isDoc and x= '" + strx + "']");

     

    The count property of getNodes is 0 after running the create document code. Only after I go to localhost/umbraco/dialogs/republish.aspx?xml=true or add the following code, does it find the actual nodes:

    Server.ScriptTimeout = 100000;
    umbraco.cms.businesslogic.web.Document.RePublishAll();
    umbraco.library.RefreshContent();

    The same behaviour occurs when running the equivalent XSLT query from an xslt macro...

  • Jesper Hauge 298 posts 487 karma points c-trib
    Sep 14, 2011 @ 20:41
    Jesper Hauge
    0

    Hi again Max,

    You are publishing the document before setting the properties in your code, which means the property values are not saved to the database. This again means that the xml node will have no data in the x property, and your XPath statement will not select anything.

    Change the usercontrol code to:

    Document doc = Document.MakeNew(docName, dt, new User(0), 1134);
    doc.getProperty("x").Value = strx;
    doc.getProperty("y").Value = stry;
    doc.Publish(new User(0));
    umbraco.library.UpdateDocumentCache(doc.Id); 

    And you should be good to go. The reason it works this way is that doc.Publish saves the document in the current state to the DB and marks it as published, and the library.UpdateDocumentCache() takes the published content in DB and updates the XML cache.

    Regards
    Jesper Hauge 

  • Max Mumford 266 posts 293 karma points
    Sep 15, 2011 @ 11:11
    Max Mumford
    0

    Haha, yep that did it! So do I need to republish a node every time I change one of it's properties for it to be fully propagated to the database and xml?

  • Jesper Hauge 298 posts 487 karma points c-trib
    Sep 15, 2011 @ 11:14
    Jesper Hauge
    0

    Yep, changing the property value of a document, triggers neither a Save nor a Publish operation. You're working on an in-memory object alone.

    Regards
    Jesper 

  • Max Mumford 266 posts 293 karma points
    Sep 15, 2011 @ 11:19
    Max Mumford
    0

    I see. And what about members since they do not have a publish method? 

  • Jesper Hauge 298 posts 487 karma points c-trib
    Sep 15, 2011 @ 23:24
    Jesper Hauge
    0

    If you look at the samples floating around you'll see that it is now generally advised that you use the asp.net membership-, role- and profileproviders to interact with the members in the member section. If you're only working with the members as normal membership users you can use the methods available in the .NET System.Web.Security.Membership class, and if you need to access and modify the properties set up with the Member type editor in the Umbraco backend you'll need to use the System.Web.Profile.ProfileBase class. 

    I put up an example of the configuration and code you could use for that here: https://bitbucket.org/jhauge/codesamples/src/tip/Umbraco/Membership/

    Remember that the names of the profile properties used in your web.config file, has to be the same as the aliases of the properties you configure for your member type in Umbraco.

    Regards
    Jesper Hauge 

Please Sign in or register to post replies

Write your reply to:

Draft