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, ...
Suggested Answer

The remote server returned error(403) Forbidden in d365fo when attaching the documents to attachments using code

(1) ShareShare
ReportReport
Posted on by 950

Dear all,

I am generating a word template using data entity in my code and uploading/inserting to standard attchments for that particular record using x . The said process executes when clicking a button in the form. the button doesnt have any restrictions for role.

I am facing the mentioned error when using custom roles, but my code is working for system admin role.

Any suggestions plzzz. 

the code used is attached below.

 <>
 DocuTemplate template = DocuTemplate::findTemplate(OfficeAppApplicationType::Word,templateName);
        extension = strReverse(subStr(strReverse(template.FileName), 1, strFind(strReverse(template.FileName),'.',1,strLen(template.FileName))));

        fileName = strReverse(subStr(strReverse(template.FileName),6,strlen(template.FileName)));
        fileNameOrig = strfmt('%1%2',contractTable.ContractID,Extension);

        // Ensure the template was present.
            
        if (template && template.TemplateID == templateName)
        {
            Map filtersToApply = new Map(Types::String, Types::Class);
                
            // Create vendors filter
            ExportToExcelFilterTreeBuilder filterBuilder = new ExportToExcelFilterTreeBuilder(template.RootEntityName);
            anytype filterString =  filterBuilder.areEqual(fieldstr(CMSContractDocumentTemplateTmpEntity, ContractId), contractTable.contractId);
            filtersToApply.insert(tablestr(CMSContractDocumentTemplateTmpEntity), filterString);
                               
            // generate the workbook using the template and filters
            DocuTemplateRender renderer = new DocuTemplateRender();
            ExportToExcelStorageHelper storagehelper = new ExportToExcelStorageHelper();
            
            //sheik written
            System.IO.MemoryStream  filestreem = new System.IO.MemoryStream();
            renderer.renderTemplateToStream(template,filtersToApply,filestreem);                
            filestreem.Seek(0, System.IO.SeekOrigin::Begin);
           
            DocumentManagement::attachFile(contractTable.TableId, contractTable.RecId, contractTable.DataAreaId,fileType.TypeId, filestreem, fileNameOrig,  classstr (FileUploadTemporaryStorageStrategy), contractTable.ContractID);
            

I have the same question (0)
  • Suggested answer
    Komi Siabi Profile Picture
    13,109 Most Valuable Professional on at

    Hello,

    You mentioned you have no error using system administrator.

    Did you please confirm that, the user has access to the button control responsible for this action.

  • TestBot Profile Picture
    950 on at

    Yes Komi Siabi.

    For system admin, it was working fine. Also for custom role, the user has access to the button. havent put any restricted access for the button

    i tried to debug the process on assigning the custom role.  please find the below screenshot.

    2843.img.jpg

  • Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

    Can you please share content of the StackTrace property of the exception object?

  • TestBot Profile Picture
    950 on at

    Hi Martin,

    Please find the attached images for the reference.

    Stack-trace.jpg

    Stack-trace1.jpg

  • Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

    Your screenshots just a fragment. Please press the down-arrow at the second column of the row with StackTrace to see the whole content. Copy it and paste as text here.

  • TestBot Profile Picture
    950 on at

    Hi Martin ,

    Please find the stack trace content copied below.

    StackTrace " at Microsoft.OData.Client.HttpWebRequestMessage.GetResponse()\r\n at Microsoft.Dynamics.Platform.Integration.Office.Export.DataAccess.ODataServiceHelper.GetODataPage(Uri requestUri, String parentEntityId, IDictionary`2 headers, List`1 entries, IEdmModel model)\r\n at Microsoft.Dynamics.Platform.Integration.Office.Export.DataAccess.ODataServiceHelper.GetODataFeed(Uri requestUri, String parentEntityId, IEdmModel model, IODataAuthenticationProvider authProvider, IDocumentGenerationLogger logger)\r\n at Microsoft.Dynamics.Platform.Integration.Office.Export.DataAccess.AxODataDataRetriever.GetDataFromOData(String collectionName, String queryOptions)\r\n at Microsoft.Dynamics.Platform.Integration.Office.Export.DataAccess.AxODataDataRetriever.ExecuteDataQuery(DataSet result, EntityDataQuery query, EntityDataQuery parentQuery)\r\n at Microsoft.Dynamics.Platform.Integration.Office.Export.DataAccess.AxODataDataRetriever.GetData(IList`1 queries)\r\n at Microsoft.Dynamics.Platform.Integration.Office.Word.Export.DocumentGenerator.GenerateDocument(WordprocessingDocument workingDocument, Dictionary`2 filtersToApply, Boolean makeStaticDocument)\r\n at Microsoft.Dynamics.Platform.Integration.Office.Word.Export.DocumentGenerator.GenerateDocument(Stream inputStream, Stream outputStream, Dictionary`2 filtersToApply, Boolean makeStaticDocument)\r\n at Microsoft.Dynamics.Platform.Integration.Office.Word.Export.DocumentGenerator.GenerateDocument(DocumentGeneratorParameters parameters)\r\n at Dynamics.AX.Application.DocuTemplateRender.`renderWordTemplate(DocuTemplate template, Map filtersToApplyByPublicName, Stream outputStream, XppObjectBase context) in xppSource://Source/ApplicationFoundation\\AxClass_DocuTemplateRender.xpp:line 557\r\n at Dynamics.AX.Application.DocuTemplateRender.renderWordTemplate(DocuTemplate template, Map filtersToApplyByPublicName, Stream outputStream, XppObjectBase context)\r\n at Dynamics.AX.Application.DocuTemplateRender.`renderTemplateToStream(DocuTemplate template, Map filtersToApply, Stream outputStream, XppObjectBase context, Boolean @context_IsDefaultSet) in xppSource://Source/ApplicationFoundation\\AxClass_DocuTemplateRender.xpp:line 113\r\n at Dynamics.AX.Application.DocuTemplateRender.renderTemplateToStream(DocuTemplate template, Map filtersToApply, Stream outputStream, XppObjectBase context, Boolean @context_IsDefaultSet)\r\n at Dynamics.AX.Application.DocuTemplateRender.renderTemplateToStream(DocuTemplate template, Map filtersToApply, Stream outputStream)\r\n at Dynamics.AX.Application.CMSContractDoucmentRevised_Extension.clicked(FormButtonControl this) in xppSource://Source/CMSDocuSignSuite\\AxClass_CMSContractDoucmentRevised_Extension.xpp:line 119\r\n at FormButtonControl::clicked(Object , Object[] , Boolean& )\r\n at Microsoft.Dynamics.Ax.Xpp.FormNestedElementsCoCSubscriptionsManager.MakeInstanceCall(Object instance, String methodName, String nestedElementName, String rootName, Object[] parameters)\r\n at Microsoft.Dynamics.Ax.Xpp.ReflectionCallHelper.KernelToXppInstanceCallCoC(Object instance, String methodName, String nestedElementName, String rootName, Object[] parameters)\r\n at Microsoft.Dynamics.Ax.MSIL.cqlClassIL.InvokeInstanceCallHandlerCoC(Object instance, String MethodName, String NestedElementName, String RootName, Object[] parameters)\r\n at callILClassMethodCoC(interpret* ip, Char* method, Char* nestedConceptName, Char* rootName, UInt32 chdl, cqlClass* c)" string

    For custom role do i need to add any standard privilege related to document handling or docuref etc. kindly let me know sir.

  • Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

    Thank you! Let me just format it a bit:

    at Microsoft.OData.Client.HttpWebRequestMessage.GetResponse()
    at Microsoft.Dynamics.Platform.Integration.Office.Export.DataAccess.ODataServiceHelper.GetODataPage(Uri requestUri, String parentEntityId, IDictionary`2 headers, List`1 entries, IEdmModel model)
    at Microsoft.Dynamics.Platform.Integration.Office.Export.DataAccess.ODataServiceHelper.GetODataFeed(Uri requestUri, String parentEntityId, IEdmModel model, IODataAuthenticationProvider authProvider, IDocumentGenerationLogger logger)
    at Microsoft.Dynamics.Platform.Integration.Office.Export.DataAccess.AxODataDataRetriever.GetDataFromOData(String collectionName, String queryOptions)
    at Microsoft.Dynamics.Platform.Integration.Office.Export.DataAccess.AxODataDataRetriever.ExecuteDataQuery(DataSet result, EntityDataQuery query, EntityDataQuery parentQuery)
    at Microsoft.Dynamics.Platform.Integration.Office.Export.DataAccess.AxODataDataRetriever.GetData(IList`1 queries)
    at Microsoft.Dynamics.Platform.Integration.Office.Word.Export.DocumentGenerator.GenerateDocument(WordprocessingDocument workingDocument, Dictionary`2 filtersToApply, Boolean makeStaticDocument)
    at Microsoft.Dynamics.Platform.Integration.Office.Word.Export.DocumentGenerator.GenerateDocument(Stream inputStream, Stream outputStream, Dictionary`2 filtersToApply, Boolean makeStaticDocument)
    at Microsoft.Dynamics.Platform.Integration.Office.Word.Export.DocumentGenerator.GenerateDocument(DocumentGeneratorParameters parameters)
    at Dynamics.AX.Application.DocuTemplateRender.`renderWordTemplate(DocuTemplate template, Map filtersToApplyByPublicName, Stream outputStream, XppObjectBase context) in xppSource://Source/ApplicationFoundation\\AxClass_DocuTemplateRender.xpp:line 557
    at Dynamics.AX.Application.DocuTemplateRender.renderWordTemplate(DocuTemplate template, Map filtersToApplyByPublicName, Stream outputStream, XppObjectBase context)
    at Dynamics.AX.Application.DocuTemplateRender.`renderTemplateToStream(DocuTemplate template, Map filtersToApply, Stream outputStream, XppObjectBase context, Boolean @context_IsDefaultSet) in xppSource://Source/ApplicationFoundation\\AxClass_DocuTemplateRender.xpp:line 113
    at Dynamics.AX.Application.DocuTemplateRender.renderTemplateToStream(DocuTemplate template, Map filtersToApply, Stream outputStream, XppObjectBase context, Boolean @context_IsDefaultSet)
    at Dynamics.AX.Application.DocuTemplateRender.renderTemplateToStream(DocuTemplate template, Map filtersToApply, Stream outputStream)
    at Dynamics.AX.Application.CMSContractDoucmentRevised_Extension.clicked(FormButtonControl this) in xppSource://Source/CMSDocuSignSuite\\AxClass_CMSContractDoucmentRevised_Extension.xpp:line 119
    at FormButtonControl::clicked(Object , Object[] , Boolean& )
    at Microsoft.Dynamics.Ax.Xpp.FormNestedElementsCoCSubscriptionsManager.MakeInstanceCall(Object instance, String methodName, String nestedElementName, String rootName, Object[] parameters)
    at Microsoft.Dynamics.Ax.Xpp.ReflectionCallHelper.KernelToXppInstanceCallCoC(Object instance, String methodName, String nestedElementName, String rootName, Object[] parameters)
    at Microsoft.Dynamics.Ax.MSIL.cqlClassIL.InvokeInstanceCallHandlerCoC(Object instance, String MethodName, String NestedElementName, String RootName, Object[] parameters)
    at callILClassMethodCoC(interpret* ip, Char* method, Char* nestedConceptName, Char* rootName, UInt32 chdl, cqlClass* c)

    There is a problem with authorization when accessing an the F&O OData service to get load data. What if you open the template directly instead of by code. Will you get the same error?

  • TestBot Profile Picture
    950 on at

    Hi Martin,

    If the word template is created using a single entity , the template is getting downloaded for custom role in form level.

    When the word template is created using multiple entities, the below error is coming  after downloading in form level.

    I have given the privileges created for the entities of which we are using them in the  word template. but no luck.

    see the below image. "Civil" template is getting downloaded and "Electrical" is not .

    6866.Template1.jpg

  • TestBot Profile Picture
    950 on at

    Hi martin,

    Are you there. expecting some suggestable guidance. pls help

  • Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

    I was happily hiking in Norway. :-)

    What if you open the template directly instead of by code? Will you get the same error?

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 551 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 278 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans