Copied to clipboard

Flag this post as spam?

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


  • alexander odhomi 3 posts 83 karma points
    Nov 10, 2017 @ 19:11
    alexander odhomi
    0

    Umbraco 7.7 user group how to (Not member group or type)

    Hey guys My first post/solution seeking. C# developer Short question, how do I programmatically create new user groups and assign a user to that group. We just upgraded to 7.7.4 from 7.4.2.

    We had to change some of our custom code creating or editing users (not members) with existing user types and assigning permissions to users directly, to now assigning permissions to groups. This is all part of a sync service pushing users into umbraco from another application where users and and categorisation entities called tiers are created.

    The architecture of our application requires that we create multiple new groups unique to the user's role and each tier he has access to. Content also are categorised by these tiers so permissios have to be synced regularly.

    We really need to know the best way to create these new user groups in c# and immediately be able to assign users to them.

    Thanks in advance

  • Matt Darby 28 posts 391 karma points c-trib
    Nov 13, 2017 @ 09:39
    Matt Darby
    102

    Hey Alex,

    You can use the UserService for this. For example:

    // Create a user group
    
    var userGroup = new UserGroup
    {
        Alias = "testgroup",
        Name = "Test Group"
    };
    
    userGroup.AddAllowedSection("content");
    userGroup.AddAllowedSection("media");
    
    Services.UserService.Save(userGroup);
    
    // Assign user to the group
    
    var user = Services.UserService.GetByEmail("[email protected]");
    user.AddGroup(userGroup);
    
    Services.UserService.Save(user);
    

    UserGroup is from Umbraco.Core.Models.Membership

  • alexander odhomi 3 posts 83 karma points
    Nov 13, 2017 @ 10:50
    alexander odhomi
    0

    Thank you Matt, that worked. thanks

  • Mikhail 1 post 71 karma points
    Nov 22, 2017 @ 08:54
    Mikhail
    0

    Hi Matt! Is there a way to do this at 7.7.2? Class UserGroup is internal at this version

  • alexander odhomi 3 posts 83 karma points
    Nov 13, 2017 @ 10:05
    alexander odhomi
    0

    Thank you Matt, I'll try this and get back to you.

  • Simon Dingley 1470 posts 3427 karma points c-trib
    Feb 03, 2018 @ 15:40
    Simon Dingley
    0

    Did you get anywhere with this? I need to programmatically create new user groups and at the moment my hands are tied on how to do it due to the classes being internal.

  • Simon Dingley 1470 posts 3427 karma points c-trib
    Feb 03, 2018 @ 16:09
    Simon Dingley
    0

    I've just upgraded to 7.7.2 and it seems the class is now public so I can do what I need.

  • berex 7 posts 77 karma points
    May 18, 2018 @ 08:48
    berex
    0

    Trying this code in Umbraco 7.10.3. but user cant find any libraries for

    Services.UserService.Save(userGroup);

    Any clues?

Please Sign in or register to post replies

Write your reply to:

Draft