Question

I need to retrieve a custom contacts view. I'm new to MS Dynamics development but was able to connect to the server and retrieve some basic info and found some examples of building the query/view in code using fetch xml but not to retrieve a custom view. Thanks

Was it helpful?

Solution

This method will, if your custom view has a unique name (within the CRM Org), get the View and execute its FetchXml returning an EntityCollection. It should get you started with the basics which you can build out based on your more specific requirements.

This is starter/learning code only - not intended for deployment without error handling.

    public EntityCollection RetrieveCustomView(string viewName, IOrganizationService service)
    {
        var query = new QueryExpression()
        {
            EntityName = "savedview",
            ColumnSet = new ColumnSet("fetchxml"),
            Criteria = new FilterExpression()
            {
                Conditions = { new ConditionExpression("name", ConditionOperator.Equal, viewName) }
            }
        };

        var result = service.RetrieveMultiple(query);

        if (result.Entities.Any())
        {
            var fetchXml = result.Entities[0].Attributes["fetchxml"].ToString();

            var fetchQuery = new FetchExpression(fetchXml);

            return service.RetrieveMultiple(fetchQuery);

        }

        return null;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top