Copied to clipboard

Flag this post as spam?

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


  • Thomas 315 posts 602 karma points c-trib
    Jul 02, 2020 @ 08:19
    Thomas
    0

    Check UmbracoUrlAlias existis

    Hey

    I'm trying to make a check that looks at a page when it's published to check if a given UmbracoUrlAlias already exisits.

    But when I check the value on the published node, the value is null...

    My function:

    private void ContentService_Publishing(IContentService sender, ContentPublishingEventArgs e)
        {
            foreach (var node in e.PublishedEntities)
            {
                using (UmbracoContextReference contextReference = _umbracoContext.EnsureUmbracoContext())
                {
                    UmbracoContext context = contextReference.UmbracoContext;
                    IPublishedContent content = context.Content.GetById(node.Id);
                    var altUrl = node.GetValue<string>("umbracoUrlAlias");
                    if (!string.IsNullOrEmpty(altUrl) & altUrl != null)
                    {
                        var roots = context.Content.GetAtRoot();
                        foreach (var root in roots)
                        {
                            var UrlAliasExists = root.DescendantsOrSelf().Where(x => x.Id != node.Id && altUrl.ToString() == x.Value<string>("umbracoUrlAlias")).ToList();
    
                            //var UrlAliasExists = root.Descendants().Where(x => x.Id != node.Id && altUrl.ToString() == x.Value<string>("umbracoUrlAlias")).ToList();
                            if (UrlAliasExists.Any())
                            {
                                e.Cancel = true;
                                e.Messages.Add(new Umbraco.Core.Events.EventMessage("fejl i url alias", string.Format("alias [{0}] der er valgt bruges allerede", node.GetValue("umbracourlalias")), Umbraco.Core.Events.EventMessageType.Error));
    
                            }
                        }
                    }
                }
            }
        }
    

    My variable "altUrl" are always null... ?

    Have tried: - node.GetValue

    Anyone that can see what i'm doing wrong ?

    Version: Umbraco 8.6.3

  • Yakov Lebski 553 posts 2117 karma points
    Jul 03, 2020 @ 15:36
    Yakov Lebski
    0

    You try get IPublishedContent from new element what not published or not saved yet

    try hey value from node

    node.GetValue("umbracoUrlAlias")
    
  • Thomas 315 posts 602 karma points c-trib
    Jul 04, 2020 @ 19:24
    Thomas
    0

    Hey

    I'm also doing that.

    var altUrl = node.GetValue<string>("umbracoUrlAlias");
    

    But it's always null ?...

  • Yakov Lebski 553 posts 2117 karma points
    Jul 04, 2020 @ 22:29
    Yakov Lebski
    100

    may be you have variants enabled? if yes you need check property by culture too

    var altUrl = node.GetValue<string>("umbracoUrlAlias","<CULTURE>");
    
  • Thomas 315 posts 602 karma points c-trib
    Jul 07, 2020 @ 13:06
    Thomas
    0

    I think that did it.. Makes it a litte more bigger to check on each culture node has UmbracoUrlAlias that matches another one..

    Why is I possible to have the samme UmbracoUrlAlias on diffrent nodes... ?

Please Sign in or register to post replies

Write your reply to:

Draft