Copied to clipboard

Flag this post as spam?

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


  • tc 41 posts 62 karma points
    Apr 12, 2011 @ 17:54
    tc
    0

    Get Documents by Document type with API

    I'm currently doing some work with the umbraco API and i read in the API Reference that when working at the back end, it is recommended to use Documents, whereas Nodes are preferable at the front end. Given the points in the reference, this makes sense. 

    Now, when working at the back end, i need to get a set of Documents with a specific document type. All the examples i've found to do this work with Nodes and the nodeTypeAlias property. The C# Document type has no similar property. The only thing i found was "Template" but i need to search on Document Type.

    Whilst looping through a set of Documents, i thought i could get the Node representation and then i would have access to the nodeTypeAlias property. I assumed the Ids would be the same for the Document and the node so i did this:

    Node n = new Node(document.Id)

    However, this just returns an empty node.

    What i want to know is:

    a) Is there are nicer way to query documents by document type ?
    b) Am i right in assuming that the method above should give me the node representation of that document and if not, is there a way to do this?

     

    Thanks

  • Alex 78 posts 136 karma points
    Apr 12, 2011 @ 18:36
    Alex
    1

    Never used it myself but it appears (from the source) that there is a static method on the Document class called GetDocumentsOfDocumentType(int docTypeId)
    Should do the trick?

  • tc 41 posts 62 karma points
    Apr 13, 2011 @ 10:21
    tc
    0

    Ah i didnt notice that method there, that'll do the trick!

    The only thing is, i'd like to get all documents of a specific document type under a particular node, to save iterating through the documents that i know for sure arent going to be returned. But thanks for pointing that method out, that'll get me started!

  • tc 41 posts 62 karma points
    Apr 13, 2011 @ 10:42
    tc
    0

    After looking around, im unsure how i can get the id of the DocType, the umbraco back end only shows the alias of the doc type.

  • Alex 78 posts 136 karma points
    Apr 13, 2011 @ 10:50
    Alex
    0

    Well the ID is the node ID, so if you don't mind hardcoding it, you can get the ID by hovering over the DocumentTypes node in the UI and getting the ID from the link url. Otherwise you can get a DocumentType object like this (this is untested):

    string docTypeAlias = "yourDocTypeAlias";
    DocumentType dt = DocumentType.GetByAlias(docTypeAlias);

    and then use that in your call to GetDocumentsOfDocumentType, like this

    Document.GetDocumentsOfDocumentType(dt.Id);
  • tc 41 posts 62 karma points
    Apr 13, 2011 @ 11:35
    tc
    0

    Thanks Alex, thats done the trick!

Please Sign in or register to post replies

Write your reply to:

Draft