I use the new ContentService API for a while now. But I was wondering how I could load the current page ID.
I use this 'var childerencontent = contentService.GetDescendants(100);' to get all the Descendants below a node, but the problem is I want to make this page very flexibel, so the usercontrol can be used in several nodes and I don't want to create a usercontrol for every node.
First of all if you are using the ContentService API for displaying content you are doing it wrong. This API hits the database directly for querying content and can become a performance bottleneck if your site has a lot of traffic. Have a look at the IPublishedContent object for querying content for displaying. See the documentation here : https://our.umbraco.org/documentation/Reference/Querying/UmbracoHelper/
Get current node api umbraco 7
Hi,
I use the new ContentService API for a while now. But I was wondering how I could load the current page ID.
I use this 'var childerencontent = contentService.GetDescendants(100);' to get all the Descendants below a node, but the problem is I want to make this page very flexibel, so the usercontrol can be used in several nodes and I don't want to create a usercontrol for every node.
Any ideas?
Tnx in advance.
Rik
Hi Rik,
with this you can get the current nodeId:
Then use
to get your current node.
Documentation of ContentService: https://our.umbraco.org/documentation/Reference/Management-v6/Services/ContentService
Some times is better to use IPublishedContent, because contentservice hit the database and IPublishedContent hit the cached umbraco.config file (see here: https://our.umbraco.org/documentation/Reference/Querying/UmbracoHelper/).
Hope this helps,
Best, Sören
Hi Rik,
First of all if you are using the ContentService API for displaying content you are doing it wrong. This API hits the database directly for querying content and can become a performance bottleneck if your site has a lot of traffic. Have a look at the IPublishedContent object for querying content for displaying. See the documentation here : https://our.umbraco.org/documentation/Reference/Querying/UmbracoHelper/
If you want to manipulate data (edit the content) than you should use the contentservice api. The documentation for that can be found here : https://our.umbraco.org/documentation/Reference/Management-v6/Services/ContentService
Dave
Hi Sören and Dave,
Tnx for your quick response!
@Sören, sorry but the Request.QueryString isn't working.
I would like to use the Umbracohelper but in the examples I also see a prefilled ID. Do you have an example of the UmbracoHelper getting an node ID?
Thanks in advance.
Rik
With the umbraco helper you can do this.
@Umbraco.AssignedContentItem : this will return the current item.
Dave
Hi Dave,
Is this also working in an usercontrol?
I referenced the umbraco.dll in my VS project.
I use the following code:
int nodeID = Umbraco.Web.UmbracoHelper.AssignedContentItem;
and I got this error:
Error 14 An object reference is required for the non-static field, method, or property 'Umbraco.Web.UmbracoHelper.AssignedContentItem.get'
Thank you for pointing me in the right direction.
Rik
I think you need this code :
Dave
Hi Dave,
I got an error on that code. VS didn't recognize the UmbracoContext.Current.
But I used this:
int nodeID = Convert.ToInt32(Umbraco.Web.UmbracoContext.Current.PageId);
And it works :)
Thanks for your help!
Rik
is working on a reply...