diff --git a/15/umbraco-cms/reference/management/using-services/contentservice.md b/15/umbraco-cms/reference/management/using-services/contentservice.md index 56b2a20d757..0a3cb387aec 100644 --- a/15/umbraco-cms/reference/management/using-services/contentservice.md +++ b/15/umbraco-cms/reference/management/using-services/contentservice.md @@ -1,5 +1,5 @@ --- -description: Example on how to create content programmatically using the ContentService. +description: Example on how to create and publish content programmatically using the ContentService. --- # Content Service @@ -11,35 +11,77 @@ Learn how to use the Content Service. In the example below, a new page is programmatically created using the content service. It is assumed that there are two document types, namely Catalogue and Product. In this case, a new Product is added underneath the Catalogue page. Add the below code in the Catalogue template. ```csharp -// Get access to ContentService - Add this at the top of your razor view -@inject IContentService ContentService -@using Umbraco.Cms.Core.Services +using Umbraco.Cms.Core.Models; +using Umbraco.Cms.Core.Services; -// Add this anywhere in your Catalogue template -@{ - // Create a variable for the GUID of the parent page - Catalogue, where you want to add a child item. - var parentId = Guid.Parse("b6fbbb31-a77f-4f9c-85f7-2dc4835c7f31"); +namespace Umbraco.Cms.Web.UI.Custom; - // Create a new child item of type 'Product' - var demoproduct = ContentService.Create("Microphone", parentId, "product"); +public class PublishContentDemo +{ + private readonly IContentService _contentService; - // Set the value of the property with alias 'category' - demoproduct.SetValue("category" , "audio"); + public PublishContentDemo(IContentService contentService) => _contentService = contentService; - // Set the value of the property with alias 'price' - demoproduct.SetValue("price", "1500"); + public void Create() + { + // Create a variable for the GUID of the parent page - Catalogue, where you want to add a child item. + var parentId = Guid.Parse("b6fbbb31-a77f-4f9c-85f7-2dc4835c7f31"); - // Save and publish the child item - ContentService.SaveAndPublish(demoproduct); - + // Create a new child item of type 'Product' + var demoProduct = ContentService.Create("Microphone", parentId, "product"); + + // Set the value of the property with alias 'category' + demoProduct.SetValue("category" , "audio"); + + // Set the value of the property with alias 'price' + demoProduct.SetValue("price", "1500"); + + // Save and publish the child item + _contentService.SaveAndPublish(demoProduct); + } } ``` -In a multilanguage setup, it is necessary to set the name of the content item for a specified culture: +In a multi-language setup, it is necessary to set the name of the content item for a specified culture: ```csharp -demoproduct.SetCultureName("Microphone", "en-us"); // this will set the english name -demoproduct.SetCultureName("Mikrofon", "da"); // this will set the danish name +demoProduct.SetCultureName("Microphone", "en-us"); // this will set the english name +demoProduct.SetCultureName("Mikrofon", "da"); // this will set the danish name ``` For information on how to retrieve multilingual languages, see the [Retrieving languages](./retrieving-languages.md) article. + +## Publishing content programmatically + +The ContentService is also used for publishing operations. + +The following example shows a page being published with all descendants. + +```csharp +using Umbraco.Cms.Core.Models; +using Umbraco.Cms.Core.Services; + +namespace Umbraco.Cms.Web.UI.Custom; + +public class PublishContentDemo +{ + private readonly IContentService _contentService; + + public PublishContentDemo(IContentService contentService) => _contentService = contentService; + + public void Publish(Guid key) + { + IContent? content = _contentService.GetById(key) + ?? throw new InvalidOperationException($"Could not find content with key: {key}."); + + _contentService.SaveAndPublishBranch(content, PublishBranchFilter.Default); + } +} +``` + +The `PublishBranchFilter` option can include one or more of the following flags: + +- `Default` - publishes existing published content with pending changes. +- `IncludeUnpublished` - publishes unpublished content and existing published with pending changes. +- `ForceRepublish` - publishes existing published content with or without pending changes. +- `All` - combines `IncludeUnpublished` and `ForceRepublish`. diff --git a/15/umbraco-cms/tutorials/editors-manual/getting-started-with-umbraco/creating-saving-and-publishing-content.md b/15/umbraco-cms/tutorials/editors-manual/getting-started-with-umbraco/creating-saving-and-publishing-content.md index 671e111663d..3620c3d760e 100644 --- a/15/umbraco-cms/tutorials/editors-manual/getting-started-with-umbraco/creating-saving-and-publishing-content.md +++ b/15/umbraco-cms/tutorials/editors-manual/getting-started-with-umbraco/creating-saving-and-publishing-content.md @@ -60,9 +60,8 @@ To publish the node with descendants, follow these steps: 2. Select the arrow next to the **Save and Publish** button. 3. Select **Publish with descendants**. - ![Publish with descendants](images/Publish-with-descendants-v15.png) -4. Toggle the option to **Include unpublished content items** if you wish to. This option includes all unpublished content items for the selected page and the available linked pages. -5. Toggle the option to **Publish unchanged items** if you wish to. This option will trigger a re-publish of all the selected page and all descendant pages even if no changes are pending. + ![Publish with descendants](images/Publish-with-descendants-v14.png) +4. Toggle the option to **Include unpublished content items** if you wish to. This option includes all unpublished content items for the selected page and the descendant pages. #### 3: Unpublish diff --git a/15/umbraco-cms/tutorials/editors-manual/getting-started-with-umbraco/images/Publish-with-descendants-v15.png b/15/umbraco-cms/tutorials/editors-manual/getting-started-with-umbraco/images/Publish-with-descendants-v15.png deleted file mode 100644 index c920e24b656..00000000000 Binary files a/15/umbraco-cms/tutorials/editors-manual/getting-started-with-umbraco/images/Publish-with-descendants-v15.png and /dev/null differ