How do we access SMB Shared Folder or FTP Location from SharePoint - JS. Is GetFolderByServerRelativeUrl supported for SMB Shared Folder Access?

Anas Patla Aboobacker 1 Reputation point
2022-10-24T11:27:43.077+00:00

I have tried 2 to 3 ways to Access & Read SMB Shared Folder from SharePoint. None seems to work.

Is GetFolderByServerRelativeUrl supported for SMB Shared Folder Access? Inorder to access SMB Shared Folder we will need to pass the Absolute URL, however the method GetFolderByServerRelativeUrl accepts relative URL. Is there any method where i can pass smb shared folder absolute path to access and read the file names within.

Code1:

var files;  
var filepath;  
var clientcontext = SP.ClientContext.get_current();  
var oWeb = clientcontext.get_web();  
var oFolder = oWeb.getFolderByServerRelativeUrl('\\SMBServerHostName\Folder1\SubFolder1\SubFolder2');  
files = oFolder.get_files();  
 var filecount = files.get_count();      
if(filecount  > 0)  
{  
for(let i=0;i< filecount ;i++)  
{   
filepath = files[i].get_path();  
console.log(filepath);  
}   
}  

Also tried with rest api but no luck:

Code2: [it doesn't fetch value in data]

        $.ajax({              
        url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/getfolderbyserverrelativeurl('\\SMBServerHostName\Folder1\SubFolder1\SubFolder2')/Files",  
        type: "GET",  
        contentType: "application/json;odata=verbose",  
        headers: {  
            "Accept": "application/json;odata=verbose",  
            "X-RequestDigest": $("#__REQUESTDIGEST").val()  
        },  
        success: function (data) {                           
             for(var i=0;i<data.d.results.length;i++)  
             {  
               console.log(data.d.results[i].Name);  
               console.log(data.d.results[i].ServerRelativeUrl);  
             }                     
        },  
        error: function (data) {  
            alert("Error Occured in SharedFolder Access API call: "+JSON.stringify(data) +" Url: "+url);  
            }  
        });  

Code3: [throws error related to accessToken, how to set accessToken]

     var accessToken = window.localStorage.getItem(LOCAL_TOKEN_KEY);  
     $.ajax({  
     url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetFolderByServerRelativeUrl('\\SMBServerHostName\Folder1\SubFolder1\SubFolder2')/?$expand=Folders,Files",  
    method: "GET",  
    headers: { Authorization: "Bearer " + accessToken,  
    Accept: "application/json;odata=verbose" }  
    });  
SharePoint Server
SharePoint Server
A family of Microsoft on-premises document management and storage systems.
2,347 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,988 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,613 questions
{count} votes

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.