0

I need to retrieve an XML file from a document library on a SharePoint 2010. But to do this I first need to be able to get the list. I am using the following code but all I get in return is a whole bunch of exceptions:

using (ClientContext clientContext = new ClientContext("http://link"))
{
    clientContext.Credentials = credentials;

    List sharedDocumentsList = clientContext.Web.Lists.GetByTitle("Title");
    clientContext.Load(sharedDocumentsList);
    clientContext.ExecuteQuery();
}

The exceptions include:

'clientContext.Web.Lists.Count' threw an exception of type 'Microsoft.SharePoint.Client.CollectionNotInitializedException'

A whole bunch of these: 'Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException'

So I'm guessing I'm not getting the list and therefore the fields are not being initialized, but why am I not getting the list? I know the link and title i use are the correct ones since I can access the list perfectly with the credentials i supply on my own.

EDIT

Added new code as solution is now updated:

using (ClientContext clientContext = new ClientContext("http://link"))
{
    clientContext.Credentials = credentials;
    var web = clientContext.Web;
    clientContext.Load(web, w => w.Lists);
    clientContext.ExecuteQuery();

    List sharedDocumentsList = web.Lists.GetByTitle("Title");
    clientContext.Load(sharedDocumentsList);
    clientContext.ExecuteQuery();
}
0

You can get more info from below sites for SharePoint list/library operations using CSOM.

UPDATE

Try this:

using (ClientContext clientContext = new ClientContext("http://link"))
{
    clientContext.Credentials = credentials;
    var web = clientContext.Web;
    clientContext.Load(web, w => w.Lists);
    clientContext.ExecuteQuery();

    List sharedDocumentsList = web.Lists.GetByTitle("Title");
    clientContext.Load(sharedDocumentsList);
    clientContext.ExecuteQuery();
}
  • I've already tried most all of those things but they don't matter if I can't get the list through the code that pretty much ALL of them supply as standard, like the clientContext.Web.Lists.GetByTitle. – Ghost Jun 7 '16 at 8:51
  • Load the web object before retrieving the list – Dikesh Gandhi Jun 7 '16 at 8:54
  • Thanks for the suggestion but it still returns 'clientContext.Web.Lists.Count' threw an exception of type 'Microsoft.SharePoint.Client.CollectionNotInitializedException'. I added: Web web = clientContext.Web; clientContext.Load(web); clientContext.ExecuteQuery(); Before trying to retrieve the list. – Ghost Jun 7 '16 at 9:01
  • I tried your suggested code but still no luck. :( I'm starting to think maybe it could be something serverside? No clue what though since I have succesfully used the same sample code to retrieve a normal List, not a Document Library. – Ghost Jun 7 '16 at 9:08
  • Exactly From which line do you get an exception? – Dikesh Gandhi Jun 7 '16 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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