0

I'm new to sharepoint. Im trying to upload a file and add metadata to the file. Below is my code. I can see some internal exceptions are their while declaring ClientContext

Exception: clientContext.ServerVersion threw an exception of type PropertyOrFieldNotInitializedException

The same is happening for Site, Web.

using (ClientContext clientContext = new ClientContext(SPURL))
{
    //ClientContext clientContext = new ClientContext(SPURL);
    clientContext.Credentials = new System.Net.NetworkCredential(userName, password, domain);

    Web web = clientContext.Web;
    FileCreationInformation newFile = new FileCreationInformation();
    newFile.Content = System.IO.File.ReadAllBytes(PPTPath);
    newFile.Url = "F:\\log.txt";
    List docs = web.Lists.GetByTitle("Test");
    Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);
    clientContext.ExecuteQuery();

    using (FileStream fileStream =
                new FileStream(PPTPath, FileMode.Open))
        ClientOM.File.SaveBinaryDirect(clientContext,
        DocumentRepository + PPTfilename, fileStream, true);   
}
Orkhan Alikhanov
  • 9,122
  • 3
  • 39
  • 60
John
  • 9
  • 7

1 Answers1

0

the CSOM libraries expect you to tell them what you want to query from the SharePoint environment upon calling ExecuteQuery(). Therefor you need to tell the clientcontext object what you want to load. For instance: Web web = clientContext.Web; should be followed by clientContext.Load(web);

This is a simple example. For uploading files your best bet is making use of the File.SaveBinaryDirect function. An example on how to use it can be found elsewhere on stackoverflow.

Community
  • 1
  • 1
Nathan Swannet
  • 220
  • 3
  • 12