0

I have created a script to pull data from a URL. I have managed to store the data to a variable but am a little stuck on how to set it to a list form field.

This is an example of the snippet I was using to do it.

 <script type="text/javascript" src="https://code.jquery.com/jquery-    3.1.1.js"></script>

    <script type="text/javascript">
    function getQuery(){
     var query_string = {};
     var query = window.location.search.substring(1);
     var link= query.split("&");

     for(var i=0; i<link .length;i++) {
        var pair = link[i].split("=");
        var pair2 = pair[1].split("%3D")

        if(pair2[0] === "%3Fduns") {
          //$("input[title='name']").val(pair2[0]);  
          //setFieldValue('name',pair2[0]);
        }
      }
     return param;
   }

</script>

Thanks

1

Update the SharePoint ListItem with value from URL using Javascript:

var parVal;
function getQuery(){
     var query_string = {};
     var query = window.location.search.substring(1);
     var link= query.split("&");

     for(var i=0; i<link .length;i++) {
        var pair = link[i].split("=");
        var pair2 = pair[1].split("%3D")

        if(pair2[0] === "%3Fduns") {
          //$("input[title='name']").val(pair2[0]);  
          //setFieldValue('name',pair2[0]);
        }
      }
     parVal = param; // store param to parVal variable
     updateListItem(); // Call update function
   }

    function updateListItem() {
        var clientContext = new SP.ClientContext.get_current();
        var oList = clientContext.get_web().get_lists().getByTitle('List1'); // Get List

        this.oListItem = oList.getItemById(3); // 3=ID which item you want to update
        oListItem.set_item('name', parVal); // set value 'parVal' from query to field
        oListItem.update();

        clientContext.executeQueryAsync(
            Function.createDelegate(this, this.onQuerySucceeded), 
            Function.createDelegate(this, this.onQueryFailed)
        );
    }

    function onQuerySucceeded() {
        alert('Item updated!');
    }

    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + 
            '\n' + args.get_stackTrace());
    }

Here, assuming that param is the value you have to set to name field.

4
  • Thanks for that, however in updateList Item() the code never runs past – johnnylak Oct 13 '16 at 23:28
  • var clientContext = new SP.ClientContext.get_current(); - Do you know why? – johnnylak Oct 13 '16 at 23:29
  • Unable to get property 'get_current' of undefined or null reference – johnnylak Oct 14 '16 at 3:54
  • try to call updateListItem() function like: SP.SOD.executeFunc('sp.js', 'SP.ClientContext', updateListItem); – Ronak Patel Oct 14 '16 at 3:59

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.