0

I'm the new to Umbraco and I'm struggling to get the unpublished contents.

I have created a document type article and article items as mentioned in our Umbraco tutorial. In content section I have created a five number of article items as a blog and unpublished two of them. I'm getting three published article items in the site. Now I want to get the unpublished contents instead of published one. How I can do this?

James Z
  • 12,209
  • 10
  • 24
  • 44
  • Bear in mind that attempting to get any content items for the front end via the `ContentService` will have significant performance issues. Use with caution. – Robert Foster Feb 24 '22 at 06:18

1 Answers1

1

You can use the ContentService to get your unpublished content, but be very careful when using the ContentService as this service directly gets the data from the Umbraco database rather than the cache and this will cause a lot of problems on your site, like the website being very slow, especially if a lot of people are reaching to your website.

Here is an example to use the ContentService, You can take a look at this blog post for more details.

var contentService = new ContentService();
IContent content = contentService.GetById(123); // 123 is the nodeId

Here is another example;

var content = ApplicationContext.Current.Services.ContentService.GetById(123);

For the latest version of Umbraco (v9 as of today), you should use Dependency Injection in your constructor to get the ContentService:

public class MyClass
{
    private IContentService _contentService;
    
    public MyClass(IContentService contentService)
    {
        _contentService = contentService;
    }
}

Update: Today I have had a chance to add some screenshots for an Umbraco v7 project, please see below how you can get the unpublished content via the ContentService - things could be a little different for an Umbraco v8 project:

enter image description here

Nurhak Kaya
  • 1,470
  • 1
  • 18
  • 20
  • I will try this and get back to you. – Jothipriya Feb 24 '22 at 07:27
  • Please test it and remember to "upvote" the answers that help you and mark the "correct" answer as the correct answer. – Nurhak Kaya Feb 24 '22 at 13:28
  • @Jothipriya; don't know if you have had a chance to do your tests but I have done some tests today and added a screenshot to show how the ContentService returns the unpublished content data. Hope this helps. – Nurhak Kaya Mar 03 '22 at 17:37