How to change Sitecore field value based on another field(s)











up vote
2
down vote

favorite












Is there a way to dynamically change the value of a field in Sitecore based on other fields' values, something like computed fields in RDBMS?



For example, let's say we have a template that has the following fields:




  • First Name

  • Last Name

  • Full Name


What I need to accomplish here is to change Full Name everytime a First Name or Last Name has changed.










share|improve this question


























    up vote
    2
    down vote

    favorite












    Is there a way to dynamically change the value of a field in Sitecore based on other fields' values, something like computed fields in RDBMS?



    For example, let's say we have a template that has the following fields:




    • First Name

    • Last Name

    • Full Name


    What I need to accomplish here is to change Full Name everytime a First Name or Last Name has changed.










    share|improve this question
























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      Is there a way to dynamically change the value of a field in Sitecore based on other fields' values, something like computed fields in RDBMS?



      For example, let's say we have a template that has the following fields:




      • First Name

      • Last Name

      • Full Name


      What I need to accomplish here is to change Full Name everytime a First Name or Last Name has changed.










      share|improve this question













      Is there a way to dynamically change the value of a field in Sitecore based on other fields' values, something like computed fields in RDBMS?



      For example, let's say we have a template that has the following fields:




      • First Name

      • Last Name

      • Full Name


      What I need to accomplish here is to change Full Name everytime a First Name or Last Name has changed.







      fieldtype






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 10 at 11:11









      Jaffal

      1458




      1458






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          6
          down vote



          accepted










          If you really need to dynamically change value of Full Name field, you can create your own ItemSave handler which will change this field for particular item templates.



          You can use something similar:



          public class CustomItemSaveEventHandler
          {
          //master database name
          public static readonly string Master = "master";
          //sample template id
          public static readonly string TemplateIdItem = "{your_template_id_goes_here}";

          public void OnItemSaved(object sender, EventArgs args)
          {
          Item item = Event.ExtractParameter(args, 0) as Item;
          if (item != null && item.Database.Name.ToLower() == Master)
          {
          if (item.TemplateID.ToString() == TemplateIdItem )
          {
          item.Editing.BeginEdit();
          item.Fields["Full Name"] = item.Fields["First Name"] + " " + item.Fields["Last Name"];
          item.Editing.EndEdit();
          item.Editing.AcceptChanges();
          }
          }
          }
          }


          Add some logic when First or Last names are missing and so on.



          Don't forget to create a patch file :



          <?xml version="1.0"?>
          <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
          <sitecore>
          <events>
          <event name="item:saved">
          <handler type="MyAssembly.MyNamespace.CustomItemSaveEventHandler, MyAssembly" method="OnItemSaved">
          </handler>
          </event>
          </events>
          </sitecore>
          </configuration>





          share|improve this answer























          • Thanks, Peter, but this will run for each item save on the tree, which might lead to a performance issue, I'm looking for a simpler approach is available.
            – Jaffal
            Dec 10 at 11:46






          • 3




            That is the way to handle this. If you do the check on the template and exit the handler if it's not the required template, you won't get a performance hit on the CM server. Make sure you set the config to not add the handler for the CD roles and you will be fine.
            – Richard Seal
            Dec 10 at 12:02






          • 2




            To further improve performance (and prevent infinite loops), you should also check the list of item changes to make sure either the First or Last name fields were changed in this save. See an example of that: sitecore.stackexchange.com/a/14742/342
            – Dan Sinclair
            Dec 10 at 13:43











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "664"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsitecore.stackexchange.com%2fquestions%2f15454%2fhow-to-change-sitecore-field-value-based-on-another-fields%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          6
          down vote



          accepted










          If you really need to dynamically change value of Full Name field, you can create your own ItemSave handler which will change this field for particular item templates.



          You can use something similar:



          public class CustomItemSaveEventHandler
          {
          //master database name
          public static readonly string Master = "master";
          //sample template id
          public static readonly string TemplateIdItem = "{your_template_id_goes_here}";

          public void OnItemSaved(object sender, EventArgs args)
          {
          Item item = Event.ExtractParameter(args, 0) as Item;
          if (item != null && item.Database.Name.ToLower() == Master)
          {
          if (item.TemplateID.ToString() == TemplateIdItem )
          {
          item.Editing.BeginEdit();
          item.Fields["Full Name"] = item.Fields["First Name"] + " " + item.Fields["Last Name"];
          item.Editing.EndEdit();
          item.Editing.AcceptChanges();
          }
          }
          }
          }


          Add some logic when First or Last names are missing and so on.



          Don't forget to create a patch file :



          <?xml version="1.0"?>
          <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
          <sitecore>
          <events>
          <event name="item:saved">
          <handler type="MyAssembly.MyNamespace.CustomItemSaveEventHandler, MyAssembly" method="OnItemSaved">
          </handler>
          </event>
          </events>
          </sitecore>
          </configuration>





          share|improve this answer























          • Thanks, Peter, but this will run for each item save on the tree, which might lead to a performance issue, I'm looking for a simpler approach is available.
            – Jaffal
            Dec 10 at 11:46






          • 3




            That is the way to handle this. If you do the check on the template and exit the handler if it's not the required template, you won't get a performance hit on the CM server. Make sure you set the config to not add the handler for the CD roles and you will be fine.
            – Richard Seal
            Dec 10 at 12:02






          • 2




            To further improve performance (and prevent infinite loops), you should also check the list of item changes to make sure either the First or Last name fields were changed in this save. See an example of that: sitecore.stackexchange.com/a/14742/342
            – Dan Sinclair
            Dec 10 at 13:43















          up vote
          6
          down vote



          accepted










          If you really need to dynamically change value of Full Name field, you can create your own ItemSave handler which will change this field for particular item templates.



          You can use something similar:



          public class CustomItemSaveEventHandler
          {
          //master database name
          public static readonly string Master = "master";
          //sample template id
          public static readonly string TemplateIdItem = "{your_template_id_goes_here}";

          public void OnItemSaved(object sender, EventArgs args)
          {
          Item item = Event.ExtractParameter(args, 0) as Item;
          if (item != null && item.Database.Name.ToLower() == Master)
          {
          if (item.TemplateID.ToString() == TemplateIdItem )
          {
          item.Editing.BeginEdit();
          item.Fields["Full Name"] = item.Fields["First Name"] + " " + item.Fields["Last Name"];
          item.Editing.EndEdit();
          item.Editing.AcceptChanges();
          }
          }
          }
          }


          Add some logic when First or Last names are missing and so on.



          Don't forget to create a patch file :



          <?xml version="1.0"?>
          <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
          <sitecore>
          <events>
          <event name="item:saved">
          <handler type="MyAssembly.MyNamespace.CustomItemSaveEventHandler, MyAssembly" method="OnItemSaved">
          </handler>
          </event>
          </events>
          </sitecore>
          </configuration>





          share|improve this answer























          • Thanks, Peter, but this will run for each item save on the tree, which might lead to a performance issue, I'm looking for a simpler approach is available.
            – Jaffal
            Dec 10 at 11:46






          • 3




            That is the way to handle this. If you do the check on the template and exit the handler if it's not the required template, you won't get a performance hit on the CM server. Make sure you set the config to not add the handler for the CD roles and you will be fine.
            – Richard Seal
            Dec 10 at 12:02






          • 2




            To further improve performance (and prevent infinite loops), you should also check the list of item changes to make sure either the First or Last name fields were changed in this save. See an example of that: sitecore.stackexchange.com/a/14742/342
            – Dan Sinclair
            Dec 10 at 13:43













          up vote
          6
          down vote



          accepted







          up vote
          6
          down vote



          accepted






          If you really need to dynamically change value of Full Name field, you can create your own ItemSave handler which will change this field for particular item templates.



          You can use something similar:



          public class CustomItemSaveEventHandler
          {
          //master database name
          public static readonly string Master = "master";
          //sample template id
          public static readonly string TemplateIdItem = "{your_template_id_goes_here}";

          public void OnItemSaved(object sender, EventArgs args)
          {
          Item item = Event.ExtractParameter(args, 0) as Item;
          if (item != null && item.Database.Name.ToLower() == Master)
          {
          if (item.TemplateID.ToString() == TemplateIdItem )
          {
          item.Editing.BeginEdit();
          item.Fields["Full Name"] = item.Fields["First Name"] + " " + item.Fields["Last Name"];
          item.Editing.EndEdit();
          item.Editing.AcceptChanges();
          }
          }
          }
          }


          Add some logic when First or Last names are missing and so on.



          Don't forget to create a patch file :



          <?xml version="1.0"?>
          <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
          <sitecore>
          <events>
          <event name="item:saved">
          <handler type="MyAssembly.MyNamespace.CustomItemSaveEventHandler, MyAssembly" method="OnItemSaved">
          </handler>
          </event>
          </events>
          </sitecore>
          </configuration>





          share|improve this answer














          If you really need to dynamically change value of Full Name field, you can create your own ItemSave handler which will change this field for particular item templates.



          You can use something similar:



          public class CustomItemSaveEventHandler
          {
          //master database name
          public static readonly string Master = "master";
          //sample template id
          public static readonly string TemplateIdItem = "{your_template_id_goes_here}";

          public void OnItemSaved(object sender, EventArgs args)
          {
          Item item = Event.ExtractParameter(args, 0) as Item;
          if (item != null && item.Database.Name.ToLower() == Master)
          {
          if (item.TemplateID.ToString() == TemplateIdItem )
          {
          item.Editing.BeginEdit();
          item.Fields["Full Name"] = item.Fields["First Name"] + " " + item.Fields["Last Name"];
          item.Editing.EndEdit();
          item.Editing.AcceptChanges();
          }
          }
          }
          }


          Add some logic when First or Last names are missing and so on.



          Don't forget to create a patch file :



          <?xml version="1.0"?>
          <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
          <sitecore>
          <events>
          <event name="item:saved">
          <handler type="MyAssembly.MyNamespace.CustomItemSaveEventHandler, MyAssembly" method="OnItemSaved">
          </handler>
          </event>
          </events>
          </sitecore>
          </configuration>






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 10 at 13:10









          Richard Seal

          13.4k32458




          13.4k32458










          answered Dec 10 at 11:38









          Peter Procházka

          4,5251837




          4,5251837












          • Thanks, Peter, but this will run for each item save on the tree, which might lead to a performance issue, I'm looking for a simpler approach is available.
            – Jaffal
            Dec 10 at 11:46






          • 3




            That is the way to handle this. If you do the check on the template and exit the handler if it's not the required template, you won't get a performance hit on the CM server. Make sure you set the config to not add the handler for the CD roles and you will be fine.
            – Richard Seal
            Dec 10 at 12:02






          • 2




            To further improve performance (and prevent infinite loops), you should also check the list of item changes to make sure either the First or Last name fields were changed in this save. See an example of that: sitecore.stackexchange.com/a/14742/342
            – Dan Sinclair
            Dec 10 at 13:43


















          • Thanks, Peter, but this will run for each item save on the tree, which might lead to a performance issue, I'm looking for a simpler approach is available.
            – Jaffal
            Dec 10 at 11:46






          • 3




            That is the way to handle this. If you do the check on the template and exit the handler if it's not the required template, you won't get a performance hit on the CM server. Make sure you set the config to not add the handler for the CD roles and you will be fine.
            – Richard Seal
            Dec 10 at 12:02






          • 2




            To further improve performance (and prevent infinite loops), you should also check the list of item changes to make sure either the First or Last name fields were changed in this save. See an example of that: sitecore.stackexchange.com/a/14742/342
            – Dan Sinclair
            Dec 10 at 13:43
















          Thanks, Peter, but this will run for each item save on the tree, which might lead to a performance issue, I'm looking for a simpler approach is available.
          – Jaffal
          Dec 10 at 11:46




          Thanks, Peter, but this will run for each item save on the tree, which might lead to a performance issue, I'm looking for a simpler approach is available.
          – Jaffal
          Dec 10 at 11:46




          3




          3




          That is the way to handle this. If you do the check on the template and exit the handler if it's not the required template, you won't get a performance hit on the CM server. Make sure you set the config to not add the handler for the CD roles and you will be fine.
          – Richard Seal
          Dec 10 at 12:02




          That is the way to handle this. If you do the check on the template and exit the handler if it's not the required template, you won't get a performance hit on the CM server. Make sure you set the config to not add the handler for the CD roles and you will be fine.
          – Richard Seal
          Dec 10 at 12:02




          2




          2




          To further improve performance (and prevent infinite loops), you should also check the list of item changes to make sure either the First or Last name fields were changed in this save. See an example of that: sitecore.stackexchange.com/a/14742/342
          – Dan Sinclair
          Dec 10 at 13:43




          To further improve performance (and prevent infinite loops), you should also check the list of item changes to make sure either the First or Last name fields were changed in this save. See an example of that: sitecore.stackexchange.com/a/14742/342
          – Dan Sinclair
          Dec 10 at 13:43


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Sitecore Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsitecore.stackexchange.com%2fquestions%2f15454%2fhow-to-change-sitecore-field-value-based-on-another-fields%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          Origin of the phrase “under your belt”?