Copied to clipboard

Flag this post as spam?

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


  • Keith 11 posts 90 karma points
    18 days ago
    Keith
    0

    Hi

    Is there a way to retrieve the number of pages on an Umbraco 13+ site?

    I would like to know the number of pages that are published and unpublished.

    If there's a plugin that does this or if anyone can give a pointer to what type of SQL it SQL tables I need to look at that too would be great.

    Thank you

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    1 week ago
    Dennis Aaen
    0

    Hi Keith

    I am not aware of any plugins for this.

    I was thinking perhaps you could use the content service https://docs.umbraco.com/umbraco-cms/reference/management/services/contentservice

    As you can see on of the methods this service has is count

    https://apidocs.umbraco.com/v13/csharp/api/Umbraco.Cms.Core.Services.IContentService.html#UmbracoCmsCoreServicesIContentServiceCountSystemString

    All the best,

    /Dennis

  • UCP 9 posts 99 karma points
    19 hours ago
    UCP
    0

    Hi Keith,

    To find out the number of published and unpublished pages on your Umbraco site, you have a few options, ranging from directly querying the database to using the Umbraco services in your code. Let's go through both methods.

    Option 1: Using Umbraco Services in Code

    You can use Umbraco's ContentService in a custom controller or component to fetch counts of published and unpublished nodes. Here's a simple example of how you could implement this in a custom backoffice controller:

    ```csharp using Umbraco.Cms.Web.BackOffice.Controllers; using Microsoft.AspNetCore.Mvc; using Umbraco.Cms.Core.Services;

    namespace YourNamespace { [Route("backoffice/api/[controller]")] public class ContentStatsController : UmbracoAuthorizedApiController { private readonly IContentService _contentService;

        public ContentStatsController(IContentService contentService)
        {
            _contentService = contentService;
        }
    
        [HttpGet]
        public IActionResult GetContentCounts()
        {
            var allContent = _contentService.GetRootContent();
            int publishedCount = 0;
            int unpublishedCount = 0;
    
            foreach (var content in allContent)
            {
                if (content.Published)
                    publishedCount++;
                else
                    unpublishedCount++;
    
                var descendants = _contentService.GetPagedDescendants(content.Id, 0, int.MaxValue, out long totalRecords);
                foreach (var desc in descendants)
                {
                    if (desc.Published)
                        publishedCount++;
                    else
                        unpublishedCount++;
                }
            }
    
            return Ok(new { Published = publishedCount, Unpublished = unpublishedCount });
        }
    }
    

    } ```

    Note: This method loads all content into memory, which can be inefficient for large sites. You might want to implement more sophisticated querying or caching mechanisms depending on your needs.

    Option 2: Direct SQL Query

    If you prefer to query the database directly, you can execute SQL queries against the Umbraco database. Here’s how you might do it:

    ```sql

    -- Count of Published Nodes
    SELECT COUNT(*)
    FROM umbracoNode AS N
    INNER JOIN umbracoDocument AS D ON N.id = D.nodeId
    WHERE N.nodeObjectType = 'C66BA18E-EAF3-4CFF-8A22-41B16D66A972' -- Document type
    AND D.published = 1 AND N.trashed = 0;
    
    -- Count of Unpublished Nodes
    SELECT COUNT(*)
    FROM umbracoNode AS N
    INNER JOIN umbracoDocument AS D ON N.id = D.nodeId
    WHERE N.nodeObjectType = 'C66BA18E-EAF3-4CFF-8A22-41B16D66A972' -- Document type
    AND D.published = 0 AND N.trashed = 0;
    

    ```

    Note: These queries count nodes where published status is true or false. Ensure that your database user has the appropriate permissions to execute these queries. Always be cautious when accessing the database directly to avoid performance issues or security risks.

    Plugins or Packages

    As of now, I'm not aware of a specific plugin that provides just these statistics, but you can easily create a dashboard in Umbraco to show these stats using the custom controller method mentioned above.

    These methods should give you a good starting point to determine how many published and unpublished pages are on your Umbraco site. If you have any more questions or need further assistance, feel free to ask!

    Best of luck with your Umbraco project, UCP

Please Sign in or register to post replies

Write your reply to:

Draft