none
check if the folder is present in sharepoint library

    Question

  • Hi,

        how can i check if the folder is present in document library using JSOM or REST

    thanks in advance

    Friday, November 9, 2018 11:31 AM

Answers

  • In Client Side Object Model for SharePoint Online, there is exception when a file/folder is not found.

    Below is a sample REST code invocation in PowerShell. You can run the RestCrud.ps1 from here https://github.com/Thangeswari/SPComics/blob/master/RESTcrud.ps1

    Call the REST api using following code:

    $serverRelativeUrl="/sites/dev/Shared Documents"
    $folderName="Thangu1"
    $restUrl="/_api/web/GetFolderByServerRelativeUrl('"+$serverRelativeUrl+"/"+$folderName+"')"
    $found=$false
    try{
    $folder=Read-SPObject -targetSite $targetSite -User $User -restUrl $restUrl
    if($folder.d.Exists){
    $found=$true
    Write-Host "Found Folder" $folder.d.Name
    }
    }
    catch{
    Write-Host $_.Exception.Message $_.Exception.GetType().FullName
    } 
    if($found -eq $false)
    {
    Write-Host "Could not find Folder" $folder.d.Name
    }

    Sample code from Javascript:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script type="text/ecmascript" language="ecmascript">var site;
    var items;var item;var info;
    var clientContext;
    function getFolder(){
    var siteUrl=_spPageContextInfo.webAbsoluteUrl;
    var serverUrl=_spPageContextInfo.webServerRelativeUrl;
    var folderName=document.getElementById("txtFolderName").value;
    var docName=document.getElementById("docFolderName").value;
    var restUrl=siteUrl+"/_api/web/GetFolderByServerRelativeUrl('"+serverUrl+"/"+docName+"/"+folderName+"')";
    alert(_spPageContextInfo.webServerRelativeUrl);
    jQuery.ajax({
            url: restUrl,
            type: "GET",        
            headers: { 
                "accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose"
            },
            success: onQuerySucceeded,
            error: onQueryFailed
    });
    
    }
    function onQuerySucceeded(data) {
    console.log(data.d.Name);
    info = data.d.Name + ' FOUND.';
    alert(info);
    document.getElementById("getFolderData").innerHTML="<b>"+info+"</b>";
    console.log(info);
    }
    function onQueryFailed(error) {
    alert('Request failed. ' + JSON.stringify(error) + '\n');
    document.getElementById("getFolderData").innerHTML="<b>Folder Not Found</b>";
    }
    </script>
    <input id='Button1' type='button' value='Get Folder Details' onclick='getFolder();' >
    Folder Name:<input id='txtFolderName' type='text' >
    Document Library Name:<input id='docFolderName' type='text'>
    <div id="getFolderData"></div>

    Friday, November 9, 2018 5:47 PM

All replies

  • You can use REST API. See this thread:

    https://sharepoint.stackexchange.com/questions/215570/rest-api-to-check-if-folder-exist-inside-a-folder-in-document-library


    Please remember to mark as answer if you found this to be helpful. Andy Wessendorf | Belcan

    Friday, November 9, 2018 2:19 PM
  • In Client Side Object Model for SharePoint Online, there is exception when a file/folder is not found.

    Below is a sample REST code invocation in PowerShell. You can run the RestCrud.ps1 from here https://github.com/Thangeswari/SPComics/blob/master/RESTcrud.ps1

    Call the REST api using following code:

    $serverRelativeUrl="/sites/dev/Shared Documents"
    $folderName="Thangu1"
    $restUrl="/_api/web/GetFolderByServerRelativeUrl('"+$serverRelativeUrl+"/"+$folderName+"')"
    $found=$false
    try{
    $folder=Read-SPObject -targetSite $targetSite -User $User -restUrl $restUrl
    if($folder.d.Exists){
    $found=$true
    Write-Host "Found Folder" $folder.d.Name
    }
    }
    catch{
    Write-Host $_.Exception.Message $_.Exception.GetType().FullName
    } 
    if($found -eq $false)
    {
    Write-Host "Could not find Folder" $folder.d.Name
    }

    Sample code from Javascript:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script type="text/ecmascript" language="ecmascript">var site;
    var items;var item;var info;
    var clientContext;
    function getFolder(){
    var siteUrl=_spPageContextInfo.webAbsoluteUrl;
    var serverUrl=_spPageContextInfo.webServerRelativeUrl;
    var folderName=document.getElementById("txtFolderName").value;
    var docName=document.getElementById("docFolderName").value;
    var restUrl=siteUrl+"/_api/web/GetFolderByServerRelativeUrl('"+serverUrl+"/"+docName+"/"+folderName+"')";
    alert(_spPageContextInfo.webServerRelativeUrl);
    jQuery.ajax({
            url: restUrl,
            type: "GET",        
            headers: { 
                "accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose"
            },
            success: onQuerySucceeded,
            error: onQueryFailed
    });
    
    }
    function onQuerySucceeded(data) {
    console.log(data.d.Name);
    info = data.d.Name + ' FOUND.';
    alert(info);
    document.getElementById("getFolderData").innerHTML="<b>"+info+"</b>";
    console.log(info);
    }
    function onQueryFailed(error) {
    alert('Request failed. ' + JSON.stringify(error) + '\n');
    document.getElementById("getFolderData").innerHTML="<b>Folder Not Found</b>";
    }
    </script>
    <input id='Button1' type='button' value='Get Folder Details' onclick='getFolder();' >
    Folder Name:<input id='txtFolderName' type='text' >
    Document Library Name:<input id='docFolderName' type='text'>
    <div id="getFolderData"></div>

    Friday, November 9, 2018 5:47 PM
  • Thanks Thangu
    Monday, November 12, 2018 4:06 PM