CRM 2016 + JavaScript: Web Resource cannot be reloaded

Since we got upgraded to CRM 2016 from CRM 2015, we faced a strange issue with JavaScript and Web Resource (html).

At some point of time the script goes to reload the html Web Resource with the following code:

var webResource = Xrm.Page.getControl("WebResource_ControlName");
webResource.setSrc(webResource.getSrc());

Easiest way to reload the web resource!

But the same code does not work in case of CRM 2016. As the web resource was already loaded when the page was loaded. CRM 2016 treats the same web resource not to be loaded again (may be for better performance!).

After trying numerous ways to solve, following code worked perfectly:

var webResource = Xrm.Page.getControl("WebResource_ControlName");
webResource.setSrc(Xrm.Page.context.getClientUrl() + "/WebResources/new_webResource?data=someValue%26ver%3D" + Math.random());

Here, I had to pass some parameters. What I have done to modify is added another parameter called “ver” (you can have any other parameter name instead of “ver”) and the value will be rendered from Math.random() method. This way the web browser treats each URL as a new request as Math.random() will return random number each time it will be executed.

CRM 2011: Get list of all Web Resources from the solution using C#

When you do not have WebResource class… When there is no proxy of CRM service added to your project and you are using Late Binding method to connect to Microsoft Dynamics CRM 2011. And you want to get the list of all Web Resources from the given solution. Here is a method to get the List of all Web Resources from the solution passed as a parameter.

Here is what we have in WebResource class:

public class WebResource
{
    /// <summary>
    /// Gets or sets the display name.
    /// </summary>
    /// <value>
    /// The display name.
    /// </value>
    public string DisplayName { get; set; }

    /// <summary>
    /// Gets or sets the web resource id.
    /// </summary>
    /// <value>
    /// The web resource id.
    /// </value>
    public Guid WebResourceId { get; set; }
}

And the method that gets the list of all Web Resources from the Solution is:

public static List<WebResource> GetSolutionRelatedWebResources(IOrganizationService OrgService, object SolutionId)
{
    var fetchQuery = @"<fetch mapping='logical' count='500' version='1.0'>
                        <entity name='webresource'>
                            <attribute name='displayname' />
                            <link-entity name='solutioncomponent' from='objectid' to='webresourceid'>
                                <filter>
                                    <condition attribute='solutionid' operator='eq' value='" + SolutionId + @"' />
                                </filter>
                            </link-entity>
                        </entity>
                    </fetch>";

    EntityCollection result = OrgService.RetrieveMultiple(new FetchExpression(fetchQuery));
    List<WebResource> WebResourceList = new List<WebResource>();

    foreach (var webresource in result.Entities)
    {
        WebResourceList.Add(new WebResource() { DisplayName = webresource.Attributes["displayname"].ToString(), WebResourceId = (Guid)webresource.Attributes["webresourceid"] });
    }

    return WebResourceList;
}

Remember, as you have not created a proxy of the CRM web service and you have not generated any class using crmsvcutil.exe provided in bin directory in SDK, you would want to try the above code.

Happy Programming!