Saving and Publishing a Node with the ContentService moves it 1 level up the tree
I'm using the ContentService and MediaService to create a new IMedia, and then update a property on an existing IContent with the new IMedia's ID and publish the changes.
Here's my code (more or less):
var existingContentNode = _contentService.GetById(contentNodeId);
var newImage = _mediaService.CreateMedia(newImageFileName, newMediaParentNodeId, "image");
_mediaService.Save(newImage);
existingContentNode.SetValue(imagePropertyAlias, newImage.Id);
_contentService.SaveAndPublishWithStatus(existingContentNode);
The image is always created and set, which is the desired behaviour. However, roughly half the time this code has an undesired side-affect and moves the existing content node one level up the tree, becoming a sibling of (what was) it's parent.
I am unable to reproduce this on my local machine; this issue is only happening on the server.
Further Information
Umbraco version 7.4.2 assembly: 1.0.5921.28477
I also have Ditto v0.8.4, uSync v3.2.0.740, and Chauffeur v0.6.4 installed (via NuGet).
The dependencies on IMediaService and IContentService are being resolved by a DI container, Autofac v3.5.2 with the following code:
var builder = new ContainerBuilder();
// ...
builder.RegisterInstance(applicationContext.Services.ContentService).As<IContentService>();
builder.RegisterInstance(applicationContext.Services.MediaService).As<IMediaService>();
// ...
Yep, I've confirmed with the Visual Studio debugger that I'm getting the right node there.
However, perhaps my way of naming the ID variable "parentNodeId" has caused some confusion. I was referring to the ID of the existing content node which I'm adding media to. I've edited my original post to make that a little clearer.
I've changed the code which saves and publishes the node to:
// Before any work is done, create a record of the node's parentId
var existingContentNode = _contentService.GetById(contentNodeId);
int parentIdBefore = parentNode.ParentId;
// ....
// After work is done, set the parentId to make sure it's not changed
existingContentNode.ParentId = parentIdBefore;
_contentService.SaveAndPublishWithStatus(existingContentNode);
And it's still not working on the server. Additionally, some other weirdness is now going on, such as:
Changes to the node not actually being published, but the backoffice saying they have been.
Clicking "Save and Publish" in the backoffice publishes the changes but moves the node 1 level up the tree.
I now think that the database and/or cache on the server's environment is corrupted or otherwise incongruous, so I'm starting the process of restoring it. I will report back when I'm done.
Hey Gary, any news on this issue cause I'm having the same problem myself. Using SaveAndPublishWithStatus() wont actually publish my content.
In the Umbraco backoffice it looks like its published but if I try to fetch children from my parentnode i get an empty list until I actually manually publish the page created.
Anything we're missing?
EDIT: I have tried Save() and Publish() with the same results. Even tried the deprecated method umbraco.library.UpdateDocumentCache(), someone else had a smiliar problem and solved it using that. (was a long time ago tho).
Tried setting the PublishState.Published on the created IContent but nothing changed.
For some reason I cleared my cookies and all of a sudden my pages showed up. I have no idea what specific cookie was causing the issue but it seems that was the problem.
Right now I get the "I have no idea what im doing" feeling.
Saving and Publishing a Node with the ContentService moves it 1 level up the tree
I'm using the ContentService and MediaService to create a new
IMedia
, and then update a property on an existingIContent
with the newIMedia
's ID and publish the changes.Here's my code (more or less):
The image is always created and set, which is the desired behaviour. However, roughly half the time this code has an undesired side-affect and moves the existing content node one level up the tree, becoming a sibling of (what was) it's parent.
I am unable to reproduce this on my local machine; this issue is only happening on the server.
Further Information
Umbraco version 7.4.2 assembly: 1.0.5921.28477
I also have Ditto v0.8.4, uSync v3.2.0.740, and Chauffeur v0.6.4 installed (via NuGet).
The dependencies on
IMediaService
andIContentService
are being resolved by a DI container, Autofac v3.5.2 with the following code:Thanks in advance,
Gary
UPDATE:
I changed
_contentService.SaveAndPublishedWithStatus(contentNode);
to:And the problem persists. This is leading me to believe that the issue happens during publish regardless of which method call I use.
Hi Gary,
Is it right logic that you get parent node?
From your code:
parentNodeId? is it parent node ?
Thanks,
Alex
Yep, I've confirmed with the Visual Studio debugger that I'm getting the right node there.
However, perhaps my way of naming the ID variable "parentNodeId" has caused some confusion. I was referring to the ID of the existing content node which I'm adding media to. I've edited my original post to make that a little clearer.
Cheers, Gary
Update on Progress:
I've changed the code which saves and publishes the node to:
And it's still not working on the server. Additionally, some other weirdness is now going on, such as:
I now think that the database and/or cache on the server's environment is corrupted or otherwise incongruous, so I'm starting the process of restoring it. I will report back when I'm done.
Hey Gary, any news on this issue cause I'm having the same problem myself. Using SaveAndPublishWithStatus() wont actually publish my content.
In the Umbraco backoffice it looks like its published but if I try to fetch children from my parentnode i get an empty list until I actually manually publish the page created.
Anything we're missing?
EDIT: I have tried Save() and Publish() with the same results. Even tried the deprecated method umbraco.library.UpdateDocumentCache(), someone else had a smiliar problem and solved it using that. (was a long time ago tho).
Tried setting the PublishState.Published on the created IContent but nothing changed.
For some reason I cleared my cookies and all of a sudden my pages showed up. I have no idea what specific cookie was causing the issue but it seems that was the problem.
Right now I get the "I have no idea what im doing" feeling.
is working on a reply...