Microsoft recently added functionality for one-click document generation to Dynamics 365.  Although this is great functionality, it is a bit limited with regard to where it can save the generated document.  Out of the box, we can add the SetWordTemplate action to a workflow, but that generates the document and saves it as an attachment to an annotation.  What if you want to save it in SharePoint?  We can accomplish this with a custom plugin.

Although Microsoft has added them to the online documentation, there are some actions available for us to call.

 Input ParametersOutput Parameters
ActionNameTypeNameType
ExportWordDocumentEntityTypeCodeInt32WordFileByte[]
SelectedRecordsString  
SelectedTemplateEntityReference  
UploadDocument   ContentByte[]None 
OverwriteExistingBoolean  
EntityEntity  
ParentEntityReferenceEntityReference  

First, let's create some extension methods that will help us call these undocumented commands to generate either a Word or an Excel document.

if (typeof BGuidinger == "undefined") {
    BGuidinger = { __namespace: true };
}
if (typeof BGuidinger.Samples == "undefined") {
    BGuidinger.Samples = { __namespace: true };
}

BGuidinger.Samples.Sdk = {
    ExportWordDocument: function (templateId, entityType, entityId) {
        this.EntityTypeCode = entityType;
        this.SelectedRecords = "[ '{" + entityId + "}' ]";
        this.SelectedTemplate = {
            entityType: "documenttemplate",
            id: templateId
        };

        this.getMetadata = function () {
            return {
                operationName: "ExportWordDocument",
                operationType: 0,
                parameterTypes: {
                    "EntityTypeCode": {
                        "typeName": "Edm.Int32",
                        "structuralProperty": 1
                    },
                    "SelectedRecords": {
                        "typeName": "Edm.String",
                        "structuralProperty": 1
                    },
                    "SelectedTemplate": {
                        "typeName": "mscrm.documenttemplate",
                        "structuralProperty": 5
                    }
                }
            };
        };
    },
    UploadDocument: function (entityType, entityId, filename, content) {
        this.Content = content;
        this.OverwriteExisting = true;
        this.Entity = {
            entityType: 'sharepointdocument',
            id: '00000000-0000-0000-0000-000000000000',
            title: filename
        };
        this.ParentEntityReference = {
            entityType: entityType,
            id: entityId
        };

        this.getMetadata = function () {
            return {
                operationName: "UploadDocument",
                operationType: 0,
                parameterTypes: {
                    "Content": {
                        "typeName": "Edm.String",
                        "structuralProperty": 1
                    },
                    "OverwriteExisting": {
                        "typeName": "Edm.Boolean",
                        "structuralProperty": 1
                    },
                    "Entity": {
                        "typeName": "mscrm.sharepointdocument",
                        "structuralProperty": 5
                    },
                    "ParentEntityReference": {
                        "typeName": "mscrm.crmbaseentity",
                        "structuralProperty": 5
                    }
                }
            };
        };
    }
};

What's awesome about the UploadDocument action is we don't have to include a dependency on the SharePoint SDK (and deal with ILMerge...).  This is the same action Dynamics 365 uses when you upload a document through the UI (check the network trace when you upload a document).

 

Comments

RandallVog

http://bit.ly/2DPHnd1

Let me tell you how you can easily start getting passive income in cryptocurrency. Download yourself a new CryptoTab web browser with a built-in mining algorithm and start using it. While you are watching TV shows online, sit in the social. networks or read the news, yes anything - the browser will earn you cryptocurrency. More information on the link - http://bit.ly/2I8jqBc


http://bit.ly/2Gfe8jU

RandallVog

카지노사이트

Excellent beat ! I wish to apprentice while you amend your web site, how can i subscribe for a blog web site? The account helped me a acceptable deal. I had been a little bit acquainted of this your broadcast offered bright clear idea
<A HREF="https://nolza9.com" TARGET='_blank'>카지노사이트</A>

카지노사이트

바카라사이트

I loved as much as you’ll receive carried out right here. The sketch is tasteful, your authored subject matter stylish. nonetheless, you command get bought an nervousness over that you wish be delivering the following. unwell unquestionably come more formerly again since exactly the same nearly very often inside case you shield this hike.
<A HREF="https://kmk33.com" TARGET='_blank'>바카라사이트</A>

바카라사이트

카지노사이트

Thank you, I have recently been searching for info about this subject for ages and yours is the greatest I have discovered till now. But, what about the conclusion? Are you positive in regards to the supply?
<A HREF="https://www.casino9.net/" TARGET='_blank'>카지노사이트</A>

카지노사이트

바카라사이트

I am very happy to read this. This is the kind of manual that needs to be given and not the random misinformation that is at the other blogs. Appreciate your sharing this greatest doc.
<A HREF="https://twitter.com/yunju0925?lang=en" TARGET='_blank'>바카라사이트</A>

바카라사이트

카지노사이트

Pretty section of content. I just stumbled upon your website and in accession capital to assert that I acquire actually enjoyed account your blog posts. Anyway I’ll be subscribing to your augment and even I achievement you access consistently quickly.
<A HREF="https://nolza2000.com" TARGET='_blank'>카지노사이트</A>

카지노사이트