Josef Henryson
03/22/2024, 9:50 AMLewis Heaton
03/22/2024, 9:52 AMSebastiaan
03/22/2024, 9:52 AMContentService
to get the content and edit it, it should have a Save & Publish methos.
Fun fact, it will hit your notification handler again, so make sure to cancel further notifications when you do that.Josef Henryson
03/22/2024, 9:54 AMvar page = umbracoContext.Content?.GetById(node.Id) as SourcePage;
if (page?.Tags?.Any() == true)
{
foreach (var themePage in themePages)
{
if (page.Tags.Any(t => t == themePage.Tag))
{
if (themePage.RelatedPages == null) themePage.RelatedPages = new List() { page };
_contentService.SaveAndPublish((Umbraco.Cms.Core.Models.IContent)themePage);
}
}
}
Josef Henryson
03/22/2024, 9:55 AMSebastiaan
03/22/2024, 9:58 AMvar contentToUpdate = _contentService.GetById(node.Key);
and take it from there.Sebastiaan
03/22/2024, 9:58 AMLewis Heaton
03/22/2024, 9:59 AMSebastiaan
03/22/2024, 10:00 AMnode
is IPublishedContent
and not updatable, you will have to go through the ContentService
.SiempreSteve
03/22/2024, 10:00 AMSebastiaan
03/22/2024, 10:02 AMSaveAndPublish
raises another notification!), looks like this is the way to prevent that:
https://our.umbraco.com/forum/using-umbraco-and-getting-started/108023-save-and-publish-without-triggering-new-notification#comment-345986SiempreSteve
03/22/2024, 10:03 AMvar page = umbracoContext.Content?.GetById(node.Id) as SourcePage;
if (page?.Tags?.Any() == true)
{
foreach (var themePage in themePages)
{
if (page.Tags.Any(t => t == themePage.Tag))
{
var themePageEditable = _contentService.GetById(themePage.Id);
var newTagValue = ""; // here's the trick you need to probably build up the json to update this.
themePageEditable.SetValue("tags", newTagValue);
_contentService.SaveAndPublish(themePageEditable);
}
}
}
Sebastiaan
03/22/2024, 10:04 AM.Key
instead of .Id
πSiempreSteve
03/22/2024, 10:05 AMJosef Henryson
03/22/2024, 10:05 AMSiempreSteve
03/22/2024, 10:06 AMSiempreSteve
03/22/2024, 10:06 AMSiempreSteve
03/22/2024, 10:07 AMvar page = umbracoContext.Content?.GetById(node.Id) as SourcePage;
if (page?.Tags?.Any() == true)
{
foreach (var themePage in themePages)
{
if (page.Tags.Any(t => t == themePage.Tag))
{
var themePageEditable = _contentService.GetByKey(themePage.Key);
var newTagValue = ""; // here's the trick you need to probably build up the json to update this.
themePageEditable.SetValue("tags", newTagValue);
_contentService.SaveAndPublish(themePageEditable);
}
}
}
Josef Henryson
03/22/2024, 10:08 AMSebastiaan
03/22/2024, 10:09 AMSaveAndPublish
line, I believe this should work:
csharp
using var scope = ScopeProvider.CreateScope(autoComplete: true);
using var _ = scope.Notifications.Suppress();
_contentService.SaveAndPublish(themePageEditable);
SiempreSteve
03/22/2024, 10:11 AMJosef Henryson
03/22/2024, 10:26 AMif (themePage.RelatedPages == null) themePage.RelatedPages = new List() { page };
But I have to find the JSON representation that is saved to DB? Any idea on how to find this out?Jemayn
03/22/2024, 10:57 AMSiempreSteve
03/22/2024, 11:31 AMAnders Bjerner
03/22/2024, 11:50 AMpublishedContent.GetProperty("myProperty")?.GetSourceValue()
also gives you the database valueJosef Henryson
03/22/2024, 1:08 PMSiempreSteve
03/22/2024, 2:11 PM