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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Forums / Finance forum / UrlUtility::getUrl() f...
Finance forum

UrlUtility::getUrl() fails when accessed via REST API in DFO365

(2) ShareShare
ReportReport
Posted on by

Hi,

I'm trying to access images from DocuRef,DocuValue, the following code gives me the filemanagement URI part + access token:

var url = Microsoft.Dynamics.AX.Framework.FileManagement.URLBuilderUtilities::GetDownloadUrl(docuValue.FileId, accessToken);

To complete the URL, I use the following code:

var currentHost = new System.Uri(UrlUtility::getUrl());
var generator = new Microsoft.Dynamics.AX.Framework.Utilities.UrlHelper.UrlGenerator();
generator.HostUrl = currentHost.GetLeftPart(System.UriPartial::Authority);

url = generator.HostUrl + '/' + url;


Problem is, the code is executed as published web service and accessed via Postman. The line 

var currentHost = new System.Uri(UrlUtility::getUrl());

throws an error, because sometimes(!) - in 85% of the time, not deterministic reproducible - the code behind

SessionContext serverContext = SessionContext::Get_Current();

returns null.

Is this because AX7 has not really a valid session sometimes when accessed via REST without an actual Web Frontend?

How can this be fixed or is there any other approach to deliver a valid URL to get access to the pictures from outside or maybe even deliver a base64 encoded string via data contract?

best regards and thanks in advance

  • Community Member Profile Picture
    on at

    Hello RB222,

    Did you manage to fix this?

  • Community Member Profile Picture
    on at

    Unfortunately not - I wrote my own Class to generate base64 from the image data as workaround, because this is still not 100% reliable.

  • Manivelan Profile Picture
    5 on at

    Hi,

    The code is executed as published web service and accessed via Postman. Please try this code 

    var env = Microsoft.Dynamics.ApplicationPlatform.Environment.EnvironmentFactory::GetApplicationEnvironment();
    urlnew = env.Infrastructure.HostUrl;
    generator.HostUrl = urlnew;

    instead of below mentioned code,

    System.Uri currentHost = new System.Uri(UrlUtility::getUrl());

    generator.HostUrl = currentHost.GetLeftPart(System.UriPartial::Authority)

    Let me know the feed back

  • Martin Dráb Profile Picture
    240,854 Most Valuable Professional on at

    I think the easiest for getting data from DocuRef is using BusinessDocumentDocuRefEntity (public collection name BusinessDocumentDocuRefs).

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Finance

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans