Copied to clipboard

Flag this post as spam?

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


  • Tom 161 posts 322 karma points
    Jan 18, 2023 @ 19:16
    Tom
    0

    How To Get a list of UnPublished Pages in Umbraco 10

    Hello: I am using Umbraco 10, C#. I think I want to use the IContentService but don't know how to use this object to get a list of unpublished pages.

    Can anyone help?

    Thanks

    Tom

  • Garðar Þorsteinsson 113 posts 534 karma points
    Jan 18, 2023 @ 20:47
    Garðar Þorsteinsson
    1

    Hi Tom,

    Here are two ways of doing it with IContentService.

    I'm sure we could find a better optimized way of doing it but it does the job.

    Hopefully this helps you.

    using Umbraco.Cms.Core.Models;
    using Umbraco.Cms.Core.Services;
    using Umbraco.Cms.Infrastructure.Persistence.Querying;
    using Umbraco.Cms.Infrastructure.Scoping;
    
    namespace Services
    {
        public class ExampleService
        {
            private readonly IContentService _cs;
            private readonly IContentTypeService _cts;
            private readonly IScopeProvider _scope;
    
            public ExampleService(IContentService cs, IContentTypeService cts, IScopeProvider scope)
            {
                _cs = cs;
                _cts = cts;
                _scope = scope;
            }
    
            public IEnumerable<IContent> GetAllUnpublishedNodeByType(string alias)
            {
                var contentType = _cts.Get(alias);
    
                if (contentType == null)
                {
                    throw new Exception("Content Type " + alias + " not found.");
                }
    
                var query = new Query<IContent>(_scope.SqlContext).Where(x => !x.Published && !x.Trashed);
    
                return _cs.GetPagedOfType(contentType.Id, 0, int.MaxValue, out var total, query);
            }
    
            public IEnumerable<IContent> GetAllUnpublishedNodes()
            {
    
                var list = new List<IContent>();
                var rootNodes = _cs.GetRootContent();
    
                var query = new Query<IContent>(_scope.SqlContext).Where(x => !x.Published && !x.Trashed);
    
                foreach (var rootNode in rootNodes)
                {
                    if (!rootNode.Published)
                    {
                        list.Add(rootNode);
                    }
    
                    var descendants = _cs.GetPagedDescendants(rootNode.Id, 0, int.MaxValue, out long totalNodes, query);
    
                    list.AddRange(descendants);
                }
    
                return list;
            }
        }
    }
    
  • Tom 161 posts 322 karma points
    Jan 19, 2023 @ 12:16
    Tom
    0

    Thank you very much for your help.

  • Tom 161 posts 322 karma points
    Jan 19, 2023 @ 20:00
    Tom
    0

    I used your second approach and it did return me a list of unpublished items. However the descendants result set did return me a list of 2 items, each of which contained a unique Content.Name (which is the page name).

    But I also need the URL as I am building a report. Can you assist further in getting the page, url, and ID please?

    In advance,

    Thanks

    Tom

  • Garðar Þorsteinsson 113 posts 534 karma points
    Jan 20, 2023 @ 10:06
    Garðar Þorsteinsson
    1

    Hi Tom,

    I updated the code to return you an NodeItem object that has Name and ID.

    Also I added a new GetCachedUnpublishedNodes that is faster.

    But it's not possible to fetch an url of an unpublished node. Only published nodes have a url.

    Hopefully this helps you.

    using Umbraco.Cms.Core.Models;
    using Umbraco.Cms.Core.Services;
    using Umbraco.Cms.Core.Web;
    using Umbraco.Cms.Infrastructure.Persistence.Querying;
    using Umbraco.Cms.Infrastructure.Scoping;
    using Umbraco.Extensions;
    
    namespace Services
    {
        public class ExampleService
        {
            private readonly IContentService _cs;
            private readonly IContentTypeService _cts;
            private readonly IScopeProvider _scope;
            private readonly IUmbracoContextFactory _umbracoContextFactory;
    
            public ExampleService(IContentService cs, IContentTypeService cts, IScopeProvider scope, IUmbracoContextFactory umbracoContextFactory)
            {
                _cs = cs;
                _cts = cts;
                _scope = scope;
                _umbracoContextFactory = umbracoContextFactory;
            }
    
            public class NodeItem
            {
                public string Name { get; set; }
                public int Id { get; set; }
    
            }
    
            public IEnumerable<NodeItem> GetCachedUnpublishedNodes()
            {
                using (var cref = _umbracoContextFactory.EnsureUmbracoContext())
                {
                    var cache = cref.UmbracoContext.Content;
    
                    var nodes = cache.GetByXPath(true, "//*").DistinctBy(x => x.Id);
    
                    return nodes.Where(x => !x.IsPublished()).Select(x => new NodeItem()
                    {
                        Id = x.Id,
                        Name = x.Name
                    });
                }
            }
    
            public IEnumerable<NodeItem> GetAllUnpublishedNodeByType(string alias)
            {
                var contentType = _cts.Get(alias);
    
                if (contentType == null)
                {
                    throw new Exception("Content Type " + alias + " not found.");
                }
    
                var query = new Query<IContent>(_scope.SqlContext).Where(x => !x.Published && !x.Trashed);
    
                return _cs.GetPagedOfType(contentType.Id, 0, int.MaxValue, out var total, query).Select(x => new NodeItem() { 
                     Id = x.Id,
                     Name = x.Name
                });
            }
    
            public IEnumerable<NodeItem> GetAllUnpublishedNodes()
            {
    
                var list = new List<IContent>();
                var rootNodes = _cs.GetRootContent();
    
                var query = new Query<IContent>(_scope.SqlContext).Where(x => !x.Published && !x.Trashed);
    
                foreach (var rootNode in rootNodes)
                {
                    if (!rootNode.Published)
                    {
                        list.Add(rootNode);
                    }
    
                    var descendants = _cs.GetPagedDescendants(rootNode.Id, 0, int.MaxValue, out long totalNodes, query);
    
                    list.AddRange(descendants);
                }
    
                return list.Select(x => new NodeItem()
                {
                    Id = x.Id,
                    Name = x.Name
                }); ;
            }
        }
    }
    
  • Tom 161 posts 322 karma points
    Jan 20, 2023 @ 13:38
    Tom
    1

    Thank you very much. I like the GetCachedUnpublishedNodes(), which is faster! You Rock! Have a great upcoming weekend.

    Tom

  • Tom 161 posts 322 karma points
    Jan 23, 2023 @ 12:36
    Tom
    0

    Got another issue. I have a page that has been published but now I made a change to it and just saved my changes in the backoffice. My thinking is it would also be in an unpublished state, but its not! When I ran the code you provided, it did not show up in the results.

    Question: What do I have to do to get the previously published page, which is now in a saved state to appear in my result set? As I thought it would be in an unpublished state but it is not.

    Thanks

    Tom

  • Garðar Þorsteinsson 113 posts 534 karma points
    Jan 23, 2023 @ 13:18
    Garðar Þorsteinsson
    1

    Hi Tom,

    Every node is either Published or Unpublished.

    You want to fetch an older version of a current published node.

    The code now only fetches nodes that the current state is unpublished, the node you are referring to is published.

    But if you want to get all unpublished nodes and also the version behind the published one its going to get way more complex.

    You would have to fetch all nodes unpublished and published. For the published nodes you will need to fetch all versions of those nodes and check if they have an older version to it and fetch new newest one.

    I cant seem to find a reason why you would need this, most likely there is some better solution to it than this.

    Could you explain the reason behind this functionality ?

  • Tom 161 posts 322 karma points
    Jan 23, 2023 @ 13:55
    Tom
    0

    OK If you're saying a page is either published or not, I'll have to do some more digging.

    Thanks

  • Tom 161 posts 322 karma points
    Jan 24, 2023 @ 13:48
    Tom
    0

    Question: In Umbraco 10, if I have a page that's been published (i.e. last week) and then go back and make a change and just save it, why does Umbraco consider this page as published?

    Note: If I create a brand new page and just save it, it's status is unPublished.

    Tom

Please Sign in or register to post replies

Write your reply to:

Draft