I am trying to upload a document from an ASP.NEt website (not an App) to a SharePoint library and using CSOM. This is an on premise sharepoint 2013, not office 365. Below is the code to upload the document. This works great if we run from visual studio. However, gets an access denied when we run on website. It might have something to do with the authentication. enter code here
private void UploadFile()
{
CamlQuery Query = new CamlQuery();
Uri x = new Uri("http://testportal.com/sites/test");
ClientContext ctx = new ClientContext(x);
ctx.Credentials = System.Net.CredentialCache.DefaultCredentials;
ctx.ExecuteQuery();
var file = "C:\\Users\\spalakollu\\Documents\\Training.vsd";
List<string> all = new List<string>();
using (var fs = new FileStream(file, FileMode.Open))
{
var fi = new FileInfo(file);
string ListToUtilze = string.Empty;
var list = ctx.Web.Lists.GetByTitle("Documents");
ctx.Load(list.RootFolder);
ctx.ExecuteQuery();
var fileUrl = string.Format("{0}/{1}", list.RootFolder.ServerRelativeUrl, fi.Name);
Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, fileUrl, fs, true);
}
}