2

I have a custom Sharepoint 2013 field control where when creating a new item in the list, I need to set the value of the custom field based on the value that was entered in another field.

For example - set the value of a suggested car for a customer based on what their current car is. When entering a new list item, you enter your current car, and then in my custom field control UpdateFieldValueInItem method I want to catch what you entered in your current car field, crunch some data and enter a value into your suggested car field. Suggested car being my custom field control.

Is this possible? Every which way I try to access the value that they have entered in their "current car" field, it always comes back null. Presumably this is because the item has not been saved yet. Can anyone help? Let me know what other information I can provide.

4

It is definitely possible, and there is a number of ways of how you can do it:

  • Create custom field types for both of the fields and perform postback every time value is changed in current car field. This approach is the heaviest one: you have to write quite a lot of code. On the other hand, you can get it done with pure server side (or also it's possible to use js of course).
  • You can use custom RenderingTemplate + ListFieldIterator and a couple of "hacky" techniques for replacing a certain field with your custom control.
  • You can use jQuery and/or SPServices - again, "hacky" approach and thus fragile, yet it works and it might prove the simplest one in your scenario.
  • ...

But I would suggest leveraging CSR for that. CSR stays for Client Side Rendering, and it is the main technique for customizing list forms and list views in SharePoint 2013. Basics about CSR can be found for example in Ross Bradbrook's blog post Introduction to Client Side Rendering in SharePoint 2013.

In this particular case you will need to apply CSR customization to the "Current car" field and possibly to "Suggested car" field as well (depending on how you want to show it). Current car field should be customized so that each time it is changed you update value for the second field.

enter image description here

I would advise to keep the whole solution client-side if that is possible. In this case solution will be better suited for delivering as an App, and Apps is currently the strongly recommended by MS approach for ALL SharePoint 2013 customizations. Usually server-side code is necessary only if you must elevate privileges or access some external service in order to get the suggested car.

For implementing this approach, some knowledge is required:

  1. SharePoint JavaScript Object Model (JSOM)
  2. Client Side Rendering (CSR)

The trend now is that SP2013 greatly relies on Apps and client-side solutions. So if you're still working server-side and don't know these things, perhaps it's time to learn ;)

And here is the example of how you do the CSR customization (please don't forget to read at least the article I referenced above if you don't know what is CSR):

<script type="text/javascript">
  SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
    Templates: {
      OnPostRender: function(ctx){
        var f = ctx.ListSchema.Field[0];
        if (f.Name == "Current_x0020_car")
        {
          var fieldControl = $get(f.Name + "_" + f.Id + "_$" + f.FieldType + "Field");
          $addHandler(fieldControl, "change", function(e)
          {
              var newValue = fieldControl.value;
              var newText = fieldControl[fieldControl.selectedIndex].text;

              // put your logic for determining the suggested car here
              var newSuggestedCarValue = "some new value for car " + newText;

              // update suggested car control
              // (should be done in callback, if suggested car is pulled from somewhere asynchronously)

              var suggestedCarField = window[ctx.FormUniqueId + "FormCtx"].ListSchema["Suggested_x0020_car"];
              $get(suggestedCarField.Name + "_" + suggestedCarField.Id + "_$" + suggestedCarField.FieldType + "Field").value = newSuggestedCarValue;
          });
        }
      }
    }
  });
</script>

(the code is tested and works in my environment)

For simplicity, I just put this code on the new form page using CEWP, before the OOTB ListFormWebPart. And after I selected the current car, I got the following result:

enter image description here

Beware, however, that this particular code will not work for all types of fields. In my case the fields were Lookup for Current car and Text for Suggested car. You have to adapt the code for your setup.

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.