Hi All,
We have a requirement to import an excel file from SharePoint to D365 Finops. The Excel file needs to be read as is from SharePoint and requires further updates and validations before the data in the excel file is inserted to the data entity through code.
So, the following method is created to read the file from share point and convert it to a Stream.
The logic i am using to read the file from SharePoint is below , The line fileResults = SharePointHelper::GetFiles(proxy, FolderPath, '') returns a result but the line file = SharePointHelper::GetFileContents(proxy, uri); returns a null value for the File and hence the line System.IO.Stream fileStream = file.content fails.
The service account used (ExternalId) to read the file from SharePoint has the site level permissions and no MFA configured.
Could you please let me know If i am missing something or Is there a better way to read the SharePoint excel file in this scenario.
public System.IO.MemoryStream getFileStreamFromSharepoint()
{
#file
#define.xlsx(".xlsx")
#define.https("https://")
#define.decodeUrl("/_api/Web/GetFileByServerRelativePath(decodedurl='/")
ISharePointProxy proxy;
System.Uri uri;
System.Uri NavigateUri;
FileResults fileResults;
FileContents file;
fileName = filename + #xlsx;
uri = new System.Uri(#https + host + #decodeUrl + site + "/" + folderPath + "/" + fileName + "')");
proxy = (proxy && proxy.Config.TargetHost == host && proxy.Config.Site == site) ?
proxy :
SharePointHelper::createProxy(host, site, externalId);
fileResults = SharePointHelper::GetFiles(proxy, FolderPath, ''); // The file result is returned successfully
file = SharePointHelper::GetFileContents(proxy, uri); //This line returns a null value and no exception or error
if(file)
{
System.IO.Stream fileStream = file.content;
System.IO.MemoryStream ms = new System.IO.MemoryStream();
fileStream.copyTo(ms);
return ms;
}
else
{
return null;
}
Thank you.