Skip to content

Commit fc538d4

Browse files
authored
Merge pull request #7559 from umbraco/content-service-update
Updated ContentService article
2 parents 659f4a1 + 713d890 commit fc538d4

File tree

3 files changed

+66
-39
lines changed

3 files changed

+66
-39
lines changed

15/umbraco-cms/reference/management/using-services/contentservice.md

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
description: Example on how to create and publish content programmatically using the ContentService.
2+
description: Example on how to create and publish content programmatically using the `IContentService`.
33
---
44

55
# Content Service
@@ -8,7 +8,9 @@ Learn how to use the Content Service.
88

99
## Creating content programmatically
1010

11-
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.
11+
In the example below, a new content item is programmatically created using the content service. It is assumed that there are two document types, namely *Catalogue* and *Product*. A new *Product* node will be created under the *Catalogue* page.
12+
13+
Create a new C# class file (for example, `MyProject/Services/PublishContentDemo.cs`) inside your web project.
1214

1315
```csharp
1416
using Umbraco.Cms.Core.Models;
@@ -36,9 +38,12 @@ public class PublishContentDemo
3638
// Set the value of the property with alias 'price'
3739
demoProduct.SetValue("price", "1500");
3840

39-
// Save and publish the child item
41+
// Save content first
4042
_contentService.Save(demoProduct);
41-
_contentService.Publish(demoProduct, ["en-us", "da"]);
43+
44+
// Publish content
45+
var userId = 0; // 0 = system user
46+
_contentService.Publish(demoProduct, new[] { "*" }, userId); // use "*" for invariant content
4247
}
4348
}
4449
```
@@ -54,35 +59,39 @@ For information on how to retrieve multilingual languages, see the [Retrieving l
5459

5560
## Publishing content programmatically
5661

57-
The ContentService is also used for publishing operations.
62+
The `IContentService` is also used for publishing existing content. The following example shows how to publish a page and all its descendants.
5863

59-
The following example shows a page being published with all descendants.
64+
Create a new C# class file (for example, `MyProject/Services/PublishBranchContentDemo.cs`) inside your web project.
6065

6166
```csharp
6267
using Umbraco.Cms.Core.Models;
6368
using Umbraco.Cms.Core.Services;
6469

6570
namespace Umbraco.Cms.Web.UI.Custom;
6671

67-
public class PublishContentDemo
72+
public class PublishBranchContentDemo
6873
{
6974
private readonly IContentService _contentService;
7075

71-
public PublishContentDemo(IContentService contentService) => _contentService = contentService;
76+
public PublishBranchContentDemo(IContentService contentService) => _contentService = contentService;
7277

7378
public void Publish(Guid key)
7479
{
75-
IContent? content = _contentService.GetById(key)
80+
var parentKey = Guid.Parse("b6fbbb31-a77f-4f9c-85f7-2dc4835c7f31");
81+
82+
var content = _contentService.GetById(key)
7683
?? throw new InvalidOperationException($"Could not find content with key: {key}.");
7784

78-
_contentService.PublishBranch(content, PublishBranchFilter.Default, ["en-us", "da"]);
85+
var userId = 0;
86+
_contentService.PublishBranch(content, PublishBranchFilter.Default, new[] { "*" }, userId);
7987
}
8088
}
8189
```
8290

8391
The `PublishBranchFilter` option can include one or more of the following flags:
8492

85-
- `Default` - publishes existing published content with pending changes.
86-
- `IncludeUnpublished` - publishes unpublished content and existing published with pending changes.
87-
- `ForceRepublish` - publishes existing published content with or without pending changes.
93+
- `Default` - publishes only nodes with pending changes.
94+
- `IncludeUnpublished` - publishes unpublished content and existing nodes with pending changes.
95+
- `ForceRepublish` - republishes all nodes, even if unchanged.
8896
- `All` - combines `IncludeUnpublished` and `ForceRepublish`.
97+
- For multilingual content, replace `"*"` with the array of cultures, for example, `new[] { "en-us", "da" }`.

16/umbraco-cms/reference/management/using-services/contentservice.md

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
description: Example on how to create and publish content programmatically using the ContentService.
2+
description: Example on how to create and publish content programmatically using the `IContentService`.
33
---
44

55
# Content Service
@@ -8,7 +8,9 @@ Learn how to use the Content Service.
88

99
## Creating content programmatically
1010

11-
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.
11+
In the example below, a new content item is programmatically created using the content service. It is assumed that there are two document types, namely *Catalogue* and *Product*. A new *Product* node will be created under the *Catalogue* page.
12+
13+
Create a new C# class file (for example, `MyProject/Services/PublishContentDemo.cs`) inside your web project.
1214

1315
```csharp
1416
using Umbraco.Cms.Core.Models;
@@ -36,9 +38,12 @@ public class PublishContentDemo
3638
// Set the value of the property with alias 'price'
3739
demoProduct.SetValue("price", "1500");
3840

39-
// Save and publish the child item
41+
// Save content first
4042
_contentService.Save(demoProduct);
41-
_contentService.Publish(demoProduct, ["en-us", "da"]);
43+
44+
// Publish content
45+
var userId = 0; // 0 = system user
46+
_contentService.Publish(demoProduct, new[] { "*" }, userId); // use "*" for invariant content
4247
}
4348
}
4449
```
@@ -54,35 +59,39 @@ For information on how to retrieve multilingual languages, see the [Retrieving l
5459

5560
## Publishing content programmatically
5661

57-
The ContentService is also used for publishing operations.
62+
The `IContentService` is also used for publishing existing content. The following example shows how to publish a page and all its descendants.
5863

59-
The following example shows a page being published with all descendants.
64+
Create a new C# class file (for example, `MyProject/Services/PublishBranchContentDemo.cs`) inside your web project.
6065

6166
```csharp
6267
using Umbraco.Cms.Core.Models;
6368
using Umbraco.Cms.Core.Services;
6469

6570
namespace Umbraco.Cms.Web.UI.Custom;
6671

67-
public class PublishContentDemo
72+
public class PublishBranchContentDemo
6873
{
6974
private readonly IContentService _contentService;
7075

71-
public PublishContentDemo(IContentService contentService) => _contentService = contentService;
76+
public PublishBranchContentDemo(IContentService contentService) => _contentService = contentService;
7277

7378
public void Publish(Guid key)
7479
{
75-
IContent? content = _contentService.GetById(key)
80+
var parentKey = Guid.Parse("b6fbbb31-a77f-4f9c-85f7-2dc4835c7f31");
81+
82+
var content = _contentService.GetById(key)
7683
?? throw new InvalidOperationException($"Could not find content with key: {key}.");
7784

78-
_contentService.PublishBranch(content, PublishBranchFilter.Default, ["en-us", "da"]);
85+
var userId = 0;
86+
_contentService.PublishBranch(content, PublishBranchFilter.Default, new[] { "*" }, userId);
7987
}
8088
}
8189
```
8290

8391
The `PublishBranchFilter` option can include one or more of the following flags:
8492

85-
- `Default` - publishes existing published content with pending changes.
86-
- `IncludeUnpublished` - publishes unpublished content and existing published with pending changes.
87-
- `ForceRepublish` - publishes existing published content with or without pending changes.
93+
- `Default` - publishes only nodes with pending changes.
94+
- `IncludeUnpublished` - publishes unpublished content and existing nodes with pending changes.
95+
- `ForceRepublish` - republishes all nodes, even if unchanged.
8896
- `All` - combines `IncludeUnpublished` and `ForceRepublish`.
97+
- For multilingual content, replace `"*"` with the array of cultures, for example, `new[] { "en-us", "da" }`.

17/umbraco-cms/reference/management/using-services/contentservice.md

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
description: Example on how to create and publish content programmatically using the ContentService.
2+
description: Example on how to create and publish content programmatically using the `IContentService`.
33
---
44

55
# Content Service
@@ -8,7 +8,9 @@ Learn how to use the Content Service.
88

99
## Creating content programmatically
1010

11-
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.
11+
In the example below, a new content item is programmatically created using the content service. It is assumed that there are two document types, namely *Catalogue* and *Product*. A new *Product* node will be created under the *Catalogue* page.
12+
13+
Create a new C# class file (for example, `MyProject/Services/PublishContentDemo.cs`) inside your web project.
1214

1315
```csharp
1416
using Umbraco.Cms.Core.Models;
@@ -36,9 +38,12 @@ public class PublishContentDemo
3638
// Set the value of the property with alias 'price'
3739
demoProduct.SetValue("price", "1500");
3840

39-
// Save and publish the child item
41+
// Save content first
4042
_contentService.Save(demoProduct);
41-
_contentService.Publish(demoProduct, ["en-us", "da"]);
43+
44+
// Publish content
45+
var userId = 0; // 0 = system user
46+
_contentService.Publish(demoProduct, new[] { "*" }, userId); // use "*" for invariant content
4247
}
4348
}
4449
```
@@ -54,35 +59,39 @@ For information on how to retrieve multilingual languages, see the [Retrieving l
5459

5560
## Publishing content programmatically
5661

57-
The ContentService is also used for publishing operations.
62+
The `IContentService` is also used for publishing existing content. The following example shows how to publish a page and all its descendants.
5863

59-
The following example shows a page being published with all descendants.
64+
Create a new C# class file (for example, `MyProject/Services/PublishBranchContentDemo.cs`) inside your web project.
6065

6166
```csharp
6267
using Umbraco.Cms.Core.Models;
6368
using Umbraco.Cms.Core.Services;
6469

6570
namespace Umbraco.Cms.Web.UI.Custom;
6671

67-
public class PublishContentDemo
72+
public class PublishBranchContentDemo
6873
{
6974
private readonly IContentService _contentService;
7075

71-
public PublishContentDemo(IContentService contentService) => _contentService = contentService;
76+
public PublishBranchContentDemo(IContentService contentService) => _contentService = contentService;
7277

7378
public void Publish(Guid key)
7479
{
75-
IContent? content = _contentService.GetById(key)
80+
var parentKey = Guid.Parse("b6fbbb31-a77f-4f9c-85f7-2dc4835c7f31");
81+
82+
var content = _contentService.GetById(key)
7683
?? throw new InvalidOperationException($"Could not find content with key: {key}.");
7784

78-
_contentService.PublishBranch(content, PublishBranchFilter.Default, ["en-us", "da"]);
85+
var userId = 0;
86+
_contentService.PublishBranch(content, PublishBranchFilter.Default, new[] { "*" }, userId);
7987
}
8088
}
8189
```
8290

8391
The `PublishBranchFilter` option can include one or more of the following flags:
8492

85-
- `Default` - publishes existing published content with pending changes.
86-
- `IncludeUnpublished` - publishes unpublished content and existing published with pending changes.
87-
- `ForceRepublish` - publishes existing published content with or without pending changes.
93+
- `Default` - publishes only nodes with pending changes.
94+
- `IncludeUnpublished` - publishes unpublished content and existing nodes with pending changes.
95+
- `ForceRepublish` - republishes all nodes, even if unchanged.
8896
- `All` - combines `IncludeUnpublished` and `ForceRepublish`.
97+
- For multilingual content, replace `"*"` with the array of cultures, for example, `new[] { "en-us", "da" }`.

0 commit comments

Comments
 (0)