Copied to clipboard

Flag this post as spam?

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


  • Javier Callico 5 posts 26 karma points
    Sep 25, 2012 @ 17:02
    Javier Callico
    0

    Recommended way of loading a Document object with it's "published" version.

    I want to be able to load a Document object with the content of it's published version. See the method below for a better idea.

     

            private Document GetPublishedDocument(int id)
            {
                Document doc = new Document(true, id);
                if (!doc.HasPendingChanges())
                {
                    return doc;
                }

                foreach (var docVersion in doc.GetVersions().OrderByDescending(v => v.Date))
                {
                    var d = new Document(id, docVersion.Version);
                    if (d.Published)
                    {
                        return d;
                    }
                }

                return null;
            }

     

    Of course this code doesn't work because the Published() property doesn't reflect the status of the version of the Document loaded since it will return true if any version of the given Document has been published.

    Can anybody with experience on the Umbraco API help me?

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Sep 26, 2012 @ 14:44
    Alex Skrypnyk
    0

    Hi Javier,

    Why you don't use Node class ?

    Node get the data which has been published and cached.

    Article about difference between Document and Node below:

    http://our.umbraco.org/wiki/reference/api-cheatsheet/difference-between-node-and-document

    Maybe I misunderstood what you want.

    Thanks,

    Alexandr

  • Javier Callico 5 posts 26 karma points
    Oct 02, 2012 @ 17:50
    Javier Callico
    0

    Hi Alexandr

    Please take a look at the documentService class located on the umbraco.webservices project.

    My problem is that the relevant methods on the Document Service return the latest version of the document but I need the published version.

    Take this method for example: 

            [WebMethod]
            public documentCarrier read(int id, string username, string password)
            {
                Authenticate(username, password);
    
                umbraco.cms.businesslogic.web.Document doc = null;
    
                try
                {
                    doc = new umbraco.cms.businesslogic.web.Document(id);
                }
                catch
                {}
    
                if (doc == null)
                    throw new Exception("Could not load Document with ID: " + id);
    
                documentCarrier carrier = createCarrier(doc);
                return carrier;
            }
    

    And this is how my new method looks so far:

            [WebMethod]
            public documentCarrier readPublished(int id, string username, string password)
            {
                Authenticate(username, password);
    
                umbraco.cms.businesslogic.web.Document publishedDoc = null;
    
                try
                {
                    publishedDoc = this.GetPublishedDocument(id);
                }
                catch
                {}
    
                if (publishedDoc == null)
                    throw new Exception("Could not load published version of Document with ID: " + id);
    
                documentCarrier carrier = createCarrier(publishedDoc);
                return carrier;
            }
    
            private Document GetPublishedDocument(int id)
            {
                Document doc = new Document(true, id);
                if (!doc.HasPendingChanges())
                {
                    return doc;
                }
    
                Guid publishedVersion = doc.GetVersions()
                    .OrderByDescending(v => v.Date)
                    .Skip(1)
                    .Select(v => v.Version)
                    .FirstOrDefault();
                if (publishedVersion == default(Guid))
                {
                    return doc;
                }
    
                return new Document(id, publishedVersion);
            }
    

    While the code I wrote seems to be fine for now I'm not sure it is neither the recommended nor the most efficient way to accomplish what I need.

    I'll explore the Node object and get back to you.

    Thanks for your reply.

    Javier


Please Sign in or register to post replies

Write your reply to:

Draft