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!

Leave a comment