0

One of the Projects I am working on, I have a requirement to read the lookup column Value in a document library while uploading a document.

For example, if someone uploads a document to the SharePoint library, they will select a value in the lookup field(mandatory) and once they hit on Save/CheckIn to save document in the library, it should alert with the lookup value that user chose.

Please let me know, how to achieve this using JSOM approach?

0

We can add the custom jQuery code in the default edit form like the picture below. And edit the default edit form, add the script web part and add the custom jQuery code in the script web part.

enter image description here

There is a demo.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        $("#ctl00_ctl40_g_208827b4_894c_4842_bbdf_f311bae2ed4a_ctl00_ctl02_ctl00_toolBarTbl_RightRptControls_ctl00_ctl00_diidIOSaveItem").click(function () {
            var value = $("select[title='look1 Required Field']").find("option:selected").text();
            alert(value);
        });
    });
</script>

The result as below, when I click the “check in”, it will alert the selected value in lookup column.

enter image description here

There is a similar post:

https://social.msdn.microsoft.com/Forums/office/en-US/529ee28f-80bf-44a6-9cb4-392e74213076/how-to-get-value-from-lookup-field-on-onchange-event?forum=sharepointdevelopmentprevious

To change the name of the file as the “Name and lookup column value”, you could refer to the demo below.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
    var look1 = $("select[title='look1 Required Field']").find("option:selected").val();
    var name = $("input[title='Name Required Field']").val();
    $("input[title='Name Required Field']").val(name+" "+look1);

    $("select[title='look1 Required Field']").change(function(){
        $("input[title='Name Required Field']").val(name+" "+$(this).find("option:selected").val());
    });
});
</script>

The result as below: enter image description here

enter image description here

  • thanks a bunch, I will try this and get back to you if it works and will mark it as answer as well. – mdevm May 2 '18 at 13:15
  • thanks so much. That worked like a charm. I had one additional question, now that I am able to read the Entity ID with your help. Is it possible to rename the file name while upload. I want to append Entity ID (lookup column, from where I am reading the field value) at the end of the file name. Let's say the user uploads a document named 'Test Doc' and then if a user selects lookup column value and clicks on CheckIN, then I would want to rename the File name as 'Test Doc'+lookup column value which we are alerting so something like Test Doc 46. Note 46 is the value. – mdevm May 2 '18 at 14:41
  • I have updated my reply. – Amy_MSFT May 7 '18 at 2:45

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.