Add edit button to content page

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 :slight_smile:

1 Like

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;
1 Like

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!

:high-5-you-rock:

1 Like

But doesn’t this cause database lookups for each request? It could potentially impact database performance I think.

1 Like

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.