Copied to clipboard

Flag this post as spam?

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


  • Ruby 1 post 71 karma points
    3 days ago
    Ruby
    0

    Extending Umbraco v13 Backoffice - Additional Field in Edit Users Group

    Hi everyone, I am attempting to add an additional field to the umbraco user group 'email'. So far I have managed to inject a custom text field in Umbraco UI and the field in the database table. However, I am having issues sending the email field with the 'Save' button of the user groups and taking over the functionality from the backend logic.

    I also attempted adding a 'Save Email' button but am not managing to call my endpoint from angular. Any help would be greatly appreciated.

    Thank you!

  • girish 20 posts 93 karma points
    2 hours ago
    girish
    0

    Hey @Ruby0,

    Nice job on adding the custom field and updating the database! Now, to get the "Save" button to store the email field properly, you'll need to make sure both the backend and Angular UI are handling it correctly.

    1.Backend: Extend the API Umbraco’s default save method for user groups doesn’t include custom fields, so you need to create a custom API endpoint to handle it.

    Create a custom controller in C# to save the email field:

    [PluginController("CustomUserGroup")]
    public class CustomUserGroupController : UmbracoAuthorizedJsonController
    {
        private readonly IUserGroupService _userGroupService;
    
        public CustomUserGroupController(IUserGroupService userGroupService)
        {
            _userGroupService = userGroupService;
        }
    
        [HttpPost]
        public IActionResult SaveUserGroupEmail(int groupId, string email)
        {
            var group = _userGroupService.GetUserGroupById(groupId);
            if (group == null) return NotFound();
    
            group.SetAdditionalData("Email", email);
            _userGroupService.Save(group);
    
            return Ok(new { success = true });
        }
    }
    

    This ensures the email gets saved when you hit the API.

    2.Frontend: Call the API from Angular

    Now, in your Angular controller, you need to send the email to your custom API when clicking "Save."

    angular.module("umbraco").controller("MyUserGroupController", function ($scope, $http, umbRequestHelper) {
        $scope.saveEmail = function () {
            var data = {
                groupId: $scope.model.id,
                email: $scope.model.email
            };
    
            umbRequestHelper.resourcePromise(
                $http.post("/umbraco/backoffice/CustomUserGroup/SaveUserGroupEmail", data),
                "Failed to save email"
            ).then(function (response) {
                if (response.success) {
                    notificationsService.success("Success", "Email updated!");
                } else {
                    notificationsService.error("Error", "Could not save email.");
                }
            });
        };
    });
    

    Make sure your API is registered and accessible.

    Ensure your Angular model ($scope.model.email) is updating properly.

    If you want to save the email automatically when clicking "Save", hook the API call into Umbraco’s existing save functionality.

    Try and let me know..

Please Sign in or register to post replies

Write your reply to:

Draft