Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading the list items Sharepoint 2010 client object model

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?

like image 468
user346514 Avatar asked Oct 06 '22 11:10

user346514


2 Answers

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.

like image 175
Maksym Avatar answered Oct 10 '22 04:10

Maksym


Remove the Query tags from the CAML query stored in myQueryString. The tags are added implicitly when the query is run.

It's tripped me up before, too. The insidious thing about it is that the query won't ever fail outright; sometimes it works, sometimes it doesn't, making it a pain to debug.

like image 30
CBono Avatar answered Oct 10 '22 03:10

CBono