Jaa


Customize entity views

 

Applies To: Dynamics CRM 2015

Entity views are special saved queries that retrieve data by using a specific filter. They also contain information about how the data in the view should be displayed in the application. Entity views are SavedQuery records that you can create programmatically. You can also define them as XML, and import them into Microsoft Dynamics CRM with an unmanaged solution.

An Entity view is different from a UserQuery. A user query, called a Saved view in the application, is owned by an individual user, can be assigned and shared with other users, and can be viewed by other users depending on the query's access privileges. This is appropriate for frequently used queries that span entity types and queries that perform aggregation. More information: UserQuery (saved view) entity

In This Topic

Types of views

View tasks

  • Create views

  • Update views

  • Delete views

  • Retrieve views

  • Deactivate views

  • Edit filter criteria or configure sorting

  • Edit columns

  • Set as default

Types of views

The following table lists the five types of views that are supported for customization. The type code of a view is stored in the SavedQuery.QueryType attribute. Note that there are other valid values for the QueryType attribute not listed here because this entity is also used to store Microsoft Office Outlook filters and templates. For more information, see Offline and Outlook filters and templates.

When views are defined for a specific entity, the SavedQuery.ReturnedTypeCode attribute returns the entity logical name.

View Type

Type Code

Description

Public

0

  • Occurrence: Many

  • Actions: Create, Update, Delete

  • Comments: You can set one of these views as the default public view by setting SavedQuery.IsDefault to true.

Advanced Find

1

  • Occurrence: 1

  • Actions: Update only.

  • Comments: By default, this view is displayed when results are shown in Advanced Find.

Associated

2

  • Occurrence: 1

  • Actions: Update only,

  • Comments: By default, this view is displayed when a grid of related records appears in the navigation pane of a record.

Quick Find

4

  • Occurrence: 1

  • Actions: Update only.

  • Comments: This view defines the columns that will be searched when a user searches for records by using the search field in a list view.

Lookup

64

  • Occurrence: 1

  • Actions: Update only.

  • Comments: This is the default view that will be used to look up a record when no other view has been configured for the lookup field.

View tasks

Because views are SavedQuery records, you can create, update, retrieve, delete and deactivate them. In addition you can edit filter criteria or configure sorting, edit columns or set a view as a default view.

Create views

To create a public view, specify the following properties:

  • SavedQuery.Name: A unique identifier for the saved query.

  • SavedQuery.ReturnedTypeCode: Matches the logical name of the entity.

  • SavedQuery.FetchXml: See Use FetchXML to construct a query.

  • SavedQuery.LayoutXml: See <layoutxml> (SavedQuery) for the valid elements.

  • SavedQuery.QueryType: Must always be zero (0).

The following sample creates a new public view for the opportunity entity:


                    System.String layoutXml =
@"<grid name='resultset' object='3' jump='name' select='1' 
    preview='1' icon='1'>
    <row name='result' id='opportunityid'>
    <cell name='name' width='150' /> 
    <cell name='customerid' width='150' /> 
    <cell name='estimatedclosedate' width='150' /> 
    <cell name='estimatedvalue' width='150' /> 
    <cell name='closeprobability' width='150' /> 
    <cell name='opportunityratingcode' width='150' /> 
    <cell name='opportunitycustomeridcontactcontactid.emailaddress1' 
        width='150' disableSorting='1' /> 
    </row>
</grid>";

                    System.String fetchXml =
                    @"<fetch version='1.0' output-format='xml-platform' 
    mapping='logical' distinct='false'>
    <entity name='opportunity'>
    <order attribute='estimatedvalue' descending='false' /> 
    <filter type='and'>
        <condition attribute='statecode' operator='eq' 
        value='0' /> 
    </filter>
    <attribute name='name' /> 
    <attribute name='estimatedvalue' /> 
    <attribute name='estimatedclosedate' /> 
    <attribute name='customerid' /> 
    <attribute name='opportunityratingcode' /> 
    <attribute name='closeprobability' /> 
    <link-entity alias='opportunitycustomeridcontactcontactid' 
        name='contact' from='contactid' to='customerid' 
        link-type='outer' visible='false'>
        <attribute name='emailaddress1' /> 
    </link-entity>
    <attribute name='opportunityid' /> 
    </entity>
</fetch>";

                    SavedQuery sq = new SavedQuery
                    {
                        Name = "A New Custom Public View",
                        Description = "A Saved Query created in code",
                        ReturnedTypeCode = "opportunity",
                        FetchXml = fetchXml,
                        LayoutXml = layoutXml,
                        QueryType = 0
                    };

                    _customViewId = _serviceProxy.Create(sq);
                    Console.WriteLine("A new view with the name {0} was created.", sq.Name);

Update views

If the SavedQuery.IsCustomizable managed property allows the view to be updated, you can use the IOrganizationService.Update method or the UpdateRequest message to update the view.

Delete views

You should only delete saved queries that you have created. A solution component or part of the application may depend on a specific saved query. If there are queries you do not want to appear in the application, you should deactivate them.

Retrieve views

Use a RetrieveMultipleRequest or IOrganizationService.RetrieveMultiple to retrieve saved query records.

The following sample retrieves all the public views for the opportunity entity:


        QueryExpression mySavedQuery = new QueryExpression
        {
            ColumnSet = new ColumnSet("savedqueryid", "name", "querytype", "isdefault", "returnedtypecode", "isquickfindquery"),
            EntityName = SavedQuery.EntityLogicalName,
            Criteria = new FilterExpression
            {
                Conditions =
{
    new ConditionExpression
    {
        AttributeName = "querytype",
        Operator = ConditionOperator.Equal,
        Values = {0}
    },
    new ConditionExpression
    {
        AttributeName = "returnedtypecode",
        Operator = ConditionOperator.Equal,
        Values = {Opportunity.EntityTypeCode}
    }
}
            }
        };
        RetrieveMultipleRequest retrieveSavedQueriesRequest = new RetrieveMultipleRequest { Query = mySavedQuery };

        RetrieveMultipleResponse retrieveSavedQueriesResponse = (RetrieveMultipleResponse)_serviceProxy.Execute(retrieveSavedQueriesRequest);

        DataCollection<Entity> savedQueries = retrieveSavedQueriesResponse.EntityCollection.Entities;

        //Display the Retrieved views
        foreach (Entity ent in savedQueries)
        {
            SavedQuery rsq = (SavedQuery)ent;
            Console.WriteLine("{0} : {1} : {2} : {3} : {4} : {5},", rsq.SavedQueryId, rsq.Name, rsq.QueryType, rsq.IsDefault, rsq.ReturnedTypeCode, rsq.IsQuickFindQuery);
        }

Deactivate views

If you do not want a public view to appear in the application, you can deactivate it. You cannot deactivate a public view that is set as the default view. The following sample deactivates the Closed Opportunities in Current Fiscal Year view for the Opportunity entity:


System.String SavedQueryName = "Closed Opportunities in Current Fiscal Year";
QueryExpression ClosedOpportunitiesViewQuery = new QueryExpression
{
    ColumnSet = new ColumnSet("savedqueryid", "statecode", "statuscode"),
    EntityName = SavedQuery.EntityLogicalName,
    Criteria = new FilterExpression
    {
        Conditions =
        {
            new ConditionExpression
            {
                AttributeName = "querytype",
                Operator = ConditionOperator.Equal,
                Values = {0}
            },
            new ConditionExpression
            {
                AttributeName = "returnedtypecode",
                Operator = ConditionOperator.Equal,
                Values = {Opportunity.EntityTypeCode}
            },
                            new ConditionExpression
            {
                AttributeName = "name",
                Operator = ConditionOperator.Equal,
                Values = {SavedQueryName}
            }
        }
    }
};

RetrieveMultipleRequest retrieveOpportuntiesViewRequest = new RetrieveMultipleRequest { Query = ClosedOpportunitiesViewQuery };

RetrieveMultipleResponse retrieveOpportuntiesViewResponse = (RetrieveMultipleResponse)_serviceProxy.Execute(retrieveOpportuntiesViewRequest);

SavedQuery OpportunityView = (SavedQuery)retrieveOpportuntiesViewResponse.EntityCollection.Entities[0];
_viewOriginalState = (SavedQueryState)OpportunityView.StateCode;
_viewOriginalStatus = OpportunityView.StatusCode;


SetStateRequest ssreq = new SetStateRequest
{
    EntityMoniker = new EntityReference(SavedQuery.EntityLogicalName, (Guid)OpportunityView.SavedQueryId),
    State = new OptionSetValue((int)SavedQueryState.Inactive),
    Status = new OptionSetValue(2)
};
_serviceProxy.Execute(ssreq);

Edit filter criteria or configure sorting

To edit the filter or edit how the data is sorted, you must set the SavedQuery.FetchXml attribute. For more information, see Build queries with FetchXML.

Tip

If you are not familiar with FetchXML the following messages can be used to convert between QueryExpression and FetchXML:QueryExpressionToFetchXmlRequest and FetchXmlToQueryExpressionRequest.

Edit columns

The columns that you want to display in views can be taken from the entity or related entities. For more information about how to specify the columns to display, see <layoutxml> (SavedQuery).

Set as default

Only one active public view can be set as the default view. To make a view the default view, set the IsDefault property to true.

See Also

Sample: Work with views
SavedQuery (view) entity messages and methods
Saved query XML reference
Build queries with FetchXML
Extend the metadata model
Customize entity forms
Customize global option sets
Developers guide to customization for Microsoft Dynamics CRM

© 2016 Microsoft. All rights reserved. Copyright