Javascript – Reading the list items Sharepoint 2010 client object model

javascriptsharepointsharepoint-2010sharepoint-api

I have a list where I am storing the image URLs and I am trying to read list of items and display the images on the page. For that I wrote the script something like below….

<script type="text/javascript">
    function ViewItem()
    {
        var myQueryString = '<Query><Where><Eq><FieldRef Name="Anchor" /><Value 

Type="Boolean">1</Value></Eq></Where></Query>'; 

        var context = new SP.ClientContext.get_current();
        var web = context.get_web();
        var list = web.get_lists().getByTitle('AnchorImageList');
        var myquery = new SP.CamlQuery();

        myquery.set_viewXml(myQueryString); 

        myItems = list.getItems(myquery);


        context.load(myItems, 'Include(Title,ImageURL)');
        context.executeQueryAsync(Function.createDelegate(this, this.success), 

Function.createDelegate(this, this.failed));
    }
    function success() 
    {

        var LinkURL= "";
        var ImageURL="";
        var ListEnumerator = this.myItems.getEnumerator();
        while(ListEnumerator.moveNext())
        {
            var currentItem = ListEnumerator.get_current();
            LinkURL = currentItem.get_item('Title') ;
            ImageURL= currentItem.get_item('ImageURL');
            document.write('<img src="' + ImageURL+ '"+>');
            alert(LinkURL);

        }

    }
    function failed(sender, args) 
    {
        alert("failed. Message:" + args.get_message());
    }
</script>
<a href="#" onclick="Javascript:ViewItem();">View Items</a>

In my CAML query I am trying to filter items which are tagged yes for "Anchor?"(yes/no column).

But I am seeing all the results even though I tagged few items not to display. What I am doing wrong here. Please someone help me. Also,after the images are loaded on the page, the page is still showing the wheel as if it is processing something. Do I need to do something for this?

Best Answer

try this one:

<View>
  <Query>
    <Where>
      <Eq>
        <FieldRef Name="Anchor" />
        <Value Type="Boolean">1</Value>
      </Eq>
    </Where>
  </Query>
</View>

in case if it doesn't work for you, follow next steps:

  1. Create a list view using standard functionality.
  2. Open it at SharePoint Designier and just copy CAML query from the code.

Hope this will help.