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.
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.
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! :)
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.
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:
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? :)
Hmm, somehow this was a double post :( I'm sorry about that - tried to deleted the duplicate, but with no luck..
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
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
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!
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:
HTH,
Hendy
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:
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! :)
Excellent, glad you got it working :)
is working on a reply...