I have an asp.net web application that has a user select three files to upload. It then creates a random 6 digit folder on a SharePoint Online site and copy the three files into it.
It runs correctly locally in my VS2015, but when I run it in IIS8 on a Windows 2012 server, only the folder is created. I'm not sure if it's something in the code or an IIS setting.
Any tips/suggestions on how I could get this to work?
protected void SaveFileToSharePoint(string fileName)
{
try
{
using (var context = new ClientContext(Url))
{
var passWord = new SecureString();
foreach (var c in Password) passWord.AppendChar(c);
context.Credentials = new SharePointOnlineCredentials(UserName, passWord);
var web = context.Web;
var newFile = new FileCreationInformation { Content = System.IO.File.ReadAllBytes(fileName), Url = Path.GetFileName(fileName) };
var docs = web.Lists.GetByTitle("Documents");
Microsoft.SharePoint.Client.Folder folder = docs.RootFolder.Folders.GetByUrl(NewFolder);
Microsoft.SharePoint.Client.File uploadFile = folder.Files.Add(newFile);
context.Load(docs);
context.Load(uploadFile);
context.ExecuteQuery();
}
}
catch (Exception ex)
{
}
}
protected void Button1_OnClick(object Source, EventArgs e)
{
try
{
if (getFile1.PostedFile != null && getFile2.PostedFile != null && getFile3.PostedFile != null)
{
using (var context = new ClientContext(Url))
{
Random generator = new Random();
NewFolder = generator.Next(0, 999999).ToString("D6");
CreateFolder(NewFolder);
SaveFileToSharePoint(getFile1.Value.ToString());
SaveFileToSharePoint(getFile2.Value.ToString());
SaveFileToSharePoint(getFile3.Value.ToString());
Session["num"] = NewFolder.ToString();
Response.Redirect("ThankYouPage.aspx");
}
}
else
{
}
}
catch (Exception ex)
{
}
}
protected void CreateFolder(string FolderName)
{
using (var clientContext = new ClientContext(Url))
{
var passWord = new SecureString();
foreach (var c in Password) passWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials(UserName, passWord);
var web = clientContext.Web;
var list = clientContext.Web.Lists.GetByTitle("Documents");
list.EnableFolderCreation = true;
clientContext.Load(list);
clientContext.Load(list.RootFolder);
clientContext.Load(list.RootFolder.Folders);
clientContext.ExecuteQuery();
var folderCollection = list.RootFolder.Folders;
foreach (var folder in folderCollection)
{
if (folder.Name == FolderName)
{
clientContext.Load(folder.Files);
clientContext.ExecuteQuery();
}
else
{
var itemCreateInfo = new ListItemCreationInformation
{
UnderlyingObjectType = FileSystemObjectType.Folder,
LeafName = FolderName
};
var newItem = list.AddItem(itemCreateInfo);
newItem["Title"] = FolderName;
newItem.Update();
clientContext.ExecuteQuery();
break;
}
}
}
}