I just started learning CSOM, I want to get all lists from specific site provided by user, and display them in WinForms app. Here is the code of acquiring lists:

    class ListCheckRepo : IListCheckRepo
    public ListCollection GetLists(string url)
        ClientContext clientcontext = new ClientContext(url);
        Web web = clientcontext.Web;
        ListCollection listColl = web.Lists;

        return listColl;

The problem is an error "Invalid file name" at ExecuteQuery

The file name you specified could not be used.  
It may be the name of an existing file or directory, 
or you may not have permission to access the file.

I Have literally no idea what it means, which file or directory is it talking about. The URL Im providing is a working one, also I was working with msdn documentation while writing it.

2 Answers 2


You can use the below sample code to get the list collection in a site and display them in a console. Change the display code accordingly to use with WinForms.

        // ClientContext - Get the context for the SharePoint Site      
        ClientContext clientContext = new 

        // Get the SharePoint web  
        Web web = clientContext.Web;  

        // Get the SharePoint list collection for the web   
        ListCollection listColl = web.Lists;  

        // Retrieve the list collection properties   

        // Execute the query to the server.   

        // Loop through all the list   
        foreach(List list in listColl)    
            // Display the list title and ID    
            Console.WriteLine("List Name: " + list.Title + "; ID: " + 
  • It's exactly the same code as I posted above, but as you said, in console.
    – Wolwgang
    Aug 4, 2017 at 9:40

Found it:

One of the lists in site was corrupted, and therefore unreadable. Deleting broken list fixed the issue.

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.