简体   繁体   中英

Server Version, Site, Web in ClientContext of SharePoint throws error

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM