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 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.
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)
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!
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? :)
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
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)
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!
@slace,
Thanks learned something new :) This isn't documented is it?
Documentation.... :P
is working on a reply...