Target .NET4.7.2, Windows Forms App template
Environment:
Here is example of ListItemCreationInformation and ListItem:
And here is an example making use of the Folder.Add methodology:
So what I am trying to determine is WHAT methodology should I be using to create these folders.
Why do I see some examples using Folders.Add and other examples making use of the ListItemCreationInformation and ListItem?
In addition there is the issue of whether the folder or (later a file) exists prior to creating or uploading. At this point I know if I try to add a folder that already exists it throws an exception and stops you.
So at this point I seem to have to check if the folder exists first for every folder which is more calls back to share point making this all a singular folder creation event and not many.
So a follow up question is if there is a way to see if a folder exists without throwing an exception. The current check for folder exists that I am using is like this:
Which basically will throw an exception and is trapped, but you have to do this for every folder test. It is hard to believe there isn't a way to do this without throwing exceptions if you wanted to send up 100 folders in one shot.
Here is something I am playing with and will see if will work, but thought I would get some feedback while testing. In this test using the Folders.Add methodology. But they key part is loading all of the folders and then checking to see if it exists. Not sure this will work yet as I pulled this from another site.
Environment:
- Windows 10
- Visual Studio 2019 16.9.4
- Create Folders in Root Library and then create subfolders a couple of levels deep.
- We have added Microsoft.SharePoint.Online.CSOM (16.1.2111.12000) Nuget Package
- We have added Microsoft .Identity.Client (4.29.0) Nuget Package
- I have seen multiple ways to potentially add folders and check to see if folder exists and so forth. I am looking to find out what is the best or recommended way of doing this.
Here is example of ListItemCreationInformation and ListItem:
C#:
public static bool CreateFolder(ClientContext context, List list, string folderName)
{
string url = list.RootFolder.ServerRelativeUrl + "/" + folderName;
try
{
ListItemCreationInformation newItemInfo = new ListItemCreationInformation();
newItemInfo.UnderlyingObjectType = FileSystemObjectType.Folder;
newItemInfo.LeafName = folderName;
ListItem newListItem = list.AddItem(newItemInfo);
newListItem["Title"] = folderName;
newListItem.Update();
context.ExecuteQuery();
return true;
}
catch (ServerUnauthorizedAccessException uae)
{
Trace.WriteLine($"You are not allowed to access this folder: " + uae.Message);
throw;
}
catch (Exception ex)
{
Trace.WriteLine($"Could create folder {url}" + ex.Message);
return false;
}
}
And here is an example making use of the Folder.Add methodology:
C#:
var list = ctx.Web.Lists.GetByTitle(listName);
var folder = list.RootFolder;
ctx.Load(folder);
ctx.ExecuteQuery();
folder = folder.Folders.Add(folderName);
ctx.ExecuteQuery();
So what I am trying to determine is WHAT methodology should I be using to create these folders.
Why do I see some examples using Folders.Add and other examples making use of the ListItemCreationInformation and ListItem?
In addition there is the issue of whether the folder or (later a file) exists prior to creating or uploading. At this point I know if I try to add a folder that already exists it throws an exception and stops you.
So at this point I seem to have to check if the folder exists first for every folder which is more calls back to share point making this all a singular folder creation event and not many.
So a follow up question is if there is a way to see if a folder exists without throwing an exception. The current check for folder exists that I am using is like this:
C#:
private static bool FolderExists(ClientContext context, string url)
{
var folder = context.Web.GetFolderByServerRelativeUrl(url);
context.Load(folder, f => f.Exists);
try
{
context.ExecuteQuery();
if (folder.Exists)
{
return true;
}
return false;
}
catch (ServerUnauthorizedAccessException uae)
{
Trace.WriteLine($"You are not allowed to access this folder: " + uae.Message);
throw;
}
catch (Exception ex)
{
Trace.WriteLine($"Could not find folder {url}" + ex.Message);
return false;
}
}
Which basically will throw an exception and is trapped, but you have to do this for every folder test. It is hard to believe there isn't a way to do this without throwing exceptions if you wanted to send up 100 folders in one shot.
Here is something I am playing with and will see if will work, but thought I would get some feedback while testing. In this test using the Folders.Add methodology. But they key part is loading all of the folders and then checking to see if it exists. Not sure this will work yet as I pulled this from another site.
C#:
List list = context.Web.Lists.GetByTitle("Documents");
FolderCollection folders = list.RootFolder.Folders;
context.Load(folders);
context.ExecuteQuery();
var folderExists = folders.Any(x => x.Name == "SOMEFOLDERNAME");
if (!folderExists)
{
//create folder
Folder newFolder= folders.Add("SOMEFOLDERNAME");
context.Load(newFolder);
context.ExecuteQuery();
}