Does anyone know how to code an Edit button for Version 16+? I would like to add an Edit button like Dyfort.Umbraco.EditButton or Flaeng/Umbraco.EditButton. However, these add ons are for older versions.
If I understand it correctly, you want to add an āEdit pageā link to your front-end website, correct?
If so, you will need to link to the document editor for each page, so something like this:
<a target="_blank" href="/umbraco/section/content/workspace/document/edit/@Model.Key">Edit page</a>
To protect the link from being shown to non-logged-in users, you would need some additional details, namely, hooking into Umbracoās authentication mechanism to add a new cookie. This article goes into explaining how to do that:
https://kjac.dev/posts/using-umb_ucontext-with-umbraco-14-plus/
Following that, you will, in fact, get a Razor helper called isAuthenticated, which you can put to use for your edit link:
@using Microsoft.AspNetCore.Authentication
@{
var result = await Context.AuthenticateAsync("MyCookieScheme");
var isAuthenticated = result.Succeeded;
}
@if (isAuthenticated)
{
<a target="_blank" href="/umbraco/section/content/workspace/document/edit/@Model.Key">Edit page</a>
}
The last thing is to style the button a bit, but Iām sure you can figure that out ![]()
Hi Jacob, Thanks for your help. Correct, Iād like to add a button to the front end because there are many blogs and it would be easier for the editors to click it when viewing a link rather than tring to search for the content in back office. What youāre saying sounds like it could work. Iāll give it a try and let you know if I have any questions.
I was able to get the Edit button working with the AutheticateAsync cookie scheme. Thanks again for your help Jacob! One other question, Is there a way to restrict a user group? I have some admin tools I would like to block so the editors canāt get to them.
Iām not sure if you can access that directly on the identity, like this example getting the user name:
var userName = result.Principal?.Identity?.Name;
If not, I see a helper method called AuthorizationHelper.TryGetUmbracoUser(result.Principal) that may be useful for retrieving the Umbraco user, and from there accessing the userās groups.
Iām a bit out of the water here, so there are probably more clever solutions than this.
I did try to get the user groups out of Identity no luck. I donāt see what namespace the AuthorizationHelper is youāre referring to.
It is this interface: Interface IAuthorizationHelper | Umbraco c# Api docs
Namespace: Umbraco.Cms.Core.Security.Authorization
Disclaimer: I have not tried it out.
Hi Jacob, The helper wouldnāt get it. But after some digging I found it by this:
@inject AuthenticationRenderHelper AuthenticationRenderHelper
@inject IUserService UserService
var username = await AuthenticationRenderHelper.UserName();
var user = UserService.GetByUsername(username);
var admin = user?.Groups.Select(x => x.Alias).Contains("admin") ?? false;
Nice thinking to use the UserServiceā¦!
Now all you need to do is wrap it up in a nice service to avoid having business logic directly in your views, I guess!
![]()
But doesnāt this cause database lookups for each request? It could potentially impact database performance I think.
Solid find on the UserService approach. For those worried about performance on high-traffic pages, you could also look into caching the āis adminā check in the userās session once theyāre authenticated. That way you arenāt hitting the database for every page load just to show a button.