The Pax Bisonica
The Pax Bisonica

Reputation: 2164

Umbraco Creating Content Through Code Causes Cache Invalidation

We have created an area where the client can manage their content in the site instead of the admin and for some reason when we create new content the cache seems to get invalidated.

After creating the content if you go to the node in the admin you will see this error near the Link to Document property: Oops: this document is published but is not in the cache (internal error).

Any ideas why this might be happening? Is there a way to check if the item is in the cache after creating it?

This is the method that seems to be causing the issue:

public static int GetOrCreateContentFolder(IContentService contentService, int userId, int parentId, string folderName, string contentTypeAlias)
    {
        var targetContentFolder = 
            contentService.GetChildren(parentId)
            .Where(c => c.Name.ToLower() == folderName.ToLower())
            .ToList();

        if (targetContentFolder.Any())
        {
            return targetContentFolder[0].Id;
        }

        var contentFolder = contentService.CreateContent(folderName, parentId, contentTypeAlias, userId);
        return contentFolder.Id;
    }

Upvotes: 3

Views: 1604

Answers (1)

Morten OC
Morten OC

Reputation: 1784

It seems that you need to publish your new node called "contentFolder".

var contentFolder = contentService.CreateContent(folderName, parentId, contentTypeAlias, userId);
contentService.SaveAndPublish(contentFolder);

Use PublishWithChildren if you need to publish all children too.

See all methods in the ContentService here: https://our.umbraco.org/documentation/Reference/Management-v6/Services/ContentService

Upvotes: 2

Related Questions