How to get Unpublished Contents from umbraco

766 views Asked by At

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?

1

There are 1 answers

3
Nurhak Kaya On

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