Copied to clipboard

Flag this post as spam?

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


  • Amigo 245 posts 600 karma points
    Feb 15, 2025 @ 16:33
    Amigo
    0

    Custom datatype button to call api controller

    Hi, In Umbraco 13.6.0, Im trying to create a custom data type that has a button. When the button is clicked it should post the current page node id to an api controller. I cant really find any documentation on how to do this.

    I did try something like this. (in app_plugin folder)

    html:

    <div>
    <button type="button" ng-click="vm.sendNodeId($event)">Send Node ID</button></div>
    

    js:

    (function () {
    "use strict";
    
    angular.module("umbraco").controller("CustomButtonController", function ($scope, $http, editorState, notificationsService) {
        var vm = this;
    
        vm.sendNodeId = function (event) {
            // Prevent default form submission
            if (event) event.preventDefault();
    
            var nodeId = editorState.current.id;
            var apiUrl = "/umbraco/api/CustomButtonApi/PostNodeId";
    
            $http.post(apiUrl, { nodeId: nodeId }).then(function (response) {
                notificationsService.success("Success", "Node ID sent successfully!");
            }, function (error) {
    
                notificationsService.error("Error", "Failed to send Node ID.");
            });
        };
    });})();
    

    CS:

    using Umbraco.Cms.Core.PropertyEditors;  
    namespace Umb13_tematest.App_Plugins.CustomButton
    {
        [DataEditor("custom.button", EditorType.PropertyValue, "Custom Button", "~/App_Plugins/CustomButton/custom-button.html")]
        public class CustomButtonPropertyEditor : DataEditor
        {
            public CustomButtonPropertyEditor(IDataValueEditorFactory dataValueEditorFactory)
                : base(dataValueEditorFactory)
            {
            }
        }
    }
    

    and then a controller:

     using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Logging;
    using Umbraco.Cms.Web.Common.Controllers;
    
    namespace Umb13_tematest.App_Plugins.CustomButton
    {
        public class CustomButtonApiController : UmbracoApiController
        {
            private readonly ILogger<CustomButtonApiController> _logger;
    
            public CustomButtonApiController(ILogger<CustomButtonApiController> logger)
            {
                _logger = logger;
            }
    
            [HttpPost]
            public IActionResult PostNodeId([FromBody] NodeRequest request)
            {
                if (request?.NodeId == null)
                {
                    return BadRequest("Invalid node ID.");
                }
    
                _logger.LogInformation($"Received node ID: {request.NodeId}");
                return Ok(new { message = "Node ID received successfully!" });
            }
    
            public class NodeRequest
            {
                public int NodeId { get; set; }
            }
        }
    }
    

    Button is shown in my document, but nothing happen when clicked, and nothing in log..

    Any ideas on how to do this correct?

    Thanks

  • Richard Hamilton 14 posts 85 karma points
    Feb 15, 2025 @ 18:08
    Richard Hamilton
    100

    Try registering the controller

    <div ng-controller="CustomButtonController as vm">
        <button type="button" ng-click="vm.sendNodeId($event)">Send Node ID</button>
    </div>
    
  • Amigo 245 posts 600 karma points
    Feb 15, 2025 @ 18:52
    Amigo
    100

    Problem solved. Thanks Richard

    I also used a package.manifest like this:

       {
      "javascript": [
        "/App_Plugins/CustomButton/custom-button.controller.js"
      ],
      "propertyEditors": [
        {
          "alias": "custom.button",
          "name": "Custom Button",
          "editor": {
            "view": "/App_Plugins/CustomButton/custom-button.html",
            "valueType": "JSON"
          },
          "icon": "icon-globe",
          "group": "Common",
          "isDeprecated": false
    
        }
      ],
      "bundleOptions": "None"
    }
    

    and html like this:

    <div ng-controller="CustomButtonController as vm">
        <button type="button" ng-click="vm.sendNodeId($event)">Send Node ID</button>
    </div>
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies