1

I have to get other column value of list item whose "Title" matches with entered string in text box from the page. I am using CAML query to match "Title". But in Success function, how to get that item's other field like ...Title, Department etc.. Below if my tried code.

Code in CEWP:

<input name="pop" id="pop" type="text"/> <input name="b1" id="b1" onclick="getTheList()" type="button" value="Submit"/><script>

function getTheList() {
alert('inside getTheList fucntion');

 var test=$("#pop").val();
 alert(test); 
 clientContext = new SP.ClientContext.get_current();
 var web = clientContext.get_web();
 var myList = web.get_lists().getByTitle('URL_List');
 var query = new SP.CamlQuery();
 query.set_viewXml('<View><Query><Where><Eq><FieldRef Name="Title"/><Value Type="Text">+ test + </Value></Eq></Where></Query></View>');      
 this.ListItem = myList.getItems(query); 
 clientContext.load(ListItem,'title'); 
clientContext.executeQueryAsync(Function.createDelegate(this, this.mySuccessFunction), Function.createDelegate(this, this.myFailFunction));
return false;
}

function mySuccessFunction(sender, args) 
{    
alert('Success');
var title = this.ListItem.get_item("Title"); 
alert(title);
//how to get title and other field if an Item here...??
  }
function myFailFunction(sender, args) 
{ 
alert('inside fail');
 alert("GetListItemById Failed. \n" + args.get_message() + "\n" + args.get_stackTrace()); 
 }</script>
  • its giving me Title undefined..! – Rajeev K Nov 10 '14 at 12:21
  • clientContext.load(ListItem,'Title'); instead of title. Missing uppercase? – Sky Nov 10 '14 at 12:39
  • nope...Does not work..:( – Rajeev K Nov 10 '14 at 12:42
  • Replace 'CSOM' to 'JSOM' in title please. – Alexander Ulmaskulov Nov 21 '17 at 7:27
1

As part of context.load you need to request for details which you need

Something like

clientContext.load(ListItem,'Include(Title,Department)'; 

Fix following error in your code

 query.set_viewXml('<View><Query><Where><Eq><FieldRef Name="Title"/><Value Type="Text">+ test + </Value></Eq></Where></Query></View>');   

to

 query.set_viewXml('<View><Query><Where><Eq><FieldRef Name="Title"/><Value Type="Text">'+ test + '</Value></Eq></Where></Query></View>');   

Update (Below code is working for me

<input name="pop" id="pop" type="text"/> <input name="b1" id="b1" onclick="getTheList()" type="button" value="Submit"/><script>

<script>
    var ListItem;
    function getTheList() {
        alert('inside getTheList fucntion');
        var test=$("#pop").val();
        alert(test); 
        clientContext = new SP.ClientContext.get_current();
        var web = clientContext.get_web();
        var myList = web.get_lists().getByTitle('URL_List');
        var query = new SP.CamlQuery();
        query.set_viewXml('<View><Query><Where><Eq><FieldRef Name="Title"/><Value Type="Text">' + test + '</Value></Eq></Where></Query></View>');      
        ListItem = myList.getItems(query); 
        clientContext.load(ListItem,'Include(Title)'); 
        clientContext.executeQueryAsync(Function.createDelegate(this, this.mySuccessFunction), Function.createDelegate(this, this.myFailFunction));
        return false;
    }

    function mySuccessFunction(sender, args) 
    {    
        var listItemEnumerator = ListItem.getEnumerator();
        while (listItemEnumerator.moveNext())        {
        var item = listItemEnumerator.get_current();
        var title = item.get_item('Title');
 }      }
    function myFailFunction(sender, args) 
    { 
        alert('inside fail');
        alert(args.get_message() + "\n" + args.get_stackTrace()); 
    }
</script>
  • thanks Amal but it didnt work! – Rajeev K Nov 10 '14 at 12:56
  • @RajeevK your CAML query is wrong. Try my updated answer – Amal Hashim Nov 10 '14 at 12:58
  • no..its not working.. – Rajeev K Nov 10 '14 at 13:05
  • thanks for your help Amal..! I appreciate :) but i really wonder why its not working at my place..! really strange.! frustrating.! – Rajeev K Nov 10 '14 at 17:51
  • Use the code I posted above. And modify mySuccessFunction to loop through the result as Nadeem suggested. – Amal Hashim Nov 10 '14 at 17:54
0

clientContext.load(ListItem,'title'); should be clientContext.load(ListItem,'Include(Title)');. In order to show other fields just add them in Include section separated by commas. For example 'Include(Title, Id)'. Make sure to use internal names of the fields.

Update

You need to loop through the list items in success handler that you get using the query, something like this:

var listItemEnumerator = this.ListItem.getEnumerator();
 while (listItemEnumerator.moveNext()) {
     var item = listItemEnumerator.get_current();
     var title = item.get_item('Title');
     }
  • I tried this..but it did not work for me..! Is there any other mistake in code..?! – Rajeev K Nov 10 '14 at 12:50
  • all the way it gives me title undefined.! what I am trying is..I have one text box from where I am entering some string..and comparing that with Title of items through caml query. – Rajeev K Nov 10 '14 at 12:53
  • i tried this also..didnt help.! Damn..! can figure out whts the problem in code..! – Rajeev K Nov 10 '14 at 13:03
  • finally I succeeded with REST and Sp services. Thanks for your help. really appreciate ..:) – Rajeev K Nov 18 '14 at 14:25

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.