diff --git a/14/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/block-editor/block-grid-editor.md b/14/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/block-editor/block-grid-editor.md index 1aa54aa5bf1..4d307d67f35 100644 --- a/14/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/block-editor/block-grid-editor.md +++ b/14/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/block-editor/block-grid-editor.md @@ -243,6 +243,7 @@ These Partial Views must be placed in the same folder as before, (`Views/Partial The following is an example of a Partial View for a Block Type of type `MyElementTypeAliasOfContent`. {% code title="MyElementTypeAliasOfContent.cshtml" %} + ```csharp @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage; @@ -252,11 +253,13 @@ The following is an example of a Partial View for a Block Type of type `MyElemen @* Render the block areas *@ @await Html.GetBlockGridItemAreasHtmlAsync(Model) ``` + {% endcode %} If you are using ModelsBuilder, you can make the property rendering strongly typed by letting your view accept a model of type `BlockGridItem`. For example: {% code title="MyElementTypeAliasOfContent.cshtml" %} + ```csharp @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage>; @using ContentModels = Umbraco.Cms.Web.Common.PublishedModels; @@ -267,6 +270,7 @@ If you are using ModelsBuilder, you can make the property rendering strongly typ @* Render the block areas *@ @await Html.GetBlockGridItemAreasHtmlAsync(Model) ``` + {% endcode %} #### Stylesheet @@ -296,6 +300,7 @@ The built-in value converter for the Block Grid property editor lets you use the The following example mimics the built-in rendering mechanism for rendering Blocks using Partial Views: {% code title="View.cshtml" %} + ```csharp @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage @using Umbraco.Cms.Core.Models.Blocks @@ -314,11 +319,13 @@ The following example mimics the built-in rendering mechanism for rendering Bloc } } ``` + {% endcode %} If you do not want to use Partial Views, you can access the block item data directly within your rendering: {% code title="View.cshtml" %} + ```csharp @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage @using Umbraco.Cms.Core.Models.Blocks @@ -348,6 +355,7 @@ If you do not want to use Partial Views, you can access the block item data dire } } ``` + {% endcode %} ## Write a Custom Layout Stylesheet @@ -457,16 +465,21 @@ Building Custom Views for Block representations in Backoffice is based on the sa ## Creating a Block Grid programmatically In this example, we will be creating content programmatically for a "spot" Blocks in a Block Grid. + 1. On a document type add a property called **blockGrid**. -2. Then add as editor **Block Grid**. +2. Then add as editor **Block Grid**. 3. In the Block Grid add a new block and click to **Create new Element Type** 4. Name this element type **spotElement** with the following properties: -- a property called **title** with the editor of **Textstring** -- a property called **text** with the editor of **Textstring** -5. Then on the **Settings model** click to add a new Setting. + +* A property called **title** with the editor of **Textstring** + +* A property called **text** with the editor of **Textstring** + +5. Then on the **Settings model** click to add a new Setting. 6. Then click to **Create new Element Type**. 7. Name this element type **spotSettings** with the following properties: -- a property called **featured** with the editor of **True/false**. + +* A property called **featured** with the editor of **True/false**. ![Block Grid - Block Configuration](../../../images/BlockEditorConfigurationProgramatically.png) @@ -536,10 +549,10 @@ All `contentData` and `layoutData` entries need their own unique `Udi` as well a 8. First and foremost, we need models to transform the raw data into Block Grid compatible JSON. Create a class called **Model.cs** containing the following: {% code title="Models.cs" lineNumbers="true" %} + ```csharp -using Newtonsoft.Json; using Umbraco.Cms.Core; - +using System.Text.Json.Serialization; namespace My.Site.Models; // this is the "root" of the block grid data @@ -552,13 +565,13 @@ public class BlockGridData SettingsData = settingsData; } - [JsonProperty("layout")] + [JsonPropertyName("layout")] public BlockGridLayout Layout { get; } - [JsonProperty("contentData")] + [JsonPropertyName("contentData")] public BlockGridElementData[] ContentData { get; } - [JsonProperty("settingsData")] + [JsonPropertyName("settingsData")] public BlockGridElementData[] SettingsData { get; } } @@ -567,7 +580,7 @@ public class BlockGridLayout { public BlockGridLayout(BlockGridLayoutItem[] layoutItems) => LayoutItems = layoutItems; - [JsonProperty("Umbraco.BlockGrid")] + [JsonPropertyName("Umbraco.BlockGrid")] public BlockGridLayoutItem[] LayoutItems { get; } } @@ -582,20 +595,20 @@ public class BlockGridLayoutItem RowSpan = rowSpan; } - [JsonProperty("contentUdi")] + [JsonPropertyName("contentUdi")] public Udi ContentUdi { get; } - [JsonProperty("settingsUdi")] + [JsonPropertyName("settingsUdi")] public Udi SettingsUdi { get; } - [JsonProperty("areas")] + [JsonPropertyName("areas")] // areas are omitted from this sample for abbreviation public object[] Areas { get; } = { }; - [JsonProperty("columnSpan")] + [JsonPropertyName("columnSpan")] public int ColumnSpan { get; } - [JsonProperty("rowSpan")] + [JsonPropertyName("rowSpan")] public int RowSpan { get; } } @@ -610,27 +623,29 @@ public class BlockGridElementData Data = data; } - [JsonProperty("contentTypeKey")] + [JsonPropertyName("contentTypeKey")] public Guid ContentTypeKey { get; } - [JsonProperty("udi")] + [JsonPropertyName("udi")] public Udi Udi { get; } [JsonExtensionData] public Dictionary Data { get; } } ``` + {% endcode %} 9. By injecting [ContentService](../../../../../reference/management/services/contentservice/) and [ContentTypeService](../../../../../reference/management/services/contenttypeservice/) into an API controller, we can transform the raw data into Block Grid JSON. It can then be saved to the target content item. Create a class called **BlockGridTestController.cs** containing the following: {% code title="BlockGridTestController.cs" lineNumbers="true" %} + ```csharp using Microsoft.AspNetCore.Mvc; using My.Site.Models; -using Newtonsoft.Json; using Umbraco.Cms.Core; using Umbraco.Cms.Core.Models; +using Umbraco.Cms.Core.Serialization; using Umbraco.Cms.Core.Services; using Umbraco.Cms.Web.Common.Controllers; @@ -640,11 +655,13 @@ public class BlockGridTestController : UmbracoApiController { private readonly IContentService _contentService; private readonly IContentTypeService _contentTypeService; + private readonly IJsonSerializer _serializer; - public BlockGridTestController(IContentService contentService, IContentTypeService contentTypeService) + public BlockGridTestController(IContentService contentService, IContentTypeService contentTypeService, IJsonSerializer serializer) { _contentService = contentService; _contentTypeService = contentTypeService; + _serializer = serializer; } // POST: /umbraco/api/blockgridtest/create @@ -707,7 +724,7 @@ public class BlockGridTestController : UmbracoApiController spotSettingsData.ToArray()); // serialize the block grid data as JSON and save it to the "blockGrid" property on the content item - var propertyValue = JsonConvert.SerializeObject(blockGridData); + var propertyValue = _serializer.Serialize(blockGridData); content.SetValue("blockGrid", propertyValue); _contentService.Save(content); @@ -715,6 +732,7 @@ public class BlockGridTestController : UmbracoApiController } } ``` + {% endcode %} For the above code `IContent? content = _contentService.GetById(1203);` change the id with your content node that is using the Block Grid. diff --git a/14/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/block-editor/block-list-editor.md b/14/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/block-editor/block-list-editor.md index cb2f0264e96..e8279c8cffa 100644 --- a/14/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/block-editor/block-list-editor.md +++ b/14/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/block-editor/block-list-editor.md @@ -158,7 +158,7 @@ With ModelsBuilder:

@content.Heading

``` -{% embed url="https://www.youtube.com/watch?ab_channel=UmbracoLearningBase&v=ltZTgfIoCtg" %} +{% embed url="" %} Working with Block List Editor in Umbraco {% endembed %} @@ -246,187 +246,3 @@ If you know the Block List Editor only uses a single block, you can cast the col ## Build a Custom Backoffice View Building Custom Views for Block representations in Backoffice is the same for all Block Editors. [Read about building a Custom View for Blocks here](build-custom-view-for-blocks.md) - -## Creating BlockList programmatically - -In this example, we will be creating some Block List objects under the `People` property in the `Home` Document Type. The `People` property implements a Block List Data Type where a `Person` Document Type can be created. The `Person` Document Type has two properties - `user_name` and `user_email`. - -The approach to saving Blocklist content programmatically is as follows. - -The JSON object we will pass into the `People` property will look like this: - -```json -{ - "layout":{ - "Umbraco.BlockList":[ - { - "contentUdi":"umb://element/0da576e5fc7445bd9d789c5ee9fe9c54", - "settingsUdi":"umb://element/7073a102dbcc487986962a3d51820de7" - }, - { - "contentUdi":"umb://element/c6e23e8137d24b409bb5ef41bdb705b9", - "settingsUdi":"umb://element/0467ceb158cc49a1acfd1516f63eccf6" - } - ] - }, - "contentData":[ - { - "contentTypeKey":"aca1158a-bad0-49fc-af6e-365c40683a92", - "udi":"umb://element/0da576e5fc7445bd9d789c5ee9fe9c54", - "user_name":"Janice", - "user_email":"janice@janiceindustries.com" - }, - { - "contentTypeKey":"aca1158a-bad0-49fc-af6e-365c40683a92", - "udi":"umb://element/c6e23e8137d24b409bb5ef41bdb705b9", - "user_name":"John", - "user_email":"john@johnindustries.com" - } - ], - "settingsData":[ - { - "contentTypeKey":"aca1158a-bad0-49fc-af6e-365c40683a92", - "udi":"umb://element/7073a102dbcc487986962a3d51820de7", - "role":"content editor" - }, - { - "contentTypeKey":"aca1158a-bad0-49fc-af6e-365c40683a92", - "udi":"umb://element/0467ceb158cc49a1acfd1516f63eccf6", - "role":"admin" - } - ] -} -``` - -We will be adding two people in `contentData` , whose `udi` values have to be referenced in the `layout` up above. The `contentTypeKey` is in this context the Key value of the Document Type we are using in the Block List (`Person`), and the `udi` we will generate manually. - -One of the approaches would be creating a basic model which we will later serialize into JSON: - -```csharp -using Newtonsoft.Json; -using System.Collections.Generic; -//this class is used to mock the correct JSON structure when the object is serialized -public class Blocklist -{ - public BlockListUdi layout { get; set; } - public List> contentData { get; set; } - public List> settingsData { get; set; } -} -//this is a subclass which corresponds to the "Umbraco.BlockList" section in JSON -public class BlockListUdi -{ - //we mock the Umbraco.BlockList name with JsonPropertyAttribute to match the requested JSON structure - [JsonProperty("Umbraco.BlockList")] - public List> contentUdi { get; set; } - //we do not serialize settingsUdi - [JsonIgnore] - public List> settingsUdi { get; set; } - public BlockListUdi(List> items, List> settings) - { - this.contentUdi = items; - this.settingsUdi = settings; - } -} -//this is our Blocklist's allowed nested element. We make a model for it so we can create some dummy data -public class Person -{ - public string user_name { get; set; } - public string user_email { get; set; } - public Person(string user_name, string user_email) - { - this.user_name = user_name; - this.user_email = user_email; - } -} -``` - -After injecting [ContentService](../../../../../reference/management/services/contentservice/) and [ContentTypeService](../../../../../reference/management/services/contenttypeservice/), we can do the following: - -```csharp - @using Umbraco.Cms.Core.Services; - @using Umbraco.Cms.Core; - @using Umbraco.Cms.Core.Models; - @inject IContentService Services; - @inject IContentTypeService _contentTypeService; - - //if the class containing our code inherits SurfaceController, UmbracoApiController, or UmbracoAuthorizedApiController, we can get ContentService from Services namespace - var contentService = Services; - //not to be confused with ContentService, this service will be useful for getting some Document Type IDs - IContentTypeService contentTypeService = _contentTypeService; - //we are creating two people to be added to Blocklist, which means we need two new Guids - GuidUdi contentUdi1 = new GuidUdi("element", System.Guid.NewGuid()); - //Since these will be BLock List objects the Guids need to mention the "element" keyword - GuidUdi contentUdi2 = new GuidUdi("element", System.Guid.NewGuid()); - //We need to do something similar for the settings data - GuidUdi settingsUdi1 = new GuidUdi("element", System.Guid.NewGuid()); - GuidUdi settingsUdi2 = new GuidUdi("element", System.Guid.NewGuid()); - //using Content Service, we create a new root node with the name Home and Document Type home - IContent request = contentService.Create(nodeName, -1, "home", -1); - //we create some dummy users - Person person1 = new Person("Janice", "janice@janiceindustries.com"); - Person person2 = new Person("John", "john@johnindustries.com"); - //initialize our new empty model to mimic proper JSON structure - Blocklist blocklistNew = new Blocklist(); - //initialize empty person list where we will add our users - var personList = new List>(); - //initalize empty settings list as well - var settingsList = new List>(); - //we get the Content Types to later get the Person Document Type key from - var contentTypes = contentTypeService.GetAll(); - //using the above types, we locate the one that corresponds to the Person Document Type - var personType = contentTypes.Where(n => n.Alias == "person").FirstOrDefault(); - //the dictionaryUdi list here will be passed in the first section of our final JSON object - var dictionaryUdi = new List>(); - //we also initialize a List for settings configuration - var settingUdi = new List>(); - //add person1 - personList.Add(new Dictionary - { - //we need to pass the key of the Block List item type, we used ContentTypeService to obtain it - {"contentTypeKey", personType.Key.ToString()}, - //each item should also have a unique udi. We are passing the one we generated before - {"udi", contentUdi1.ToString()}, - //Document Type custom property - {"user_name", person1.user_name}, - //Document Type custom property - {"user_email", person1.user_email} - }); - settingsList.Add(new Dictionary - { - //in this case the settings is set to use the contentTypeKey as the content - Person document type - {"contentTypeKey", personType.Key.ToString()}, - {"udi", settingsUdi1.ToString()}, - //role is our setting for this blocklist - not a property for the actual content - {"role", "content editor"} - }); - //with person 1 added to the contentData section of our JSON, we also add a reference to layout section - dictionaryUdi.Add(new Dictionary { { "contentUdi", contentUdi1.ToString() }, { "settingsUdi", settingsUdi1.ToString() } }); - //add person2 - personList.Add(new Dictionary - { - {"contentTypeKey", personType.Key.ToString()}, - {"udi", contentUdi2.ToString()}, - {"user_name", person2.user_name}, - {"user_email", person2.user_email} - }); - settingsList.Add(new Dictionary - { - {"contentTypeKey", personType.Key.ToString()}, - {"udi", settingsUdi2.ToString()}, - {"role", "admin"} - }); - dictionaryUdi.Add(new Dictionary { { "contentUdi", contentUdi2.ToString() }, { "settingsUdi", settingsUdi2.ToString() } }); - - //first section of JSON must contain udi references to whatever is in contentData - blocklistNew.layout = new BlockListUdi(dictionaryUdi, settingUdi); - //contentData is a list of our Person objects - blocklistNew.contentData = personList; - //we bind the previously made settings list to our model to be serialized - blocklistNew.settingsData = settingsList; - //bind the serialized JSON data to our property alias, "people" - request.SetValue("people", JsonConvert.SerializeObject(blocklistNew)); - //save and publish the node - contentService.SaveAndPublish(request); -``` - -When adding several items, it is important that each item added in `contentData` section has the corresponding Udi mentioned in the `layout` section as well - otherwise the item will not show. diff --git a/14/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/checkbox-list.md b/14/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/checkbox-list.md index ee6526b974f..c97351f976c 100644 --- a/14/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/checkbox-list.md +++ b/14/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/checkbox-list.md @@ -58,8 +58,9 @@ See the example below to see how a value can be added or changed programmaticall ```csharp @inject IContentService Services; +@using Umbraco.Cms.Core.Serialization @using Umbraco.Cms.Core.Services; -@using Newtonsoft.Json +@inject IJsonSerializer Serializer; @{ // Get access to ContentService var contentService = Services; @@ -71,7 +72,7 @@ See the example below to see how a value can be added or changed programmaticall var content = contentService.GetById(guid); // ID of your page // Set the value of the property with alias 'superHeros'. - content.SetValue("superHeros", JsonConvert.SerializeObject(new[] { "Umbraco", "CodeGarden"})); + content.SetValue("superHeros", Serializer.Serialize(new[] { "Umbraco", "CodeGarden"})); // Save the change contentService.Save(content); @@ -95,6 +96,6 @@ If Modelsbuilder is enabled you can get the alias of the desired property withou @{ // Set the value of the property with alias 'superHeros' -content.SetValue(Home.GetModelPropertyType(_publishedSnapshotAccessor,x => x.SuperHeros).Alias, JsonConvert.SerializeObject(new[] { "Umbraco", "CodeGarden"})); +content.SetValue(Home.GetModelPropertyType(_publishedSnapshotAccessor,x => x.SuperHeros).Alias, Serializer.Serialize(new[] { "Umbraco", "CodeGarden"})); } ``` diff --git a/14/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/dropdown/README.md b/14/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/dropdown/README.md index 5a8d2eed4f6..8410bc50159 100644 --- a/14/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/dropdown/README.md +++ b/14/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/dropdown/README.md @@ -86,7 +86,8 @@ See the example below to see how a value can be added or changed programmaticall ```csharp @using Umbraco.Cms.Core.Services; @inject IContentService Services; -@using Newtonsoft.Json +@using Umbraco.Cms.Core.Serialization +@inject IJsonSerializer Serializer; @{ // Get access to ContentService var contentService = Services; @@ -98,7 +99,7 @@ See the example below to see how a value can be added or changed programmaticall var content = contentService.GetById(guid); // ID of your page // Set the value of the property with alias 'categories'. - content.SetValue("categories", JsonConvert.SerializeObject(new[] { "News" })); + content.SetValue("categories", Serializer.Serialize(new[] { "News" })); // Save the change contentService.Save(content); @@ -121,6 +122,6 @@ If Modelsbuilder is enabled you can get the alias of the desired property withou @inject IPublishedSnapshotAccessor _publishedSnapshotAccessor; @{ // Set the value of the property with alias 'categories' - content.SetValue(Home.GetModelPropertyType(_publishedSnapshotAccessor, x => x.Categories).Alias, JsonConvert.SerializeObject(new[] { "News" })); + content.SetValue(Home.GetModelPropertyType(_publishedSnapshotAccessor, x => x.Categories).Alias, Serializer.Serialize(new[] { "News" })); } ``` diff --git a/14/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/image-cropper.md b/14/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/image-cropper.md index 6ca37d87849..d99e943f434 100644 --- a/14/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/image-cropper.md +++ b/14/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/image-cropper.md @@ -118,13 +118,12 @@ The following sample demonstrates how to add or change the value of an Image Cro ```csharp using Microsoft.AspNetCore.Mvc; -using Newtonsoft.Json; using Umbraco.Cms.Core.Models; using Umbraco.Cms.Core.PropertyEditors; using Umbraco.Cms.Core.PropertyEditors.ValueConverters; +using Umbraco.Cms.Core.Serialization; using Umbraco.Cms.Core.Services; using Umbraco.Cms.Web.Common.Controllers; -using Umbraco.Extensions; namespace Umbraco.Docs.Samples.Web.Property_Editors_Add_Values; @@ -133,16 +132,17 @@ public class CreateImageCropperValuesController : UmbracoApiController private readonly IContentService _contentService; private readonly IMediaService _mediaService; private readonly MediaUrlGeneratorCollection _mediaUrlGeneratorCollection; - + private readonly IJsonSerializer serializer; public CreateImageCropperValuesController( IContentService contentService, IMediaService mediaService, - MediaUrlGeneratorCollection mediaUrlGeneratorCollection) + MediaUrlGeneratorCollection mediaUrlGeneratorCollection, IJsonSerializer serializer) { _contentService = contentService; _mediaService = mediaService; _mediaUrlGeneratorCollection = mediaUrlGeneratorCollection; + this.serializer = serializer; } // /Umbraco/Api/CreateImageCropperValues/CreateImageCropperValues @@ -176,7 +176,7 @@ public class CreateImageCropperValuesController : UmbracoApiController }; // Serialize the image cropper value - var propertyValue = JsonConvert.SerializeObject(imageCropperValue); + var propertyValue = serializer.Serialize(imageCropperValue); // Set the value of the property with alias "cropper" // - remember to add the "culture" parameter if "cropper" is set to vary by culture diff --git a/14/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/multi-url-picker.md b/14/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/multi-url-picker.md index b3935e090a9..96048576a99 100644 --- a/14/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/multi-url-picker.md +++ b/14/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/multi-url-picker.md @@ -53,10 +53,11 @@ See the example below to see how a value can be added or changed programmaticall ```csharp @using Umbraco.Cms.Core; +@using Umbraco.Cms.Core.Serialization @using Umbraco.Cms.Core.Services; -@using Newtonsoft.Json; @using Umbraco.Cms.Core.Models; @inject IContentService Services; +@inject IJsonSerializer Serializer; @{ // Get access to ContentService var contentService = Services; @@ -80,7 +81,7 @@ See the example below to see how a value can be added or changed programmaticall var contentPageUdi = Udi.Create(Constants.UdiEntityType.Document, contentPage.Key); // Create a list with different link types - var externalLink = new List + var externalLinks = new List { // External Link new Link @@ -111,7 +112,7 @@ See the example below to see how a value can be added or changed programmaticall }; // Serialize the list with links to JSON - var links = JsonConvert.SerializeObject(externalLink); + var links = Serializer.Serialize(externalLinks); // Set the value of the property with alias 'footerLinks'. diff --git a/14/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/tags.md b/14/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/tags.md index 19af569fa49..ef988bc8ea4 100644 --- a/14/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/tags.md +++ b/14/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/tags.md @@ -70,9 +70,10 @@ Whenever a tag has been added it will be visible in the typeahead when you start You can use the ContentService to create and update Umbraco content from c# code, when setting tags there is an extension method (SetTagsValue) on IContentBase that helps you set the value for a Tags property. Remember to add the using statement for `Umbraco.Core.Models` to take advantage of it. ```csharp +@using Umbraco.Cms.Core.Serialization @using Umbraco.Cms.Core.Services @inject IContentService Services; -@using Newtonsoft.Json +@inject IJsonSerializer Serializer; @{ // Get access to ContentService var contentService = Services; @@ -84,7 +85,7 @@ You can use the ContentService to create and update Umbraco content from c# code var content = contentService.GetById(guid); // ID of your page // Set the value of the property with alias 'tags'. - content.SetValue("tags", JsonConvert.SerializeObject(new[] { "News", "Umbraco", "Example", "Setting Tags", "Helper" })); + content.SetValue("tags", Serializer.Serialize(new[] { "News", "Umbraco", "Example", "Setting Tags", "Helper" })); // Save the change contentService.Save(content); @@ -107,7 +108,7 @@ If Modelsbuilder is enabled, you can get the alias of the desired property witho @inject IPublishedSnapshotAccessor _publishedSnapshotAccessor; @{ // Set the value of the property with alias 'tags' - content.SetValue(Home.GetModelPropertyType(_publishedSnapshotAccessor, x => x.Tags).Alias, JsonConvert.SerializeObject(new[] { "News", "Umbraco", "Example", "Setting Tags" })); + content.SetValue(Home.GetModelPropertyType(_publishedSnapshotAccessor, x => x.Tags).Alias, Serializer.Serialize(new[] { "News", "Umbraco", "Example", "Setting Tags" })); } ``` diff --git a/14/umbraco-cms/implementation/unit-testing.md b/14/umbraco-cms/implementation/unit-testing.md index 391bb3b6271..84892eb4592 100644 --- a/14/umbraco-cms/implementation/unit-testing.md +++ b/14/umbraco-cms/implementation/unit-testing.md @@ -201,7 +201,7 @@ public class ProductsControllerTests [Test] public void WhenGetAllProductsJson_ThenReturnViewModelWithExpectedJson() { - var json = JsonConvert.SerializeObject(this.controller.GetAllProductsJson().Value); + var json = serializer.Serialize(this.controller.GetAllProductsJson().Value); Assert.AreEqual("[\"Table\",\"Chair\",\"Desk\",\"Computer\",\"Beer fridge\"]", json); }