# Content Service

Learn how to use the Content Service.

## Creating content programmatically

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.

Create a new C# class file (for example, `MyProject/Services/PublishContentDemo.cs`) inside your web project.

```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 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");

        // 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 content first
        _contentService.Save(demoProduct);

        // Publish content
        var userId = -1; // -1 = system user
        _contentService.Publish(demoProduct, new[] { "*" }, userId); // use "*" for invariant content
    }
}
```

{% hint style="info" %}
Always call `Save()` before `Publish()`, as publishing without saving first will not persist the changes.
{% endhint %}

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
```

For information on how to retrieve multilingual languages, see the [Retrieving languages](https://docs.umbraco.com/umbraco-cms/reference/management/using-services/localizationservice) article.

## Publishing content programmatically

The `IContentService` is also used for publishing existing content. The following example shows how to publish a page and all its descendants.

Create a new C# class file (for example, `MyProject/Services/PublishBranchContentDemo.cs`) inside your web project.

```csharp
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Services;

namespace Umbraco.Cms.Web.UI.Custom;

public class PublishBranchContentDemo
{
    private readonly IContentService _contentService;

    public PublishBranchContentDemo(IContentService contentService) => _contentService = contentService;

    public void Publish(Guid key)
    {
        var parentKey = Guid.Parse("b6fbbb31-a77f-4f9c-85f7-2dc4835c7f31");

        var content = _contentService.GetById(key)
            ?? throw new InvalidOperationException($"Could not find content with key: {key}.");

        var userId = -1;
        _contentService.PublishBranch(content, PublishBranchFilter.Default, new[] { "*" }, userId);
    }
}
```

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

* `Default` - publishes only nodes with pending changes.
* `IncludeUnpublished` - publishes unpublished content and existing nodes with pending changes.
* `ForceRepublish` - republishes all nodes, even if unchanged.
* `All` - combines `IncludeUnpublished` and `ForceRepublish`.
* For multilingual content, replace `"*"` with the array of cultures, for example, `new[] { "en-us", "da" }`.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.umbraco.com/umbraco-cms/reference/management/using-services/contentservice.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
