How do we access SMB Shared Folder or FTP Location from SharePoint - JS. Is GetFolderByServerRelativeUrl supported for SMB Shared Folder Access?
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" }
});