Copied to clipboard

Flag this post as spam?

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


  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Nov 27, 2010 @ 16:12
    Bo Damgaard Mortensen
    0

    Get document by id from C#

    Hi Umbraco's,

    Currently I have been using the following method to fetch a specific document by it's id:

     

        Member m = Member.GetCurrentMember();
        int docId = Convert.ToInt32(HttpContext.Current.Request.QueryString["nodeId"]);
    
        DocumentType docType = DocumentType.GetByAlias("docTypeAlias");
        IEnumerable allDocFromGivenDocType = Document.GetDocumentsOfDocumentType(docType.Id);
        List docs = new List();
        foreach (Document d in allDocFromGivenDocType )
        {
            if (d.getProperty("property").Value.Equals(m.LoginName))
                 docs.Add(d);
        }
    
        foreach (Document doc in docs)
        {
           // do work
        }
    

    While the above sample works, I think it may be quite consuming when you have to loop through all existing nodes of a given document type alias.

    What are your experiences with this? :)

  • Richard Soeteman 4046 posts 12899 karma points MVP 2x
    Nov 28, 2010 @ 11:11
    Richard Soeteman
    0

    What is it that you want to achieve, this method does three things. But as your title says you want to ghet the document by Id.

    If you want to use it on your website (published content) Use

    Node

     

     

    .GetCurrent().Id

    This method is using the NodeFactory which is very fast.

    If you want to use it in your datatype or some other BackOffice Extension use Document doc = new Document(docId). This is only meant for use in the BackOffice since it's using database calls which is much slower.

    If you want to achieve something else please let me know.

    Cheers,

    Richard

     

     

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Nov 28, 2010 @ 12:35
    Aaron Powell
    0

    Actually Richard using UmbracoContext.Current.PageId is an easier way to access the ID for the page in the current request, saves creating the Node object if you don't need it (like you would with Node.GetCurrent)

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Nov 28, 2010 @ 19:59
    Bo Damgaard Mortensen
    0

    Richard (and slace), that's just what I wanted to know :) thanks! I've already written some code to use nodeFactory instead of document and now my site is running way more smooth! 

    Thanks for clarifying!

  • Richard Soeteman 4046 posts 12899 karma points MVP 2x
    Nov 28, 2010 @ 21:34
    Richard Soeteman
    0

    @slace,

    Thanks learned something new :) This isn't documented is it?

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Nov 28, 2010 @ 21:42
    Aaron Powell
    0

    Documentation.... :P

Please Sign in or register to post replies

Write your reply to:

Draft