Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Handling SharePoint Folder Not Found Exception

Handling SharePoint Folder Not Found Exception

Scheduled Pinned Locked Moved C#
c++phpsharepointcomjson
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups