Reaching a list from silverlight to sharepoint 2010

Although I have tried everything ,almost every code to get the contents of a list in sharepoint 2010 ,Why am I not able to get the files... below is my code:

            ClientContext ctx = new ClientContext("http://sharepoint-one");
             Site site = ctx.Site;
             Web web = site.RootWeb;

             ListCollection lists = web.Lists;

             List taskList = lists.GetByTitle("Shared Documents");

             CamlQuery query =CamlQuery.CreateAllItemsQuery();

             ListItemCollection listItems = taskList.GetItems(query);


I have a silverlight application this needs to get into a list in sharepoint and get the files inside... but somehow the code above gives the exception CollectionNotInitializedException in the first line even though the exception i can continue with F10 in debug mode but then it gives error in executequery saying that  :

The method or property that is called may block the UI thread and it is not allowed. Please use background thread to invoke the method or property, for example, using System.Threading.ThreadPool.QueueUserWorkItem method to invoke the method or property.

if I use ExecuteQueryAsync(Onsuccess,Onfailure); then it somehow does not go to onsuccess  or onfailure methods and continues like nothing happens and i cannot get list specific information...

like the count of the items inside the list with

             int count = 0;

             foreach (ListItem a in listItems)

when I try to get info about the list with above code I get the error msg below...

The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
Who is Participating?
quihongConnect With a Mentor Commented:
Here is some code from a workshop I took on SP2010 and Client Object Model. Hopefully it will help you.

using ClientOM = Microsoft.SharePoint.Client;

 using (ClientOM.ClientContext ctx = new ClientOM.ClientContext(textBox1.Text))
        ClientOM.Web site = ctx.Web;
            x => x.Lists.Where(l => l.Title != null));
        foreach (ClientOM.List list in site.Lists)
Are you using the Client Object Model? For Silverlight I believe you need to.
fatihdiAuthor Commented:
yes i am using client object model thats right
Thanks a lot quihong. That resolved the issue for me. Looks like you have to load the object and that did the trick for me.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.