How to check if a path exists in Sharepoint and create it if it doesn't on Sharepoint using C#?

Puneeth, C 81 Reputation points
2022-06-30T19:41:41.877+00:00

So, I have a bunch of "paths to create" on a list (some paths may exist partially. For ex: A/B might already be there. But I might have to create A/B/C). I need to check if the path already exists and if it doesn't create it using CSOM on Sharepoint online.
Can you please help?
Thanks.

Microsoft 365 and Office SharePoint Development
Microsoft 365 and Office SharePoint For business Windows
{count} votes

Accepted answer
  1. Tong Zhang_MSFT 9,251 Reputation points
    2022-07-05T02:33:16.747+00:00

    Hi @Puneeth, C ,

    I'm glad to hear you solve the problem ,if you have any issue about SharePoint, you are welcome to raise a ticket in this forum.

    By the way, since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others." and according to the scenario introduced here: Answering your own questions on Microsoft Q&A, I would make a brief summary of this thread:

    [How to check if a path exists in Sharepoint and create it if it doesn't on Sharepoint using C#?]

    Issue Symptom:

    I have a bunch of "paths to create" on a list (some paths may exist partially. For ex: A/B might already be there. But I might have to create A/B/C). I need to check if the path already exists and if it doesn't create it using CSOM on Sharepoint online.

    Solutions:

    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;  
     }  
    

    You could click the "Accept Answer" button for this summary to close this thread, and this can make it easier for other community member's to see the useful information when reading this thread. Thanks for your understanding!


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Tong Zhang_MSFT 9,251 Reputation points
    2022-07-04T09:07:38.99+00:00

    Hi @Puneeth, C ,

    Based on my research and testing, I have not yet found a way to check if the path exists in sharepoint using CSOM. As it is to check and create folders, you can refer to this document to check if a SharePoint folder exists using CSOM.

    As a workaround, I also recommend you to use powershell to check if folder exists. Please refer to the following code:

    #Parameters  
    $SiteURL = "https://crescent.sharepoint.com/sites/Operations"  
    $FolderURL = "/sites/Operations/Shared Documents/Classifieds" #Server Relative URL  
       
    #Connect to PnP Online  
    Connect-PnPOnline -Url $SiteURL -Interactive  
       
    #Try to Get the Folder  
    $Folder = Get-PnPFolder -Url $FolderURL -ErrorAction SilentlyContinue  
       
    #sharepoint online powershell To check if folder exists  
    If($Folder -ne $null)  
    {  
        Write-Host -f Green "Folder exists!"  
    }  
    Else  
    {  
        Write-Host -f Yellow "Folder does not exists!"  
    }  
    

    Hope it can help you. Thanks for your understanding.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



  2. Puneeth, C 81 Reputation points
    2022-07-04T13:19:32.637+00:00
    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;  
    }  
    

    The code above works perfectly for me.
    Thanks

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.