0

I am retrieving a list with caml query and Javascript. Everything works fine besides the fact that I have an image column and not all lists have that column populated: Here is a sample query I am using:

<Where>
  <And>         
     <Eq><FieldRef Name='Name' /> <Value Type=’Text’>'name variable+'</Value></Eq>         
     <Eq><FieldRef Name='Address' /> <Value Type=’Text’>'+address variable+'</Value></Eq>
  </And>
</Where>

Assuming I have a title column and not all title fields (columns) are populated, when I try to retrieve my list - the list items without anything in the title is not returned.

listItemInfo += '\nRoom Name: ' + oListItem.get_item('Title')+ ' ' +'Name: ' + oListItem.get_item('Name') + ' ' + 'Address: ' + oListItem.get_item('Address');

document.getElementById('queryResults').innerHTML=listItemInfo.toString();

How can I return list items regardless of if the Title column is populated or not?

Thanks.

  • I am not sure if I follow you correctly or not, but remove the title from your where clause and put it into a general query. It should return null at least. CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><ViewFields>" Are you capturing your 'title variable' in a foreach? – lazoDev Apr 24 '14 at 15:15
  • @lazoDev - I have updated my original question. Hopefully it makes more sense to you now. Thanks. – sw6 - KTBFFH Apr 24 '14 at 17:32
  • Have you tried using SPServices? Their API is great for list manipulation, especially retrieving list items. It looks as if when you are getting the innerHTML if it is empty, then it is going to break. Might be the reason you are not getting data back because it cant find it. Try it. I will put the code block SPServices uses to get list items for you in an answer below. You can store the SPServices library in the _layouts folder. I would make a new file for it like js or something. Then you can reference it. – lazoDev Apr 24 '14 at 20:33
1

If you are using client-side coding to get the list items, I would think you are using a CEWP? If not, it might be easier to do so if you are just working with this one list. Here is the code for you if you decide to give SPServices a try. I use this within a CEWP myself for my list manipulation:

<script type="text/javascript" src="/_layouts/js/jquery-1.11.0.js"></script>
<script type="text/javascript" src="/_layouts/js/jquery.SPServices-2014.01.js"></script>

$().SPServices({
operation: "GetListItems",
async: false,
listName: "My Custom List",
CAMLViewFields: "<ViewFields><FieldRef Name='ID' /><FieldRef Name='Name' /><FieldRef Name='Address' /></ViewFields>",
completefunc: function (xData, Status) {

//Uncomment the alert to see XML printout of your SharePoint nodes.  This will also give you information about the properties of the node.

//alert(xData.responseXML.xml);

 $(xData.responseXML).SPFilterNode("z:row").each(function() {
//This is an example on how to set your list items to variables     
//var item = $(this);
//var id = item.attr("ows_ID"); 


});

}

});

This code will go through each one of your list items. If there is a null value it will return null instead of breaking and returning nothing. You can then use conditional logic to handle the nulls if you like. You can even use SPServices to update the list items that are null with some information of your choosing. I will let you play around with it good luck to ya! I hope this helps you.

  • @lazoDev- you are absolutely spot on - with what I have the list breaks and returns nothing when it encounters nulls. Are there good links you can point me to for good documentation on SPServices as I haven't seen anything detailed on the methods/functions of SPservices both from Codeplex and elsewhere. Thanks. – sw6 - KTBFFH Apr 25 '14 at 1:03
  • Sure. Here is the link for the documentation. I provided a link to the home page of the site in my last comment above. SPServices is great. I also have a posting there of a code block which retrieves all list items dynamically, including all hidden fields SharePoint uses behind the scenes, and any fields you add in the future without having to go back into the code and change the query. It is under the GetListItems documentation. – lazoDev Apr 25 '14 at 13:11

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.