0

I have a template which contains a General Link field. If a new item is created of that template, then the link field should be populated with the item's current path.

Suppose, template A have a general link field called CTA. Every time a new item is created using template A that CTA field is populated with the item's path.

How to achieve this functionality? Thanks for the help.

Update based on comments below:

I was thinking to perform this at rendering time but now I am looking something at the template level. Whenever a new item is created of that template, the CTA field populated with the item's current path. That's why I asked if we can create a custom token for general link field or not? –

  • 1
    Are you looking for a script or code that runs through the 100 items, or are you looking for a solution for doing this at runtime? – Mark Cassidy Apr 23 '19 at 11:40
1

Render Time:

To perform this at rendering time, simply check if the General Link field is empty when populating your View Model. If it is empty, you can then get the current items Url and use that.

Lets assume the view model is:

public class MyViewModel {

   public HtmlString MyLinkField { get; set; }

}

In the controller;

if (item["MyLinkField"] == string.Empty)
{
    var url = LinkManager.GetItemUrl(Sitecore.Context.Item);
    viewModel.MyLinkField = $"<a href=\"@url\">Work out your title here</a>";
}
else 
{
    // todo: render the link normally here
}

That is really rough pseudo code to get you going. But that should work to replace empty links with a link to the current page at render time.

On Creation

To do this on item creation, you can hook into the item:created or item:creating events and set the value of the link field based on the code below:

item.Editing.BeginEdit();
LinkField linkField = item.Fields["MyLinkField"];
if (string.IsNullOrEmpty(linkField?.TargetID))
{
    linkField.TargetID = Sitecore.Context.Item.ID;
    linkField.Text = Sitecore.Context.Item[Sitecore.FieldIDs.DisplayName];
    linkField.LinkType = "internal";
}
item.Editing.EndEdit();

Caveats

This method should be disabled for the Experience Editor - as it would mean that content editors could not add a link to the field unless one was already there.

There may be better ways to check if a General Link field is empty.

| improve this answer | |
  • linkField.TargetID, TargetID is throwing a guid which is not available in the Sitecore. It is not pointing to the current context item. – PSS Apr 23 '19 at 13:04
-2

You can do something like this. You can optimize this code, this is just an example.

      var link = (Sitecore.Data.Fields.LinkField)item.Fields[GeneralLink];

      if (link != null && string.IsNullOrEmpty(link.Url))
      {           

        contextItem.Editing.BeginEdit();
        using (new EditContext(contextItem))
        {
          contextItem.Fields["GeneralLink"].Value = LinkManager.GetItemUrl(Sitecore.Context.Item);
        }
        contextItem.Editing.EndEdit();

      }
| improve this answer | |
  • If you are using .Editing.BeginEdit() you don't need a new EditContext - besides, you shouldn't use EditContext anyway: kamsar.net/index.php/2017/01/EditContext-Considered-Harmful – Richard Seal Apr 23 '19 at 11:39
  • Also, the raw value of a GeneralLink field is not just the plain Url – Richard Seal Apr 23 '19 at 11:40
  • @RichardSeal Can we implement this functionality by creating a custom token? Because in this case value is going to change when the item is rendered. And is it possible to pass a custom token in General Link field? – PSS Apr 23 '19 at 11:44
  • @PrabhatSharma try to make your opening question clearer, what are you trying to achieve with this? It sounds like you are trying to do something at rendering time. All the answers so far are saving the content to the content database – Richard Seal Apr 23 '19 at 11:47
  • @RichardSeal Initially I was thinking to perform this at rendering time but now I am looking something at the template level. Whenever a new item is created of that template, the CTA field populated with the item's current path. That's why I asked if we can create a custom token for general link field or not? – PSS Apr 23 '19 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.