Skip to content

Commit a8027da

Browse files
authored
Merge pull request #6932 from umbraco/cms/release-15.3-edit
Additional documentation for Umbraco 15.3
2 parents 45dab71 + c36ee13 commit a8027da

File tree

3 files changed

+64
-23
lines changed

3 files changed

+64
-23
lines changed
Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
description: Example on how to create content programmatically using the ContentService.
2+
description: Example on how to create and publish content programmatically using the ContentService.
33
---
44

55
# Content Service
@@ -11,35 +11,77 @@ Learn how to use the Content Service.
1111
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.
1212

1313
```csharp
14-
// Get access to ContentService - Add this at the top of your razor view
15-
@inject IContentService ContentService
16-
@using Umbraco.Cms.Core.Services
14+
using Umbraco.Cms.Core.Models;
15+
using Umbraco.Cms.Core.Services;
1716

18-
// Add this anywhere in your Catalogue template
19-
@{
20-
// Create a variable for the GUID of the parent page - Catalogue, where you want to add a child item.
21-
var parentId = Guid.Parse("b6fbbb31-a77f-4f9c-85f7-2dc4835c7f31");
17+
namespace Umbraco.Cms.Web.UI.Custom;
2218

23-
// Create a new child item of type 'Product'
24-
var demoproduct = ContentService.Create("Microphone", parentId, "product");
19+
public class PublishContentDemo
20+
{
21+
private readonly IContentService _contentService;
2522

26-
// Set the value of the property with alias 'category'
27-
demoproduct.SetValue("category" , "audio");
23+
public PublishContentDemo(IContentService contentService) => _contentService = contentService;
2824

29-
// Set the value of the property with alias 'price'
30-
demoproduct.SetValue("price", "1500");
25+
public void Create()
26+
{
27+
// Create a variable for the GUID of the parent page - Catalogue, where you want to add a child item.
28+
var parentId = Guid.Parse("b6fbbb31-a77f-4f9c-85f7-2dc4835c7f31");
3129

32-
// Save and publish the child item
33-
ContentService.SaveAndPublish(demoproduct);
34-
30+
// Create a new child item of type 'Product'
31+
var demoProduct = ContentService.Create("Microphone", parentId, "product");
32+
33+
// Set the value of the property with alias 'category'
34+
demoProduct.SetValue("category" , "audio");
35+
36+
// Set the value of the property with alias 'price'
37+
demoProduct.SetValue("price", "1500");
38+
39+
// Save and publish the child item
40+
_contentService.SaveAndPublish(demoProduct);
41+
}
3542
}
3643
```
3744

38-
In a multilanguage setup, it is necessary to set the name of the content item for a specified culture:
45+
In a multi-language setup, it is necessary to set the name of the content item for a specified culture:
3946

4047
```csharp
41-
demoproduct.SetCultureName("Microphone", "en-us"); // this will set the english name
42-
demoproduct.SetCultureName("Mikrofon", "da"); // this will set the danish name
48+
demoProduct.SetCultureName("Microphone", "en-us"); // this will set the english name
49+
demoProduct.SetCultureName("Mikrofon", "da"); // this will set the danish name
4350
```
4451

4552
For information on how to retrieve multilingual languages, see the [Retrieving languages](./retrieving-languages.md) article.
53+
54+
## Publishing content programmatically
55+
56+
The ContentService is also used for publishing operations.
57+
58+
The following example shows a page being published with all descendants.
59+
60+
```csharp
61+
using Umbraco.Cms.Core.Models;
62+
using Umbraco.Cms.Core.Services;
63+
64+
namespace Umbraco.Cms.Web.UI.Custom;
65+
66+
public class PublishContentDemo
67+
{
68+
private readonly IContentService _contentService;
69+
70+
public PublishContentDemo(IContentService contentService) => _contentService = contentService;
71+
72+
public void Publish(Guid key)
73+
{
74+
IContent? content = _contentService.GetById(key)
75+
?? throw new InvalidOperationException($"Could not find content with key: {key}.");
76+
77+
_contentService.SaveAndPublishBranch(content, PublishBranchFilter.Default);
78+
}
79+
}
80+
```
81+
82+
The `PublishBranchFilter` option can include one or more of the following flags:
83+
84+
- `Default` - publishes existing published content with pending changes.
85+
- `IncludeUnpublished` - publishes unpublished content and existing published with pending changes.
86+
- `ForceRepublish` - publishes existing published content with or without pending changes.
87+
- `All` - combines `IncludeUnpublished` and `ForceRepublish`.

15/umbraco-cms/tutorials/editors-manual/getting-started-with-umbraco/creating-saving-and-publishing-content.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,8 @@ To publish the node with descendants, follow these steps:
6060
2. Select the arrow next to the **Save and Publish** button.
6161
3. Select **Publish with descendants**.
6262

63-
![Publish with descendants](images/Publish-with-descendants-v15.png)
64-
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.
65-
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.
63+
![Publish with descendants](images/Publish-with-descendants-v14.png)
64+
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.
6665

6766
#### 3: Unpublish
6867

0 commit comments

Comments
 (0)