Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
I have a .NET macro that I am migrating from an older version of Umbraco (7.4.2) and I need to determine what groups the current user is a part of.
I am coding in Visual Basic. Every hint and tip I have found thus far seems to reference methods that don't exist.
Here is the code I was previously using that no longer works:
If umbraco.BusinessLogic.User.GetCurrent IsNot Nothing Then
Dim currentUser As String = umbraco.BusinessLogic.User.GetCurrent.UserType.Name
If currentUser = "Administrators" Then
showAdmin = True
The umbraco.BusinessLogic.User namespace is marked as obsolete in the later versions of Umbraco 7, so you'll need to use the UserService instead to update your macro.
Here's a basic C# example that gets the current user and lists the groups they belong too. Sorry it's not in VB, but hopefully it points you in the right direction.
var username = UmbracoContext.Security.CurrentUser.Username;
var user = ApplicationContext.Current.Services.UserService.GetByUsername(username);
var groups = user.Groups;
@foreach (var group in groups)
Thank you for the quick response. This does help. My problem now is that I need to create an object reference to get access to the 'userservices' methods. Below is the requirements. I don't know what any of these types are. Is there an easier way?
Public Sub New(provider As Umbraco.Core.Persistence.UnitOfWork.IDatabaseUnitOfWorkProvider, repositoryFactory As Umbraco.Core.Persistence.RepositoryFactory, logger As Umbraco.Core.Logging.ILogger, eventMessagesFactory As Umbraco.Core.Events.IEventMessagesFactory)
Hi Ross, I decided to create a quick test myself.
Right enough, there's an issue retrieving the user - since there's no authentication at all (more here if you're interested), the UmbracoContext.Security.CurrentUserwon't work. This works fine for back-office stuff, but not on the front-end (that ones on me!)
I've updated things, and the following solution below seems to work (again in C#).
var userTicket = new System.Web.HttpContextWrapper(System.Web.HttpContext.Current).GetUmbracoAuthTicket();
if (userTicket != null)
var currentUser = ApplicationContext.Services.UserService.GetByUsername(userTicket.Name);
var groups = currentUser.Groups;
foreach (var group in groups)
Again, thank you for the response. However, I don't have any issue with getting the current user. My issue is accessing the 'UserServices' methods. Visual Studio is claiming that I need an object reference to do this since the methods are not shared. To create an object, I need to declare a new one, using the 'New' method I posted above.
Again, it just seems like there is an easier way. What you are suggesting seems quite simple, but I don't know what your "UmbracoContext" or "ApplicationContext" are.
Here is the code I typed up. It gets by the compiler but I get a runtime error saying I can't put 'nothing' in for the User Service.
Dim username = umbraco.helper.GetCurrentUmbracoUser.LoginName
Dim g As New umbraco.Core.Services.UserService(Nothing, Nothing, Nothing, Nothing)
Dim user = g.GetByUsername(username)
For Each group In user.Groups
lblNote.Text += group.Name + "<br />"
I was able to figure it out thanks to your post and from this one:
'ApplicationContext' is a member of 'Core'. I wasn't able to find the 'UmbracoContext' you referenced, but below is the finished solution:
Dim ticket = New System.Web.HttpContextWrapper(System.Web.HttpContext.Current).GetUmbracoAuthTicket()
If ticket IsNot Nothing Then
Dim user = umbraco.Core.ApplicationContext.Current.Services.UserService.GetByUsername(ticket.Name)
For Each group In user.Groups
If group.Name = "Administrators" Then showAdmin = True
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted