Handling SharePoint Folder Not Found Exception
C#
1
Posts
1
Posters
0
Views
1
Watching
-
I want to check to see if a folder exists on SharePoint. There doesn't seem to be a native SharePoint API call for this. So, I wrote the following code, which I got from here[^].
public async Task FolderExists(string folderName)
{
bool exists = false;try { var fullPath = \_clientContext.Web.ServerRelativeUrl + "/Shared%20Documents/" + folderName; Folder folder = \_clientContext.Web.GetFolderByServerRelativeUrl(fullPath); \_clientContext.Load(folder); await \_clientContext.ExecuteQueryAsync(); exists = folder.Exists; } catch (Exception e) { if (e.InnerException.Message != "File Not Found") { throw e; } } return exists;
}
Instead of a FileNotFoundException, it throws an exception with the message of 'File Not Found'. I don't see any other way to check for a folder without actually checking the message when an exception is thrown. This seems like a hack. Anyone have a better way?
In theory, theory and practice are the same. But in practice, they never are.” If it's not broken, fix it until it is. Everything makes sense in someone's mind.