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>
4
  • its giving me Title undefined..!
    – Rajeev K
    Nov 10, 2014 at 12:21
  • clientContext.load(ListItem,'Title'); instead of title. Missing uppercase?
    – Sky
    Nov 10, 2014 at 12:39
  • nope...Does not work..:(
    – Rajeev K
    Nov 10, 2014 at 12:42
  • Replace 'CSOM' to 'JSOM' in title please. Nov 21, 2017 at 7:27

2 Answers 2

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>
5
  • thanks Amal but it didnt work!
    – Rajeev K
    Nov 10, 2014 at 12:56
  • @RajeevK your CAML query is wrong. Try my updated answer Nov 10, 2014 at 12:58
  • no..its not working..
    – Rajeev K
    Nov 10, 2014 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, 2014 at 17:51
  • Use the code I posted above. And modify mySuccessFunction to loop through the result as Nadeem suggested. Nov 10, 2014 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');
     }
4
  • I tried this..but it did not work for me..! Is there any other mistake in code..?!
    – Rajeev K
    Nov 10, 2014 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, 2014 at 12:53
  • i tried this also..didnt help.! Damn..! can figure out whts the problem in code..!
    – Rajeev K
    Nov 10, 2014 at 13:03
  • finally I succeeded with REST and Sp services. Thanks for your help. really appreciate ..:)
    – Rajeev K
    Nov 18, 2014 at 14:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.