I'm wondering how to use the ContentService from a MacroPartial (MVC)?
I can't use Services.ContentService - or at least, that throws an error that 'Services' doesn't exist in the current context. How should I go about this?
I'm running 6.1.2, MVC version. Tried searching the documentation for this piece of information, but no luck.
You have to have using Umbraco.Core; and the you should be able to get the service from ApplicationContext.Current.Services.ContentService.
Alternatively you can get the ApplicationContext through the UmbracoContext.
PS: remember to use the ContentService with care when accessed from the frontend of a website, as it will usually mean database is hit (there is some caching depending which actions you use).
Its will be cached on first hit, but only stay cached for 5min as far as I remember.
If there is not a specific reason to get Content/IContent objects then we always recommend using IPublishedContent. Think its documented under Querying.
If you need to create or publish content or otherwise manipulate it, then the ContentService is the right thing to use. Almost everything else would use Published/Cached content (what we call IPublishedContent, which is retrieved from the XML cache).
How to use the ContentService from MacroPartial?
I'm wondering how to use the ContentService from a MacroPartial (MVC)?
I can't use Services.ContentService - or at least, that throws an error that 'Services' doesn't exist in the current context. How should I go about this?
I'm running 6.1.2, MVC version. Tried searching the documentation for this piece of information, but no luck.
Anyone can help me out?
You have to have using Umbraco.Core; and the you should be able to get the service from ApplicationContext.Current.Services.ContentService.
Alternatively you can get the ApplicationContext through the UmbracoContext.
PS: remember to use the ContentService with care when accessed from the frontend of a website, as it will usually mean database is hit (there is some caching depending which actions you use).
- Morten
Morten, thanks for the help.
If I just get a Node (with its children) through the Content Service, will that mean that the databse is hit, or that the cache is hit?
Its will be cached on first hit, but only stay cached for 5min as far as I remember.
If there is not a specific reason to get Content/IContent objects then we always recommend using IPublishedContent. Think its documented under Querying.
If you need to create or publish content or otherwise manipulate it, then the ContentService is the right thing to use. Almost everything else would use Published/Cached content (what we call IPublishedContent, which is retrieved from the XML cache).
- Morten
is working on a reply...