Selecting the ID of a parentNode of a specific ContentType.Alias using the Content Service
Hi all,
I'm trying to find a more dynamic way of selecting what should be the parentNode of my new content when creating pages using the Content Service. Currently I am doing the following:
//Umbraco Content Services
int rootID = 1117;
var continentPages = contentService.GetChildren(rootID).Where(x => x.ContentType.Alias == "continent");
//Loop through each continent in the response
foreach (var continent in response)
{
bool update = false;
//Loop through each existing page with a document type alias of "destination"
foreach (var existingContinentPage in continentPages)
{
var existingContinentPageId = existingContinentPage.GetValue<int>("continentId");
// If an existing page matches the new continent ID, update it
if (existingContinentPageId == continent.Id)
{
existingContinentPage.SetValue("continentName", continent.ContinentName + "updated");
existingContinentPage.SetValue("continentNotes", continent.Notes + "updated");
contentService.SaveAndPublishWithStatus(existingContinentPage);
update = true;
break;
}
}
// If no page update has taken place, add a new page
if (update == false)
{
var destination = contentService.CreateContent(continent.ContinentName, rootID, "continent");
destination.SetValue("continentId", continent.Id);
destination.SetValue("continentName", continent.ContinentName);
destination.SetValue("continentNotes", continent.Notes);
contentService.SaveAndPublishWithStatus(destination);
}
}
However, what I really want to do is get rid of the hard-coded rootID and acquire this dynamically. In this case, there will only ever be one page of contentType.Alias == "continents" that will be the parent page of the continent pages in the code above.
How do I go about acquiring this ID for use in my code?
I thought of doing the following:
var homepage = contentService.GetRootContent();
But this would only retreive the homepage of which the "continents" page I require is a child.
Any help with this would be greatly appreciated. Also, if anyone knows how I can force there to only be one content page of type "continents" that would also be greatly appreciated.
Selecting the ID of a parentNode of a specific ContentType.Alias using the Content Service
Hi all,
I'm trying to find a more dynamic way of selecting what should be the parentNode of my new content when creating pages using the Content Service. Currently I am doing the following:
However, what I really want to do is get rid of the hard-coded rootID and acquire this dynamically. In this case, there will only ever be one page of contentType.Alias == "continents" that will be the parent page of the continent pages in the code above.
How do I go about acquiring this ID for use in my code?
I thought of doing the following:
But this would only retreive the homepage of which the "continents" page I require is a child.
Any help with this would be greatly appreciated. Also, if anyone knows how I can force there to only be one content page of type "continents" that would also be greatly appreciated.
It seems that the best way to handle this is as follows. Correct me if I'm wrong:
is working on a reply...