Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Kedar More 3 posts 93 karma points
    Mar 02, 2023 @ 05:45
    Kedar More
    0

    Umbraco 10: Need to keep underscore character in document type property when we create document type from code.

    I am using Umbraco 10.

    I am trying to create Document types from code. I noticed that whenever the property alias is having underscore ("_") character in it's Alias, it is getting removed. For some reason we need to keep the underscore character in it. Here is sample piece of code that I am using (for testing purpose the code is placed in a template/razor view) -

    @using Umbraco.Cms.Core;
    @using Umbraco.Cms.Core.Strings;
    @using Umbraco.Cms.Core.Models;
    @using Umbraco.Cms.Web.Common.PublishedModels;
    @using Umbraco.Cms.Core.IO;
    @using Umbraco.Cms.Core.Services;
    @using Umbraco.Extensions;
    @using System.IO;
    @using Umbraco.Cms.Core.PropertyEditors
    @using Umbraco.Cms.Core.Models.Editors;
    
    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage;
    
    @inject IContentTypeService ContentTypeService
    @inject IDataTypeService DataTypeService
    @{
        Layout = null;
    
    
        DefaultShortStringHelperConfig.Config helperconfig = new DefaultShortStringHelperConfig.Config();
        helperconfig.StringType = CleanStringType.Unchanged;
    
        DefaultShortStringHelperConfig config = new DefaultShortStringHelperConfig();
        config = config.WithConfig(CleanStringType.Unchanged, helperconfig);
    
        var stringhelper = new DefaultShortStringHelper(config);
    
    
        var ct = new ContentType(stringhelper, -1);
        ct.Name = "TestDocumentType";
        ct.Alias = "testDocumentType";
    
        // properties
        PropertyType pt = new PropertyType(stringhelper, "Umbraco.Textbox", ValueStorageType.Nvarchar);
        pt.Name = "Name_test";
        pt.Alias = "name_test";
        ct.AddPropertyType(pt);
        pt = new PropertyType(stringhelper, "Umbraco.Textbox", ValueStorageType.Nvarchar);
        pt.Name = "Property2_Test";
        pt.Alias = "property2_test";
        ct.AddPropertyType(pt);
    
        ct.Icon = "icon-defrag color-red";
        ct.AllowedAsRoot = true;
    
        //ContentTypeService.Save(ct);
    }
    
    

    When debugging, it is noticed that after assigning the value to Alias, Umbraco is cleaning up the value and removing the underscore character. This is due to PropertyType.cs class in umbraco does the cleaning for Alias value. We need to override this default behavior but not able to do so. In above example tried with providing CleanString type in configuration but not works.

    Will be great help if you have any solution to above scenario.

    Thanks.

  • Kedar More 3 posts 93 karma points
    Mar 02, 2023 @ 10:47
    Kedar More
    100

    We are able to resolve the issue. Here is the updated code

    @using Umbraco.Cms.Core;
    @using Umbraco.Cms.Core.Strings;
    @using Umbraco.Cms.Core.Models;
    @using Umbraco.Cms.Web.Common.PublishedModels;
    @using Umbraco.Cms.Core.IO;
    @using Umbraco.Cms.Core.Services;
    @using Umbraco.Extensions;
    @using System.IO;
    @using Umbraco.Cms.Core.PropertyEditors
    @using Umbraco.Cms.Core.Models.Editors;
    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage;
    @inject IContentTypeService ContentTypeService
    @inject IDataTypeService DataTypeService
    @{
        Layout = null;
    
        DefaultShortStringHelperConfig.Config helperconfig = new DefaultShortStringHelperConfig.Config
                {
                    PreFilter = (new DefaultShortStringHelperConfig()).ApplyUrlReplaceCharacters,
                    IsTerm = (c, leading) => leading
                    ? char.IsLetter(c) // only letters
                        : (char.IsLetterOrDigit(c) || c == '_'), // letter, digit or underscore
                    StringType = CleanStringType.Ascii | CleanStringType.UmbracoCase,
                    BreakTermsOnUpper = false
                };
    
        DefaultShortStringHelperConfig config = new DefaultShortStringHelperConfig();
        config = config.WithConfig(CleanStringType.Alias, helperconfig);
    
        var stringhelper = new DefaultShortStringHelper(config);
    
    
        var ct = new ContentType(stringhelper, -1);
        ct.Name = "TestDocumentType";
        ct.Alias = "testDocumentType";
    
        // properties
        PropertyType pt = new PropertyType(stringhelper, "Umbraco.Textbox", ValueStorageType.Nvarchar);
        pt.Name = "PUN_csr";
        pt.Alias = "pun_csr";
        ct.AddPropertyType(pt);
        pt = new PropertyType(stringhelper, "Umbraco.Textbox", ValueStorageType.Nvarchar);
        pt.Name = "GNR_csr";
        pt.Alias = "gnr_csr";
        ct.AddPropertyType(pt);
    
        ct.Icon = "icon-defrag color-red";
        ct.AllowedAsRoot = true;
    
        ContentTypeService.Save(ct);
        }
    

    Here some changes are made for configuration. For this referred the code from GitHub.

    https://github.com/umbraco/Umbraco-CMS/blob/v9/contrib/src/Umbraco.Core/Strings/DefaultShortStringHelperConfig.cs

    If anyone has any other link for detailed documentation on this will be more helpful in understanding the behavior.

    For now, we are able to create the property with alias containing underscore character.

Please Sign in or register to post replies

Write your reply to:

Draft