Show us the Document Templates!
There is tremendous value in this flow in that it allows for users to generate a set of standard documents to accompany a CRM record and to share out those documents to team members.
In the next three posts, we will be reviewing how we were able to deliver this functionality to our client. All the files for this project can be found here.
This post will be focused on creating the page to host the functionality, querying Dynamics CRM's Web API for the document templates available for the record type, and displaying the options.
The base HTML and CSS for this page is pretty basic. The purpose here is to create the minimum required code to achieve our functionality.
Note that there is a reference to ClientGlobalContext.js.aspx (line 7 in the HTML). This will allow us to use the GetGlobalContext command to gather some information about the operation context under which the window operates. We will need this context later in this post to know which entity we are working with.
More information about the ClientGlobalContext.js.aspx can be found here.
// Do document template stuff here. We will show how to submit this to an action in a later blog post.
Upon opening, it will automatically have Word Documents selected.
Don't worry about the contents of that method, we will be building it out in a later segment.
Having this in our stateChanged function will ensure that the button will be properly enabled or disabled whenever there is a change in the control's state.
Clear the templates in the state. They will no longer be valid as we have changed the template type we are looking for.
Submit a request to Dynamics CRM's Web API to get the new templates.
Let our control know that its state has changed.
Clearing the templates from state is as simple as resetting the state.templates variable back to an empty array.
The request to Dynamics CRM's Web API will be in the requestTemplates function that we created. That function takes the template type as a parameter and returns a Promise. That Promise will resolve with the templates that we need.
Finally, we need to call the stateChanged method to indicate to our control that we've cleared the templates.
When the Promise resolves, we will grab the templates and set the state.templates to the templates we got back from Dynamics CRM.
Finally, we will indicate that the state has changed by calling the stateChanged method.
For this, we want to set the template select control based on the templates contained in the state.template property.
Where name represents the name of the document template, and id represent's its GUID. The code below iterates through the templates and creates an option for each one we have retrieved.
In order to retrieve the document templates from Dynamics CRM, we will make a request to its Web API. The Web API is a REST web service that supports ODATA queries.
Additionally, because we are using ODATA, we can do some filtering to reduce the size of the result we receive to only the records and data we are interested in. This will allow us to reduce the data over the wire.
First, we will declare a variable called columns which will hold the names of the columns we want to get back from the service as a string array.
Then, we will format a select segment of a query string to append to our ODATA endpoint. The select segment of ODATA queries act similar to the SELECT command in SQL. It allows you to specify which columns you want returned in your query.
Earlier in the post, while we were setting up the HTML page, we made sure to include the ClientGlobalContext.js.aspx page. In this case, we can use it to grab the query string parameters fed in to the window. How that gets set will be covered by our next post.
Please note that in the highlighted line in the code section above, it should be '&' and not '&'. WordPress has some encoding issues.
To submit the request, we will be using an XMLHttpRequest. More information about XMLHttpRequests can be found here. Below is the basic shell of our request. The code below is setting the request type, location, and headers that are required for our request. Additionally, we have a handler that we've attached to handle when the request has completed.
The results of the query will be in the response property of the request and in JSON format. First, we will parse out the result from the response. Next, we will get the array from the value field of the result. We will default it to an empty array if the value property doesn't exist. Finally, we will resolve the templates promise.
For now, for errors, we will simply reject the promise. For your specific case, you should log the error and handle the response in a way that best suits your need.
And that's it! Our control should be ready for publishing!
In our next blog post, we will show how to upload your web resources and how to create a button on your record to open this dialogue.