Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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);
        }


        }
share|improve this question

3 Answers 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

share|improve this answer
    
I got the same Exception! –  Bruno Ferreira Apr 26 '12 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 '12 at 9:19
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..

share|improve this answer

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); 
    } 
} 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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