2

I'm trying to printOut some items from a ListItem. But when run the code I get following exception:

"Invalid usage of query execution. The query should be executed by using ExecuteQuery method on the client context object".

This is the code Block:

  static void Main(string[] args)
    {
        try
        {
            ClientContext _clientContext = new ClientContext("http://spweb.reply.eu/sites/RC1/1003/");
        _clientContext.Credentials = new NetworkCredential("mossuser","Reply.96","replynet");
        SP.List list = _clientContext.Web.Lists.GetByTitle("Contenuti");
        CamlQuery query = new SP.CamlQuery();
        query.ViewXml = "@<View><Query><Where><Eq><FieldRef Name='tt_network_id' /><Value Type='Text'>clu_noe</Value></Eq></Where></Query></View>"; 
        var itemts= list.GetItems(query);
        _clientContext.Load(itemts);
        _clientContext.ExecuteQuery();
            Console.Write(itemts.Count().ToString());
        }
        catch (Exception e)
        {

            Console.Write(e.Message);
        }


        }

3 Answers 3

3

I guess you're missing call to clientContext.Load(List) in your code, try this

try
    {
        ClientContext _clientContext = new ClientContext("http://spweb.reply.eu/sites/RC1/1003/");
    _clientContext.Credentials = new NetworkCredential("mossuser","Reply.96","replynet");
    SP.List list = _clientContext.Web.Lists.GetByTitle("Contenuti");
    CamlQuery query = new SP.CamlQuery();
    query.ViewXml = "@<View><Query><Where><Eq><FieldRef Name='tt_network_id' /><Value Type='Text'>clu_noe</Value></Eq></Where></Query></View>"; 
    var itemts= list.GetItems(query);
    _clientContext.Load(list);
    _clientContext.Load(itemts);
    _clientContext.ExecuteQuery();
        Console.Write(itemts.Count().ToString());
    }
    catch (Exception e)
    {

        Console.Write(e.Message);
    }

Find out more here http://msdn.microsoft.com/en-us/library/ee857094.aspx#SP2010ClientOM_Object_Identity

2
  • I got the same Exception!
    – hello B
    Apr 26, 2012 at 9:00
  • Can you please remove the items.Count() query, and instead use foreach loop, and see if that will produce the same error message ?
    – Michael
    Apr 26, 2012 at 9:19
1

Remove the () after itemts.Count

static void Main(string[] args) 
{ 
    try 
    { 
      ClientContext _clientContext = new ClientContext("http://spweb.reply.eu/sites/RC1/1003/"); 
      _clientContext.Credentials = new NetworkCredential("mossuser","Reply.96","replynet"); 
      SP.List list = _clientContext.Web.Lists.GetByTitle("Contenuti"); 
      CamlQuery query = new SP.CamlQuery(); 
      query.ViewXml = "@<View><Query><Where><Eq><FieldRef Name='tt_network_id' /><Value Type='Text'>clu_noe</Value></Eq></Where></Query></View>";  
      var itemts= list.GetItems(query); 
      _clientContext.Load(itemts); 
      _clientContext.ExecuteQuery(); 
      Console.Write(itemts.Count.ToString()); 
    } 
    catch (Exception e) 
    { 
      Console.Write(e.Message); 
    } 
} 
0
static void Main(string[] args)
    {
        try
        {
            ClientContext _clientContext = new ClientContext("http://spweb.reply.eu/sites/RC1/1003/");
        _clientContext.Credentials = new NetworkCredential("mossuser","Reply.96","replynet");
        SP.List list = _clientContext.Web.Lists.GetByTitle("Contenuti");
        _clientContext.Load(list);
        _clientContext.ExecuteQuery();
        CamlQuery query = new SP.CamlQuery();
        query.ViewXml = "@<View><Query><Where><Eq><FieldRef Name='tt_network_id' /><Value Type='Text'>clu_noe</Value></Eq></Where></Query></View>"; 
        var itemts= list.GetItems(query);
        _clientContext.Load(itemts);
        _clientContext.ExecuteQuery();
            Console.Write(itemts.Count().ToString());
        }
        catch (Exception e)
        {

            Console.Write(e.Message);
        }


        }

You have to load each item before doing any operation on them. thus only you can get it be executed. You forget to load _clientContext.Load(list); _clientContext.ExecuteQuery();
Thank you..

Your Answer

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

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