web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Unable to read pdf file from sharepoint in X++

(3) ShareShare
ReportReport
Posted on by 98
public static System.IO.Stream generateSPResponseStream(str _url)
{
    //https://avanade.sharepoint.com/sites/ISG-Intersurgical/Shared%20Documents/Design/Designs/Technical%20Design%20Documents/Post%20Development/CR%20137574%20-%20TDD%20-%20Nice%20Label%20Integration%20Entity.docx
    str _urlPrefix = "https://";
    str _spServer= "XXXX.sharepoint.com";
    str _siteName = "sites/XXXX";
    str _documentFolder = "XXXXX";
    str _fileName= "XXXX";
    str _fileExt= ".pdf";
    System.IO.Stream          Stream;
    System.Net.WebException         webEx;
    //DocuType fileType = DocuType::find("");
    System.UriBuilder builder = new System.UriBuilder('XXXX.sharepoint.com');
    str extId = xUserInfo::getCurrentUserExternalId();
    Microsoft.Dynamics.AX.Framework.FileManagement.SharePointDocumentStorageProvider provider;
    Microsoft.Dynamics.AX.Framework.FileManagement.DocumentContents documentContents;
    Microsoft.Dynamics.AX.Framework.FileManagement.DocumentLocation location = new Microsoft.Dynamics.AX.Framework.FileManagement.DocumentLocation();
    provider = new Microsoft.Dynamics.AX.Framework.FileManagement.SharePointDocumentStorageProvider(
                                                                    _spServer, _siteName, _documentFolder, extId);
    provider.ProviderId = DocuStorageProviderType::SharePoint;
    str url =   _urlPrefix + _spServer + "/" + _siteName + "/" + _documentFolder
                 + "/" + (_fileName + _fileExt);
    info(strFmt("URL : %1",URL));
    System.Uri uri = new System.Uri(url);
    System.Uri uri1 = new System.Uri(_urlPrefix + _spServer + "/" + _siteName
                                    + "/_api/Web/GetFileByServerRelativePath(decodedurl='/"
                                    + _siteName + "/" + _documentFolder + "/" + (_fileName + _fileExt) + "')");
    //System.Uri uri1 = new System.Uri("https://intersurgicalglobal.sharepoint.com/sites/Products/_api/WebGetFileByServerRelativePath(decodedurl='/sites/Products/Shared Documents/Specifications/XXXX.pdf')");
              
    location.NavigationUri  = uri;
    location.AccessUri = uri1;
            
    documentContents = provider.GetFile(location);
    try
    {
        if (documentContents)
        {
            Stream = documentContents.Content;
        }
        else
        {
            Error(strFmt("Error"));
        }
    }
    catch(webEx)
    {
        Error(webEx.Message);
    }
    return Stream;
}
 
 
 
While trying above code unable to fetch the file as code is dropping in else block with error message. Is this because of any access missing ? 
I have the same question (0)
  • Martin Dráb Profile Picture
    237,976 Most Valuable Professional on at
    Are you saying that no exception provider.GetFile() returns null (it doesn't raise any exception)?
     
    I've tried to refactor your code to make it easier to read; I also threw away code that wasn't used at all or wasn't relevant to this case.
    using Microsoft.Dynamics.AX.Framework.FileManagement;
    
    str spServer = 'XXXX.sharepoint.com';
    str siteName = 'sites/XXXX';
    str documentFolder = 'XXXXX';
    
    public test()
    {
        str extId = xUserInfo::getCurrentUserExternalId();
        SharePointDocumentStorageProvider provider = new SharePointDocumentStorageProvider(spServer, siteName, documentFolder, extId);
        provider.ProviderId = DocuStorageProviderType::SharePoint;
        
        DocumentLocation location = this.buildDocLocation();
        DocumentContents documentContents = provider.GetFile(location);
        
        info(documentContents ? "Success" : "No contents");
    }
    
    private DocumentLocation buildDocLocation()
    {
        str urlPrefix = 'https://';
        str fileName = 'XXXX' + '.pdf'
        
        str siteBaseUrl = strFmt('%1%2/%3', urlPrefix, spServer, siteName);
        
        str navigationUrl = strFmt('%1/%2/%3',
            siteBaseUrl,
            documentFolder,
            fileName);
        
        str accessUrl = strFmt('%1/_api/Web/GetFileByServerRelativePath(decodedurl=\'%2/%3/%4/\')',
            siteBaseUrl,
            siteName,
            documentFolder,
            fileName);
    
        DocumentLocation location = new DocumentLocation();
        location.NavigationUri = new System.Uri(navigationUrl);
        location.AccessUri = new System.Uri(accessUrl);
        
        return location;
    }
     
  • CU19031442-0 Profile Picture
    98 on at
    thanks for response @Martin Dráb 
     
    Yes, provider.GetFile() returning null value
  • Martin Dráb Profile Picture
    237,976 Most Valuable Professional on at
    What is the business scenario? I wonder why you don't use existing classes for this purpose and you're trying to re-implement them by yourself. I suspent you spent extra time to create code that is actually worse than what's already available.
     
    If you use Docu::GetStorageProvider() or Docu::GetStorageProviderById(), DocuSharePointStorageProvider class will create an instance of SharePointDocumentStorageProvider (which is what you do in your code), but it also handles authentication (which you're missing).
     
    If your scenario is about document attachments, you can simply use DocuActionFile.getStream().
  • CU19031442-0 Profile Picture
    98 on at
    @Martin Dráb the business scenario is I need to know particular file with name which i am passing in URL exist or not in sharepoint path. Can u suggest better way to reach that requirement or what i am missing here?
     
    So, here for that I am connecting with SharePoint at that time only getting mentioned null values
  • CU19031442-0 Profile Picture
    98 on at
    @Martin Dráb As you suggested Docu::GetStorageProviderById() method, but it is also validating the sharepoint root URL but i have only particular path access 
     
  • Martin Dráb Profile Picture
    237,976 Most Valuable Professional on at
    Your "business scenario" doesn't talk about a business scenario, just about a particular implementation detail. You haven't explain what business problem you're trying to solve by checking the URL.  We still don't even know whether your requirement is related to document management or not.
     
    I see you've created a new thread for the error message (Using Docu::fileExists() method getting access error), therefore let's not duplicate the discussion here.
  • CU19031442-0 Profile Picture
    98 on at
    ok thank you for response
  • Griet Profile Picture
    31 on at
    I think I have the same problem.
     
    We have an excel file stored in sharepoint, which I access through Microsoft.Dynamics.AX.Framework.FileManagement.SharePointDocumentStorageProvider.
     
    I got my code working and everything was tested and found ok.
     
    But now, for whatever reason, provider.GetFile() only returns null values (with the same code, same parameters, same file).
     
    (I'm now testing with Docu::fileExists( and I'm also getting the 'You are not authorized to connect error'. I'm going to dig deeper in that one.)
  • CU19031442-0 Profile Picture
    98 on at
    Hi @Griet have u fixed the problem which you were facing ?
  • Griet Profile Picture
    31 on at
    No, I haven't found a solution yet...

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 449 Super User 2025 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 422 Most Valuable Professional

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 239 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans