Oluwafemi
Oluwafemi

Reputation: 14899

How to unpublish content programmatically in Umbraco 7

I have been trying to unpublish a content programmatically in Umbraco 7 but it seems not to work as expected. Although the content was removed from the cache but the database record was never updated:

node.UnPublish();
umbraco.library.UnPublishSingleNode(node.Id);

After further investigation, I discovered that the method UnPublishSingleNode is obsolete:

[Obsolete("This method is no longer used, 
a document's cache will be removed automatically 
when the document is deleted or unpublished")]
public static void UnPublishSingleNode(int DocumentId);

The message did not suggest a new method :(

I need directions on how to unpublish a content programmatically.

Umbraco version: 7.3.3

Upvotes: 2

Views: 1581

Answers (1)

Oluwafemi
Oluwafemi

Reputation: 14899

I finally got it to work through Umbraco.Core.Services.IContentService

Usage:

var contentServices = ApplicationContext.Current.Services.ContentService;
var content = contentServices.GetById(node.Id);
contentServices.UnPublish(content, 0);

I hope this helps others.

Upvotes: 7

Related Questions