Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jason Espin 368 posts 1335 karma points
    May 01, 2014 @ 14:49
    Jason Espin
    0

    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.

  • Jason Espin 368 posts 1335 karma points
    May 01, 2014 @ 15:13
    Jason Espin
    100

    It seems that the best way to handle this is as follows. Correct me if I'm wrong:

    IContentService contentService = ApplicationContext.Current.Services.ContentService;
    
    var homePageId = contentService.GetRootContent().ElementAt(0).Id;
    var continentsPageId = contentService.GetChildren(homePageId).Where(x => x.ContentType.Alias == "continents").ElementAt(0).Id; 
    var continentPages = contentService.GetChildren(continentsPageId).Where(x => x.ContentType.Alias == "continent");
    
Please Sign in or register to post replies

Write your reply to:

Draft