2

I have the following code in my app to pull details from a sharepoint list.

            string siteUrl = "http://SHAREPOINTURL";

        ClientContext clientContext = new ClientContext(siteUrl);
        clientContext.Credentials = new NetworkCredential("UN", "PW", "DOMAIN");
        SP.List oList = clientContext.Web.Lists.GetByTitle("Licences");

        CamlQuery camlQuery = new CamlQuery();
        camlQuery.ViewXml = "<Where><Eq><FieldRef Name='Account' /><Value Type='Text'>123456</Value></Eq></Where>";

        ListItemCollection collListItem = oList.GetItems(camlQuery);
        clientContext.Load(collListItem);
        clientContext.ExecuteQuery();

        Console.WriteLine("Filtered List: " + collListItem.Count.ToString() + "\n");
        foreach (ListItem oListItem in collListItem)
        {
            Console.WriteLine("Account: {0} \nLicence: {1} \nMAC: {2}\n", oListItem["Account"], oListItem["Licence"], oListItem["MAC"]);
        }

In the sharepoint list I have created multiple test items but every time I run the above code all items in the list are returned regardless of what I use for the camlQuery.

Can anyone let me know where I'm going wrong with this pretty new to C# and never touched sharepoint before this.

Edit1: updated with advice from below.

Edit2: simplified the code but still getting the same problem.

haddow64
  • 676
  • 1
  • 7
  • 24
  • THis question has been answered here: https://stackoverflow.com/questions/22847269/caml-query-to-sharepoint-list-returns-entire-set/22849434#22849434 – nadsy May 06 '15 at 12:46

3 Answers3

5

If you want to return the filtered items, in your case is returning all the items, correct? if yes, this problem is in CAMLQuery ...

I have read a small documentation(link below):

Microsoft MSDN

And I noticed the formatting ViewXml:

camlQuery.ViewXml = "<Where><Eq><FieldRef Name='Account' /><Value Type='Text'>123456</Value></Eq></Where>"

try with this code:

camlQuery.ViewXml = "<View><Query><Where><Eq><FieldRef Name='Account' /><Value Type='Text'>123456</Value></Eq></Where></Query></View>"

I have working using with caml.

some syntax to reflect:

camlquery.query = "<query> ..... </query>";
camlquery.ViewXml = "<view> ..... </view>";

SORRY MY BAD ENGLISH :S

b1c10
  • 163
  • 1
  • 6
2

The issue is your last line of code:

textBoxReadShow.Text = Licence + "\t\t" + MAC + "\n";

You're truncating the text in the textbox each iteration of the loop.

You appear to want to be appending the text to the end of the textbox:

textBoxReadShow.Text += Licence + "\t\t" + MAC + "\n";
Servy
  • 202,030
  • 26
  • 332
  • 449
  • Thanks, that sorted that problem but I the CAML query doesn't appear to be working and I cant spot any problem with it. At the moment it is just returning all items in the list. – haddow64 Aug 08 '13 at 08:07
  • 1
    It is late but if it is a `ViewXml`, shouldn't the start tag be ``? – Si8 Oct 15 '15 at 13:15
-2

Remove<Query> and </Query> from the CAML query.

Le_Fredo
  • 639
  • 3
  • 5