Using Form Save with Custom Web Resources

Microsoft has provided plenty of tools for use when out-of-the-box customizations fall short. Using custom web resources on forms can be a powerful tool, but it should integrate seamlessly with the form. To accomplish this, the web resource can hook onto the save of the form. This provides an easy way for users to interact across the different parts of the form without having to worry about multiple toolbars. In today's blog, we'll show you how it's done!

Let's say we have a web resource that surfaces information from a different entity, or a different source all together, and allows user input. In just a few simple steps, we can save any data we need from the web resource at the same time as the form.

1. Retrieve initial modifiedon information:

var modifiedon = parent.Xrm.Page.getAttribute("modifiedon").getValue();

2. Set up callbacks:

parent.Xrm.Page.data.entity.addOnSave(function(context){

    //Trigger any necessary calls here

});

parent.Xrm.Page.getAttribute("modifiedon").addOnChange(function(context){

    modifiedOn = context.getFormContext().getAttribute("modifiedon").getValue();

});

3. Set modifiedon based on user interaction in the web resource:

If(WebResource Needs To Save){

    Xrm.Page.getAttribute("modifiedon").setValue(new Date());

}

else{

    Xrm.Page.getAttribute("modifiedon").setValue(modifiedOn);

}

While the user interacts with the web resource, it is setting the modifiedon field to either the saved date or a new date. This will make the form appear dirty whenever the web resource requires a save, or other function call, to happen. Users will no longer know they are interacting with a separate web resource.

We hope this comes in handy for you. For more tips and tricks – be sure to check out our blog!

Happy D365'ing!

Language Localized Web Resources

One of the lesser-known features in Dynamics 365 v9.0 is the ability to include localized strings in web resources. In the past, developers resorted to coming up with their own ways to handle this. This usually involved code to determine the current language and then hardcoding strings directly in the JavaScript or storing values in XML files or entities and writing more code to retrieve values themselves. Now using resource files (RESX) and a new JavaScript method you can store the values in a structured manner and use a single line of code to retrieve the correct value.

When creating the resource files for each language there are few points that need to be remembered. In the web resource, you'll need to include the numeric language code in the Name as well as specify the Language type in the Content settings. In this example, 1033 is English and 1036 is French. Also, make sure the key is the same in each file – example "Greeting."

In this example, Visual Studio is being used to create the RESX files but under the hood, they are just XML files. The schema is a bit complex so if possible use a tool to create them rather than doing it manually.

language localized web resources

language localized web resources

In order to get a localized value from one of these files you can use the new "Xrm.Utility.getResourceString" method. The first parameter is the first part of the web resource Name including the prefix. The second is the key from the RESX file. The specific language is not defined anywhere. The result will come from the RESX file that matches the logged in user's default language. If it doesn't exist, it will fall back to the organization's base language.

language localized web resources

The last important piece that needs to be done is to associate the RESX files you might need with the JavaScript web resource. This ensures that when the JavaScript file is loaded it will also load any other associated web resources without needing to add them directly to any forms that might require them.

language localized web resources

Once the setup is complete, you can bind scripts to form events as you normally would.

The resulting alert displays the value based on the logged in user's language selection with is found under the user options.

When the language is set to English, the result comes from the English RESX file.

When the language is set to French, the result comes from the French RESX file.

language localized web resources

There you have it! For more helpful Dynamics 365 tips and tricks check out our blog.

Happy Dynamics 365'ing!

Searching Custom Web Resource Content

There are times when we would like to search the content of our web resources to look for specific unsupported code or to just search for specific content.

Typically, this would be done by searching a source code repository and looking for matching files. While this works, sometimes it would be easier and would save time if we could pass in multiple keywords and generate a file that lists all the web resources with those keywords. This is particularly helpful when you have clients upgrading to newer version of Dynamics 365.

How to Search Web Resources

The approach below describes how we can search by using the Dynamics 365 Service. Alternatively, if you have an On-Premises environment you could request your Administrator to run the SQL query and return the results.

1. Let's define the keywords we want to search and any web resources we want to exclude from the search. This would usually be third party solutions that we have added, etc.

web resource content

 

2. We can now use a Query to retrieve the content of web resources from Dynamics 365 by specifying our criteria to only return the web resources we are interested in. Since we cannot directly query the content of the web resource, we would have to query it after we convert it from the base 64 string.

web resource content

 

3. Once we have the web resource content mapped to the name of the web resource, we can use the keywords we have defined to start searching. Since a web resource could match on multiple keywords, we get the distinct one in the last step.

 

4. Now that we have the list of all the web resources, we can output this information to a file easily.

web resource content

There you go – a quicker way to search for your custom web resource content. Subscribe to our blog today to start receiving more Dynamics 365 tips and tricks in your inbox!

Happy Dynamics 365'ing!