Create a custom property editor in Umbraco


Umbraco User’s Guide

This post is helpful in following scenarios –

  • If you are looking to create a custom property editor in Umbraco
  • If you are looking for a way to populate a checkbox list in the content dynamically
  • If you are looking for a way to call an api in umbraco property editor
  • If you are looking for a way to populate a checkbox list with one of the document types dynamically

For this post, we are going to build a custom property editor that will populate the checkbox list with categories (added under home) using an api url.

Let’s say, we have a food website that have different categories like Healthy, Baking, Recipes, Dinner, Summer recipes, etc.
We also have articles related to all these different categories.

Categories under Home –

Content with categories as checkbox list

What is the problem?

Now the problem here is whenever a new category is added, the checkbox list for categories has to be updated manually as we have use the default checkbox list data type.

In order to fix this problem, we will create a custom property editor and use it to create the checkbox list dynamically. So whenever, a new category will be added, the checkbox list for categories will be updated automatically.

What is the solution?

Skills needed to build a Property Editor –

  • Umbraco BackOffice (Using ver 8.0.1 for this post)
  • HTML
  • CSS
  • Javascript (Optional)
  • Angular (Optional)

Files needed for a Property Editor –

  • Manifest
  • HTML file
  • Javascript file
  • CSS (Optional)

First, let’s create a manifest file.

{
  propertyEditors: [
    {
      alias: "CustomCheckBoxList",
      name: "Custom Checkbox list",
      editor: {
        view: "~/App_Plugins/CheckboxListing/checkboxlist.html",
        hideLabel: false,
        valueType: "STRING"
      },
	  prevalues: {
			fields: [				
				{
					label: "Api Url",
					description: "Add the API Url to get the list of items. api/{controller}/{Action}",
					key: "apiUrl",
					view: "textstring"
				}
			]
		}
    }
  ],
  javascript: [
    "~/App_Plugins/CheckboxListing/checkboxlist.controller.js"
  ]
}

Now, let’s add the HTML file

<div ng-controller="CheckboxList.Controller">
   
    <ul class="unstyled">
        <li ng-repeat="category in selectedItems">
            <label class="checkbox">
                <input type="checkbox" name="checkboxlist"
                       value="{{category.key}}"
                       ng-model="category.selected"/>
                {{category.val}}
            </label>
        </li>
    </ul>
</div>

Now, we have to create the Javascript file, which will contain the main logic of this property editor.

angular.module("umbraco")
    .controller("CheckboxList.Controller",
        function($scope, $http, assetsService) {
            var newItems = [];
            $scope.selectedItems = [];
         
            var categories = function() {
                return $http.get($scope.model.config.apiUrl)
                    .then(function(data) {
                        $scope.categories = data;
                        for (var i = 0; i < $scope.categories.data.length; i++) {
                            newItems.push({ id: $scope.categories.data[i].id, value: $scope.categories.data[i].name });
                        }
                        $scope.categories = newItems;
                    });
            }
            categories()
                .then(function() {
                    $scope.$on("formSubmitting",
                        function(ev, args) {
                            if ($scope.model.value === null || $scope.model.value === undefined) {
                                $scope.model.value = [];
                            }
                            var selectedCategories = "";

                            angular.forEach($scope.selectedItems,
                                function(value, key) {                                    
                                    var itemSelected = value.selected;
                                    var item = value.val;

                                    selectedCategories = itemSelected === true
                                        ? selectedCategories + item + ","
                                        : selectedCategories + "";
                                });
                            $scope.model.value = selectedCategories.replace(/(^,)|(,$)/g, "");

                        });


                    function setupViewModel() {
                        if ($scope.model.value === null || $scope.model.value === undefined) {
                            $scope.model.value = [];
                        }

                        for (var i = 0; i < $scope.categories.length; i++) {
                            var isSelected = $scope.model.value.includes($scope.categories[i].value);
                           
                            $scope.selectedItems.push({
                                selected: isSelected,
                                key: $scope.categories[i].id,
                                val: $scope.categories[i].value
                            });
                        }
                    }

                    setupViewModel();

                    $scope.$watch("selectedItems",
                        function(newVal, oldVal) {
                            $scope.model.value = [];

                            for (var x = 0; x < $scope.selectedItems.length; x++) {
                                if ($scope.selectedItems[x].checked) {
                                    $scope.model.value.push($scope.selectedItems[x].key);
                                }
                            }
                        },
                        true);


                    $scope.model.onValueChanged = function(newVal, oldVal) {
                        setupViewModel();
                    };
                });
        });


That’s it!! You are done with the coding part.

What’s Next?

Just rebuild your project and go to backoffice where you have to create a new data type for the categories.

Go to Settings – Right click on Data Types to create a new data type.
Select datatype that we just created – Custom Checkbox List

Now, you must be thinking that what is Api Url. Ok, so this is the url of the api you will create to get the categories. I am using categories for this post. However, you can use this field to add other api urls as well to populate the checkbox list with anything you want.

Now, we will add the newly created document type to our content page.

And when you load the content again, you will see the categories populated in the checkbox list.

So now, whenever a new category will be added, the checkbox list will be auto-populated with the new one.

Note: It is good to create a datatype like this before you actually create a content as later on if you delete and update a property in your content, you have to update the data associated as well which could be a complex task sometimes.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.