Düzenle

Customize views in model-driven apps

Customize views in model-driven apps programmatically to control which data users retrieve and how the application displays it. Views are SavedQuery records that use specific filters and display settings. You can create them in code or define them as XML and import them with an unmanaged solution.

A SavedQuery view is different from a UserQuery. A user query, called a Saved view in model-driven apps, 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 view type is appropriate for frequently used queries that span table types and queries that perform aggregation. For more information, see Saved queries.

You can also use the customization tool to customize views. For more information, see Create and edit views.

Types of views

The following table lists the five types of views that you can customize. The type code of a view is stored in the SavedQuery.QueryType parameter.

When you define views for a specific table, the SavedQuery.ReturnedTypeCode parameter returns the table logical name.

View Type Type Code Description
Public 0 - Occurrence: Many
- Actions: Create, Update, Delete
- Comments: 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 are searched when a user searches for records by using the search column in a list view.
Lookup 64 - Occurrence: 1
- Actions: Update only.
- Comments: This is the default view that's used to look up a record when no other view is configured for the lookup column.

Manage views as solution components

Views are solution components. When you create, update, or delete solution components, you apply the change to the solution that contains them. If you don't explicitly specify a solution, the changes get set to the preferred solution of whoever runs your code. If that person doesn't have a preferred solution, the changes go to one of the default solutions.

As a developer, use the SolutionUniqueName optional parameter to explicitly associate these data changes with a specific unmanaged solution.

Create views

To create a public view, specify the following SavedQuery properties:

Property Description
Name A unique identifier for the saved query.
ReturnedTypeCode Matches the logical name of the table.
FetchXml Edit filter criteria or configure sorting. See Query data using FetchXml.
LayoutXml See the layoutxml element in the Customization solutions file schema for the valid elements.
QueryType Must always be zero (0).

The following sample creates a new public view for the Opportunity table:

This sample uses the IOrganizationService.Execute method with the CreateRequest class and the SolutionUniqueName optional parameter.

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>
   <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>";

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

var request = new CreateRequest
{
   Target = sq
};
request["SolutionUniqueName"] = "< Your Solution Unique Name >";

var response = (CreateResponse)service.Execute(request);
_customViewId = response.id;
Console.WriteLine("A new view with the name {0} was created.", sq.Name);

Learn more about the Dataverse SDK for .NET

Update views

If the IsCustomizable managed property allows the view to be updated, use the UpdateRequest class message to update the view. Always update views in the context of a solution. Use the SolutionUniqueName optional parameter to associate the change to a view with a solution.

For an update example, see Deactivate views

Delete views

You should only delete saved queries that you created. A solution component or part of the application might depend on a specific saved query. If there are queries you don't want to appear in the application, deactivate them. Always delete views in the context of a solution. Use the SolutionUniqueName optional parameter to associate the deletion of a view with a solution.

Retrieve views

The following samples retrieve all the public views for the Opportunity table:

This example uses a RetrieveMultipleRequest class with the IOrganizationService.Execute method to retrieve saved query records.

var 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)service.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);
}

Learn more about the Dataverse SDK for .NET

Deactivate views

If you don't want a public view to appear in the application, deactivate it. You can't deactivate a public view that is set as the default view.

Deactivation is an update operation. Always update views in the context of a solution. Use the SolutionUniqueName optional parameter to associate the change to a view with a solution.

The following sample deactivates the Closed Opportunities in Current Fiscal Year view for the Opportunity table:

This sample uses the IOrganizationService.Execute method with the UpdateRequest class and the SolutionUniqueName optional parameter.

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)service.Execute(retrieveOpportuntiesViewRequest);

SavedQuery OpportunityView =
   (SavedQuery)retrieveOpportuntiesViewResponse.EntityCollection.Entities[0];

var updateRequest = new UpdateRequest
{
  Target = new SavedQuery
  {
    Id = OpportunityView.Id,
    StateCode = new OptionSetValue(1), // Inactive
    StatusCode = new OptionSetValue(2) // Inactive
  }
};
updateRequest["SolutionUniqueName"] = "< Your Solution Unique Name >";

service.Execute(updateRequest);

Learn more about the Dataverse SDK for .NET

Note

The view state: active or inactive isn't included with the view when you add it to a solution. Therefore, when you import the solution into a target organization, the status is set to active by default.

Edit columns

You can select columns to display in views from the table or related tables. For more information about how to specify the columns to display, see the layoutxml element in the Customization solutions file schema.

Add custom icons and tooltips to view columns

You can add a custom icon with tooltip text to display in a column depending on the column value. You can also specify localized tooltip text. Add the custom icons as image web resources in your instance, and then use a JavaScript web resource to add JavaScript code for a column to display the icons depending on the column value.

Note

You can add custom icons with tooltips only to read-only grids. This feature isn't supported for editable grids. For more information about editable grids, see Use editable grids.

Two new parameters, imageproviderwebresource and imageproviderfunctionname, are added to the cell element of the layoutxml of savedquery. These parameters let you specify the name of a web resource and a JavaScript function name to display custom icons and tooltip text for a column. The JavaScript code runs when the page loads.

You can also use the new Web Resource and Function Name in the Column Properties page while modifying the property of a column in a view definition to specify the web resource name and JavaScript function name.

The following sample code demonstrates how you can programmatically specify a web resource and a JavaScript function name for adding custom icons and tooltips for the opportunityratingcode column in 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' 
          imageproviderwebresource='new_SampleWebResource'
          imageproviderfunctionname='displayIconTooltip' />
    <cell name='opportunitycustomeridcontactcontactid.emailaddress1'
        width='150' disableSorting='1' />
  </row>
</grid>

The JavaScript function for displaying custom icons and tooltip text expects the following two arguments: the entire row object specified in layoutxml and the calling user's Locale ID (LCID). The LCID parameter enables you to specify tooltip text for the icon in multiple languages. For more information about the supported languages, see Regional and language options for your environment. For a list of locale ID (LCID) values that you can use in your code, see Locale IDs assigned by Microsoft.

Assuming you add custom icons for a choice type of column because it has a limited set of predefined options, use the integer value of the options instead of the label to avoid breaking the code due to changes in the localized label string. In your JavaScript function, specify just the name of an image web resource that you want to use as an icon for a value in the column. The image should be 16x16 pixels. Larger images are automatically scaled down to 16x16 pixels.

The following sample code displays different icons and tooltip text based on one of the values (1: Hot, 2: Warm, 3: Cold) in the opportunityratingcode (Rating) column. The sample code also shows how to display localized tooltip text. For this sample to work, you must create three image web resources each with 16x16 images ( , , and ) in your instance with the following names respectively: new_Hot, new_Warm, and new_Cold.

function displayIconTooltip(rowData, userLCID) {
  var str = JSON.parse(rowData);
  var coldata = str.opportunityratingcode_Value;
  var imgName = "";
  var tooltip = "";
  switch (parseInt(coldata, 10)) {
    case 1:
      imgName = "new_Hot";
      switch (userLCID) {
        case 1036:
          tooltip = "French: Opportunity is Hot";
          break;
        default:
          tooltip = "Opportunity is Hot";
          break;
      }
      break;
    case 2:
      imgName = "new_Warm";
      switch (userLCID) {
        case 1036:
          tooltip = "French: Opportunity is Warm";
          break;
        default:
          tooltip = "Opportunity is Warm";
          break;
      }
      break;
    case 3:
      imgName = "new_Cold";
      switch (userLCID) {
        case 1036:
          tooltip = "French: Opportunity is Cold";
          break;
        default:
          tooltip = "Opportunity is Cold";
          break;
      }
      break;
    default:
      imgName = "";
      tooltip = "";
      break;
  }
  var resultarray = [imgName, tooltip];
  return resultarray;
}

This results in displaying the values in the Rating column with appropriate icons depending on the value, and icon tooltip text when you hover over the icons.

Screenshot of custom icons displayed in the Rating column of a view.

Set a public view as the default view

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

Community tools

There are several community tools that use these APIs to manage views:

Note

These community tools aren't a product of Dataverse, and Microsoft doesn't provide support for the community tools. If you have questions about a tool, contact the publisher. More Information: Community tools