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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Document Template Generation in Plugin

(0) ShareShare
ReportReport
Posted on by 306

Hi,

I generate a document template in a plugin like this and it works well;

            OrganizationRequest req = new OrganizationRequest("SetWordTemplate");

            req["Target"] = new EntityReference(entity.LogicalName, entity.Id);
            req["SelectedTemplate"] = new EntityReference(documentTemplate.LogicalName, documentTemplate.Id);

            context.OrganizationService.Execute(req);

This gets attached to my entity and I can open it.  I have a problem when I try and attach this generated document to an email. It will not open and says the document is corrupted. Perhaps the mime type is wrong?  I use the following code and the mime type is application/octet-stream which I think is the type for the note, not the attachment.

            Entity attachement = new Entity("activitymimeattachment");
            attachement["body"] = document.Attributes["documentbody"];
            attachement["filename"] = document.Attributes["filename"];
            attachement["objectid"] = new EntityReference("email", emailId);
            attachement["objecttypecode"] = "email";
            attachement["mimetype"] = document.Attributes["mimetype"];

            context.OrganizationService.Create(attachement);

How can I attach this document (retrieved via fetchXML) to an email and have it open correctly?

Thanks for any pointers.

EDIT:  Further debugging shows that my query to get the latest document returns a very small amount of data for 'documentbody'.  It feels like maybe the SetWordTemplate has created the document but is running asynchronously and hasn't finished creating the document yet? Any ideas would be great!

*This post is locked for comments

I have the same question (0)
  • a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Document Template Generation in Plugin

    Hello,

    Can you please provide code that you use to retrieve document?

  • mluce Profile Picture
    306 on at
    RE: Document Template Generation in Plugin

    Hi,

    The document retrieve code is;

                var query = @"
                    <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true' count='1'>
                      <entity name='annotation'>
                        <attribute name='filename' />
                        <attribute name='annotationid' />
                        <attribute name='mimetype' />
                        <attribute name='documentbody' />
                        <order attribute='subject' descending='false' />
                        <filter type='and'>
                          <condition attribute='isdocument' operator='eq' value='1' />
                        </filter>
                        <order attribute='createdon' descending='false' />
                      </entity>
                    </fetch>
                ";
    
                var results = context.OrganizationService.RetrieveMultiple(new FetchExpression(query));
    
                if (results == null || results.Entities.Count != 1) return null;
                return results.Entities[0];
    


  • yosra.walid Profile Picture
    on at
    RE: Document Template Generation in Plugin

    hello, i think your problem is in the filter in the fetchxml query ( <filter type='and'>

                         <condition attribute='isdocument' operator='eq' value='1' />

                       </filter>) , delete this line and it will be ok , just add    <condition attribute="filename" operator="null" />  to selet just the annotation they have a file,

    i created the same thing with Dialog process i search the note ( i display the filename ) and i selected the file and then i  add the selected file to c# plugin, in the plugin i created an email and i attach the selected file to it

  • mluce Profile Picture
    306 on at
    RE: Document Template Generation in Plugin

    This change means that nothing is returned in the query (therefore not working).

  • yosra.walid Profile Picture
    on at
    RE: Document Template Generation in Plugin

    TRY to delete this filter <filter type='and'>

                        <condition attribute='isdocument' operator='eq' value='1' />

                      </filter>

  • yosra.walid Profile Picture
    on at
    RE: Document Template Generation in Plugin

    Sorry  you're right ; add this line    <condition attribute="filename" operator="not-null" />

  • mluce Profile Picture
    306 on at
    RE: Document Template Generation in Plugin

    This brings back the same document as my initial query.  Neither of which can be opened when they are attached to the email :-(

  • Verified answer
    mluce Profile Picture
    306 on at
    RE: Document Template Generation in Plugin

    Found the issue.  DocumentBody was being truncated to 2,000 chars because i had  distinct='true' in my query.  Setting this to false returns the entire documentbody.

  • CRM-guy Profile Picture
    80 on at
    RE: Document Template Generation in Plugin

    Hi Mluce,

    Would you please share sample code to create word document from plugin? Thanks.

  • NAOMI314 Profile Picture
    395 on at
    RE: Document Template Generation in Plugin

    Hi Mluce,

    Would you please share sample code to create word document from plugin? Thanks.

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Community Member Profile Picture

Community Member 2

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans