var onlineCredentials = new SharePointOnlineCredentials(ServiceUserName,securePassword);
var context = new ClientContext(clientUrl);
context.Credentials = onlineCredentials;
ar cc = GetAllFiles(sourceDir); //- get the files
int errorLog = 0;
foreach (string path in cc)
{
var filepath = "";
var sub_folder = new FileInfo(path).Directory.FullName;
filepath = sub_folder.Split(new[] { sourceDir }, StringSplitOptions.None)[1];
sub_folder = sub_folder.Split(new[] { sourceDir }, StringSplitOptions.None)[1].Replace("\\", "//");
try
{
var folder = CreateFolder(context.Web, listname, sub_folder);//--- Create folder
Web web = context.Web;
FileCreationInformation newFile = new FileCreationInformation();
newFile.Content = System.IO.File.ReadAllBytes(path);
newFile.Url = filepath + "\\" + Path.GetFileName(path);
List docs = web.Lists.GetByTitle(listname);
Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);
context.Load(uploadFile);
context.ExecuteQuery();
}
catch (Exception e)
{
System.IO.File.AppendAllText(ErrorLogPath, e.Message);
}
}
I am trying to upload my local files to SharePoint using c#. If folder is not in
Document Libraray , its creating the folders and sub-folders. But, It's not uploading the files.
In Exception, getting the following error:
Microsoft.Sharepoint.Client.ServerException : {File not Found}
Note:
- User having Full Admin Permissions.
If I Change user permission from Admin To Normal, getting error
Access is denied
But folders were created.
- In VS,"Platform target:" is "Any CPU".
Create Folder Function:
public static Folder CreateFolder(Web web, string listTitle, string fullFolderPath)
{
if (string.IsNullOrEmpty(fullFolderPath))
throw new ArgumentNullException("fullFolderPath");
var list = web.Lists.GetByTitle(listTitle);
return CreateFolderInternal(web, list.RootFolder, fullFolderPath);
}
private static Folder CreateFolderInternal(Web web, Folder parentFolder, string fullFolderPath)
{
var folderUrls = fullFolderPath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
string folderUrl = folderUrls[0];
var curFolder = parentFolder.Folders.Add(folderUrl);
web.Context.Load(curFolder);
web.Context.ExecuteQuery();
if (folderUrls.Length > 1)
{
var folderPath = string.Join("/", folderUrls, 1, folderUrls.Length - 1);
return CreateFolderInternal(web, curFolder, folderPath);
}
return curFolder;
}
It's creating Folder, no issue in creating folder. Only i couldn't upload a file.
CreateFolder
method