0

I have added the below code, But I am not able to retrieve the web or site. And added this script in Content editor web part of list. Can you please let me know where did I go wrong?

$(document).ready(function() {
    $(ctl00_ctl34_g_9308082e_bd1c_4559_868d_9aa8eb1da6aa_ctl00_ctl05_ctl01_ctl00_ctl00_ctl04_ctl00_ctl00_UserField_checkNames).attr("onclick", "ValidateForm()");
});

function ValidateForm() {
    var url = window.location.href;
    var vars = url.split("=");
    var ID = vars[1].split("&");
    var ID1 = ID[0];
    alert(ID1);
    var context = new SP.ClientContext("http://siteName");
    alert(context);
    var web = context.get_web();
    alert(web);
    alert
    var list = context.get_web().get_lists().getByTitle('ListName');
    alert(list);
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='ID'/><Value Type ='Text'>" + ID1 + "</Value></Eq></Where></Query></View>");
    listitem = list.getItems(camlQuery);
    alert(camlQuery);
    alert(listitem);
    context.load(listitem);
    context.executeQueryAsync(Function.createDelegate(this, this.onSuccess), Function.createDelegate(this, this.onFailed));

}

function onSuccess(sender, args) {
    var ListEnumerator = listitem.getEnumerator();
    while (ListEnumerator.moverNext()) {
        var currentItem = ListEnumerator.get_current();
        var id = currentItem.get_item("ID");
        alert(id);
    }
}

function onFailed(sender, args) {
    alert("error");
}
1
  • Use F12 developer tools > Console and reload the page to see what error message(s) you get. Commented Aug 24, 2020 at 10:49

1 Answer 1

0

My test code for your reference:

<script type="text/javascript">

    SP.SOD.executeOrDelayUntilScriptLoaded(ValidateForm,"sp.js");

function ValidateForm(){
var url = window.location.href;
    var vars = url.split("=");
    var ID = vars[1].split("&");
    var ID1 = ID[0];
    var clientContext = new SP.ClientContext("http://sitename");
    var list = clientContext.get_web().get_lists().getByTitle("list name");
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml(
        "<View><Query><Where><Eq><FieldRef Name='ID'/><Value Type ='Counter'>" + ID1 + "</Value></Eq></Where></Query></View>");
    var items = list.getItems(camlQuery);
    clientContext.load(items);
    clientContext.executeQueryAsync(Function.createDelegate(this, onSuccess),Function.createDelegate(this, onFail));
    function onSuccess(){
        var itemArray = [];
        var ListEnumerator = items.getEnumerator();
        while (ListEnumerator.moveNext()) {
        var currentItem = ListEnumerator.get_current();
        var id = currentItem.get_item("ID");
        alert(id);
     }
        
    }
    function onFail(sender,args){
    alert(args.get_message());
    }
}

</script>

You error: enter image description here And you jQuery selector should be wrong.

jQuery selector documentation for your reference:

https://api.jquery.com/category/selectors/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.