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, ...
Unanswered

Microsoft word integration issue

(2) ShareShare
ReportReport
Posted on by 24
 I tried to generate a word file using Microsoft.Dynamics.Platform.Integration.Office.Word.Export.IDocumentGenerator
 
When  I call the line
var result = documentGenerator.GenerateDocument(params);
I face the error 
CLR Error: The remote server returned an error: (403) Forbidden. The remote server returned an error: (403) Forbidden.
 
 
I try to generate the file with system admin role , this is faced on some environments not all so what is the missing permission and how can I check 
Categories:
I have the same question (0)
  • Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at
    Please tell us more about your implementation. According to its name, IDocumentGenerator is just an interface, therefore it has no code to execute. You must have a class to do something.
  • MF-16081018-0 Profile Picture
    24 on at
    The customized code is similar to that idea applied in create Letter under application in human resources module and same error is faced in this standard function I share the code from the standard class : HrmApplicationWord fucntion : getTemplateStream Line : renderer.renderTemplateToStream(template, this.getDocumentFilters(_application), attachmentStream, OfficeTrimmable::construct()); 
     
    class DocuTemplateRender fucntion : renderWordTemplate 
     
    Below I shared the related code .
     
     
     
     protected System.IO.Stream getTemplateStream(HRMApplication _application)
     {
         DocuTemplateRender      renderer = new DocuTemplateRender();
         System.IO.MemoryStream  attachmentStream = new System.IO.MemoryStream();
         renderer.renderTemplateToStream(template, this.getDocumentFilters(_application), attachmentStream, OfficeTrimmable::construct());
         attachmentStream.Seek(0, System.IO.SeekOrigin::Begin);
         return attachmentStream;
     }
     
     
     
     public void renderTemplateToStream(DocuTemplate template, Map filtersToApply, System.IO.Stream outputStream, object context = null)
     {
         if (!template || !outputStream)
         {
             throw error(error::missingParameter(null));
         }
         if (!filtersToApply)
         {
             filtersToApply = new Map(Types::String, Types::Class);
         }
         else if (filtersToApply.keyType() != Types::String || filtersToApply.valueType() != Types::Class)
         {
             throw error(error::wrongUseOfFunction(methodStr(DocuTemplateRender, renderTemplateToStream)));
         }
         if (!outputStream.CanWrite)
         {
             throw error(error::wrongUseOfFunction(methodStr(DocuTemplateRender, renderTemplateToStream)));
         }
         var filtersByPublicName = this.translateFilterMap(filtersToApply);
         switch (template.TemplateType)
         {
             case OfficeAppApplicationType::Excel:
                 this.renderExcelTemplate(template, filtersByPublicName, outputStream, context);
                 break;
             case OfficeAppApplicationType::Word:
                 this.renderWordTemplate(template, filtersByPublicName, outputStream, context);
                 break;
             default:
                 var ex = new System.NotImplementedException();
                 throw error(ex.Message);
         }
     }
     
     
     
    private void renderWordTemplate(DocuTemplate template, Map filtersToApplyByPublicName, System.IO.Stream outputStream, object context)
    {
        if (!template || !filtersToApplyByPublicName || !outputStream)
        {
            throw error(error::missingParameter(null));
        }
        if (template.TemplateType != OfficeAppApplicationType::Word)
        {
            throw error(error::wrongUseOfFunction(methodStr(DocuTemplateRender, renderWordTemplate)));
        }
        if (filtersToApplyByPublicName.keyType() != Types::String || filtersToApplyByPublicName.valueType() != Types::Class)
        {
            throw error(error::wrongUseOfFunction(methodStr(DocuTemplateRender, renderWordTemplate)));
        }
        if (!outputStream.CanWrite)
        {
            throw error(error::wrongUseOfFunction(methodStr(DocuTemplateRender, renderWordTemplate)));
        }
        using (var origTemplateStream = template.getTemplateStream())
        {
            var params = new DocumentGeneratorParameters();
            params.InputStream = origTemplateStream;
            params.OutputStream = outputStream;
            var filterDictionary = params.FiltersToApply;
            var filterEnumerator = filtersToApplyByPublicName.getEnumerator();
            while (filterEnumerator.moveNext())
            {
                filterDictionary.Add(filterEnumerator.currentKey(), filterEnumerator.currentValue());
            }
            var documentGenerator = this.GetDocumentGenerator();
            var result = documentGenerator.GenerateDocument(params);
            if (!result)
            {
                throw error(strFmt("@ApplicationFoundation:DocuTemplateGenerationFailed", template.TemplateID));
            }
        }
    }
     
  • Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at
    If the standard function doesn't work, you can involve Microsoft (by creating a support ticket). But try it first on a T2+ environment; as I'm looking into code, authentication works in a special way on Dev boxes and it might be the cause.
     
    For reference, the actual class is Microsoft.Dynamics.Platform.Integration.Office.Word.Export.DocumentGenerator.

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
Martin Dráb Profile Picture

Martin Dráb 611 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 285 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans