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? :)

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

    Hmm, somehow this was a double post :( I'm sorry about that - tried to deleted the duplicate, but with no luck..

  • Rich Green 2246 posts 4008 karma points
    Nov 27, 2010 @ 16:21
    Rich Green
    1

    Hey Bo,

    Not 100% but pretty sure you can achieve this by using Hendy's helper class using GetNodesFromXpath

    Have a look http://blog.hendyracher.co.uk/umbraco-helper-class/

    Cheers

    Rich

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Nov 27, 2010 @ 16:38
    Hendy Racher
    0

    Hi Bo,

    I was just replying to the other post, and was going to suggest that if you're associating a document with a member how about doing so using a relation type instead ? (the relations api enables you to return all associated nodes with a single DB hit), but as Rich suggests an XPath expression would also be an efficient way to return the nodes you're after.

    Cheers,

    Hendy

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

    Hi Rich and Hendy,

    It feels like I am better off using documents instead of nodes when I'm i.e. editing content. The member and the documents are in fact related to each other using the relationship API, I just don't know how to get all documents that's related to the current member to be honest :(

    If any of you should know of any hints on how to accomplish this, I would be very happy! :)

    Thanks again!

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Nov 27, 2010 @ 17:04
    Hendy Racher
    0

    Hi Bo,

    If they are already linked with the relationship API, then that sounds like the way to go.

    Do you know which way the relations are defined ? 

    Here's some example code but this assumes that the parent in the relation type is the member:

    // Get the relation type from it's alias
    RelationType relationType = RelationType.GetByAlias("relateMemberToDocument");

    // Get all relations for this relation type that are associated with a member
    Relation[] relations = Relation.GetRelations(memberId, relationType);

    Node document;
    foreach (Relation relation in relations)
    {
      document = new Node(relation.Child.Id);
    }

    HTH,

    Hendy

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

    Hendy, that's just perfect!! Thanks a lot :)

    Yes the member is the parent in my case and it's a two-way relation, so that works just great.

    I did change it around a little so it's like this now:

     

    Member m = Member.GetCurrentMember();
    RelationType relationType = RelationType.GetByAlias("member2spot");
    
    // Get all relations for this relation type that are associated with a member
    Relation[] relations = Relation.GetRelations(m.Id, relationType);
    
    List<Document> docs = new List<Document>();
    foreach (Relation relation in relations)
    {
        docs.Add(new Document(relation.Child.Id));
    }
    
    foreach(Document d in docs)
    {
      // do work...
    }
    

    This seems like a much better solution as it doesn't have to loop through all nodes to get the related ones.

    Thanks a lot for your help! :)

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Nov 27, 2010 @ 17:31
    Hendy Racher
    0

    Excellent, glad you got it working :)

Please Sign in or register to post replies

Write your reply to:

Draft