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
    Jun 25, 2015 @ 14:18
    Jason Espin
    0

    Selecting UnPublished nodes in Umbraco 7

    Hi all,

    How do I go about selecting Unpublished nodes in Umbraco? I know that I can retreive published content using:

    Umbraco.Content(id)
    

    However, I need to be able to essentially get all nodes whether they are published or not for an automated update process I am writing.

    The process uses the Lucene index to build up a dictionary containing a unique id as the key and the related Umbraco page (unpublished or published) as the value so that I can then access this with the content service and update existing pages and create new ones if necessary.

    // Define shared search providers
            BaseSearchProvider AxumSearcher = Examine.ExamineManager.Instance.SearchProviderCollection["AxumSearcher"];
    
    ISearchCriteria tourContainerCriteria = AxumSearcher.CreateSearchCriteria(BooleanOperation.Or);
                            IBooleanOperation tourQuery = tourContainerCriteria.Field("nodeTypeAlias", "tour");
                            tourQuery = tourQuery.Or().Field("nodeTypeAlias", "tourRepository");
                            tourQuery = tourQuery.And().Field("siteNode", culture.Value.Id.ToString());
                            ISearchResults tours = AxumSearcher.Search(tourQuery.Compile());
    
    foreach (SearchResult tourRecord in tours.Where(x => x.Fields["nodeTypeAlias"] == "tour"))
                                {
                                    if (tourRecord.Fields["id"] == "1641")
                                    {
                                        var a = "test";
                                    }
    
                                    if (Umbraco.Content(tourRecord.Fields["id"]).GetType().ToString() != "Umbraco.Core.Dynamics.DynamicNull")
                                    {
                                        IPublishedContent tourNode = Umbraco.Content(tourRecord.Fields["id"]);
                                        if (!umbraco_tours.ContainsKey(tourNode.GetPropertyValue<int>("tripId")))
                                        {
                                            umbraco_tours.Add(tourNode.GetPropertyValue<int>("tripId"), tourNode.Id);
                                        }
                                    }
                                }
    

    I then do the following:

    // Loop through web service tours and create a page if it does not exist for the current culture
                            foreach (GroupTourMasterData web_service_item in web_service)
                            {
                                IContent umbraco_node = null;
                                List<JSONTourInstance> instances = new List<JSONTourInstance>();
    
                                if (umbraco_tours.ContainsKey(web_service_item.Id))
                                {
                                    // If the tour code already exists then get the Umbraco page
                                    int id = umbraco_tours[web_service_item.Id];
                                    umbraco_node = cs.GetById(id);
                                }
                                else
                                {
                                    // If not create a new tour page
                                    if (parentNode != null)
                                    {
                                        umbraco_node = cs.CreateContent(web_service_item.Title, parentNode.Id, "tour");
                                    }
                                }
     }
    

    Where possible I want to try and avoid the content service for querying existing items as it is incredibly slow.

    I have checked and my index is successfully supporting unpublished content but how do I then access this in code, edit it then publish it as at the moment Umbraco.Content is not working for me.

    Cheers,

    Jason

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Jun 25, 2015 @ 14:52
    Ismail Mayat
    0

    Jason,

    The line

    IPublishedContent tourNode = Umbraco.Content(tourRecord.Fields["id"]);
    

    after getting that you are just looking to get property tripId why not just do

    tourRecord.Fields["tripId"]
    

    Regards

    Ismail

  • Jason Espin 368 posts 1335 karma points
    Jun 25, 2015 @ 15:25
    Jason Espin
    0

    Hi Ismael,

    Because they are two different IDs. One is the Umbraco ID and one is the internal ID within our company software which is the process that is populating these pages automatically.

    I've now refined the first stage of my code to do the following which works a treat.

    if (tours.Where(x => x.Fields["nodeTypeAlias"] == "tour").Count() > 0)
                            {
                                foreach (SearchResult tourRecord in tours.Where(x => x.Fields["nodeTypeAlias"] == "tour"))
                                {
                                    int axumId = 0;
                                    int umbracoId = 0;
                                    Int32.TryParse(tourRecord.Fields["tripId"], out axumId);
                                    Int32.TryParse(tourRecord.Fields["id"], out umbracoId);
    
                                    if (axumId != 0 && umbracoId != 0 && !umbraco_tours.ContainsKey(axumId))
                                    {
                                        umbraco_tours.Add(axumId, umbracoId);
                                    }
                                }
                            }
    
  • Jason Espin 368 posts 1335 karma points
    Jun 25, 2015 @ 15:26
    Jason Espin
    100

    I then do this in the second stage:

    // Loop through web service tours and create a page if it does not exist for the current culture
                            foreach (GroupTourMasterData web_service_item in web_service)
                            {
                                IContent umbraco_node = null;
                                List<JSONTourInstance> instances = new List<JSONTourInstance>();
    
                                if (umbraco_tours.ContainsKey(web_service_item.Id))
                                {
                                    // If the tour code already exists then get the Umbraco page
                                    int id = umbraco_tours[web_service_item.Id];
                                    if (id == 1641)
                                    {
                                        var abreak = "break";
                                    }
                                    try
                                    {
                                        umbraco_node = cs.GetById(id);
                                    }
                                    catch (Exception ex)
                                    {
                                        Log.Error("Non Fatal : Tour master with id " + id + " not found. Skipping to next tour.", ex);
                                    }
                                }
                                else
                                {
                                    // If not create a new tour page
                                    if (parentNode != null)
                                    {
                                        umbraco_node = cs.CreateContent(web_service_item.Title, parentNode.Id, "tour");
                                    }
                                }
    
                               if (umbraco_node != null){
                                       // do stuff
                               }
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft