4

Every BasePage template I have has a general link field that needs its value to be set to itself by default, with the ability to change it. Is there anyway to do this on the standard values of the item?

UPDATE: This means full use of standard values, updating as well as creation of the item.

  • User is requesting input and to draw on the experience of the users of the site (best-practices). Please respond following the Good Subjective/Bad Subjective guidelines. stackoverflow.blog/2010/09/good-subjective-bad-subjective – Mark Cassidy Jan 17 '17 at 20:37
  • Clarifying question: If you already have items created, what is the value of that field? is it blank? set to something that resembles the correct item? or just completely wrong? – Pete Navarra Jan 17 '17 at 22:30
2

1. Implement a custom token

You can try to implement a custom token for that if you want to go on the safe way. Here is a great post how you can implement that.

2. Implement a custom field

Possible solution could to use a custom field which is inherited from the default General Link field. Which is returning back the current item if it is empty. Here you can find how to implement a custom field.

You can implement your own class, the following implementation is just a prototype but I think you need something similar.

using Sitecore.Diagnostics;
using Sitecore.Shell.Applications.ContentEditor;


namespace YourProject
{
    public class LinkWithDefault : Link
    {
        /// <summary>
        /// This is copied from Sitecore.Shell.Applications.ContentEditor.Link
        /// </summary>
        private XmlValue XmlValue
        {
            get
            {
                return new XmlValue(GetViewStateString("XmlValue"), "link");
            }
            set
            {
                Assert.ArgumentNotNull(value, "value");
                this.SetViewStateString("XmlValue", value.ToString());
            }
        }

        public override string GetValue()
        {
            var xmlValue = this.XmlValue;
            if (!string.IsNullOrWhiteSpace(xmlValue.ToString()))
            {
                return xmlValue.ToString();
            }

            xmlValue.SetAttribute("id", Sitecore.Context.Item.ID.ToString());
            return xmlValue.ToString();
        }
    }
}

To include this as a new custom field you can follow the instructions here: https://www.sitecore.net/company/blog/474/creating-a-custom-sitecore-field-4246

  • But doesn't a token only capture on creation? I have 300 existing items that would need to use this. – rfraczkowski Jan 17 '17 at 20:14
  • @rfraczkowski you are right, if the items are already there it won't work. – Tamás Tárnok Jan 17 '17 at 20:17
  • 1
    @rfraczkowski I updated the answer. Tomorrow I can provide you the exact solution with code for these. At the moment I can't try it on my PC but you can check the links. – Tamás Tárnok Jan 17 '17 at 20:25
6

Try putting the below in the standard value's raw values:

<link text="link text" linktype="internal" class="" title="" 
                target="" querystring="" id="$id" anchor="" />
  • I would consider this really "hacky". I also have about 300 items that would need to use this that have already been created. – rfraczkowski Jan 17 '17 at 20:11
  • 2
    you have to run a powershell script then. please, submit another question as they have completely different meaning – Andrey Bobrov Jan 17 '17 at 20:17
  • I'm looking for best-practices only, this doesn't cover the full use of standard values. – rfraczkowski Jan 17 '17 at 20:19
  • Comment retracted. I see now what you're really asking. – Pete Navarra Jan 17 '17 at 22:28

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.