利用 Common Data Service 创建应用Creating an app from the Common Data Service

生成基于 Common Data Service 的三屏应用,然后通过应用探索屏幕和控件。Generate a three screen app from the Common Data Service, then explore screens and controls from the app. 更新应用屏幕、控件和字段;引入其他数据并在应用中触发流。Update app screens, controls, and fields; bring in additional data and trigger a flow from the app.


生成应用Generate an app

在本课程的这一部分中,我们将创建基于 Common Data Service 实体的应用。In this section of the course, we'll create an app based on entities in the Common Data Service. 实体是可以修改、存储、检索和交互的共享数据区块。Entities are chunks of shared data that can be modified, stored, retrieved, and interacted with. 我们将生成基于实体的应用,并介绍如何自定义应用、添加其他数据源以及在应用中调用流。We'll generate the app from an entity, show you how to customize the app, add another data source, and call a flow from the app. 如果已阅读完创建基于 SharePoint 列表的应用这一部分,便会发现我们将介绍的内容有部分重叠,不同之处在于介绍更为深入,尤其是在自定义应用方面。If you already completed the section on creating an app from a SharePoint list, we will cover some of the same territory but in more depth, especially around customizing the app.

我们将创建一个案例管理应用,以供 IT 部门跟踪整个组织内的软硬件问题,安排问题处理优先顺序,并采取相应措施。We'll create a case-management app that an IT department could use to track, prioritize, and act on hardware and software issues across an organization. 浏览这些主题时,可能还会想到类似应用的其他用途。As you go through the topics, you might also think of other uses for an app like this. 我们使用的是 Common Data Service 数据,因为此服务非常适合存储应用数据。不过,你也可以生成基于其他数据源的相同应用。We're using data from the Common Data Service because it's well-suited to storing app data, but you could build the same app with a different data source.

PowerApps 包含更为复杂的案例管理模板,其中所用实体与我们即将生成的应用使用的实体相同。PowerApps includes a more complex Case Management template that uses the same entities as the app we'll build. 建议在阅读完此部分后探索这个模板,大致了解可以在 PowerApps 中生成的内容。After you've completed this section, we encourage you to explore that template to get a sense of what you can build in PowerApps.

创建 Common Data Service 数据库Create a Common Data Service database

生成此应用的第一步是创建 Common Data Service 数据库(如果尚未创建的话)。The first step in building this app is to create a Common Service database if you don't already have one. Common Data Service 数据库是在环境中创建。You create a Common Data Service database in an environment. 环境是应用和其他资源的容器(本课程的后续部分将详细介绍环境)。An environment is a container for apps and other resources (you'll learn more about environments later in the course). 环境管理员可以按照下面这些步骤操作,创建数据库(如果不是管理员,请与组织管理员联系)。An environment admin can follow these steps to create a database (if you're not an admin, check with an admin in your organization).

单击“主页”选项卡中的“创建数据库”。From the Home tab, click Create Database.

创建 Common Data Service 数据库

指定是否要限制对数据库的访问权限(我们将继续允许公开访问),然后单击“创建我的数据库”。Specify whether you want to restrict access to the database (we'll keep it open), then click Create my database.

指定对 Common Data Service 的访问权限

完成此过程后,便会看到通用数据模型中包含的所有标准实体。When the process is complete, you see all the standard entities that are included in the common data model. 下图展示了部分实体。Some of them are shown below.

Common Data Service 标准实体

生成基于“案例”实体的应用Generate an app from the Case entity

至此,数据库已创建,我们将连接“案例”实体并生成应用。Now that the database is created, we connect to the Case entity and generate an app. 依次单击“新建应用”和“PowerApps Studio for web”。Click New app, then PowerApps Studio for web.

PowerApps Studio for web 中的“新建应用”

由于我们要生成的是基于 Common Data Service 实体的手机应用,因此单击或点击“Common Data Service”下的“手机布局”。We're building a phone app for a Common Data Service entity, so under Common Data Service click or tap Phone layout.

基于 Common Data Service 的手机应用

在下一屏中,选择一个连接和要连接的实体,然后单击“连接”。In the next screen, you choose a connection and an entity to connect to, then click Connect.


在你单击“连接”后,PowerApps 便会开始生成应用。After you click Connect, PowerApps starts to generate the app. PowerApps 会进行各种数据推论,生成实用应用,以便你可以在此基础上进行各种自定义。PowerApps makes all sorts of inferences about your data so that it generates a useful app as a starting point.

在 PowerApps Studio 中查看应用View the app in PowerApps Studio

此时,新建的三屏应用会在 PowerApps Studio 中打开。Your new three-screen app opens in PowerApps Studio. 所有基于数据生成的应用都有一组相同的屏幕:All apps generated from data have the same set of screens:

  • 浏览屏幕:可以在此屏幕中浏览、排序、筛选和刷新从列表中拉取的数据,并能通过单击 (+) 图标添加项。The browse screen: where you browse, sort, filter, and refresh the data pulled in from the list, as well as add items by clicking the (+) icon.
  • 详细信息屏幕:可以在此屏幕中查看项的详细信息,并能选择删除或编辑项。The details screen: where you view more detail about an item, and can choose to delete or edit the item.
  • 编辑/创建屏幕:可以在此屏幕中编辑现有项,也可以创建新项。The edit/create screen: where you edit an existing item or create a new one.

在左侧导航栏中,单击或点击右上角的图标,切换到缩略图视图。In the left navigation bar, click or tap an icon in the upper-right corner to switch to the thumbnail view.


单击或点击每个缩略图,查看相应屏幕上的控件。Click or tap each thumbnail to view the controls on that screen.


接下来,我们将详细探索应用,然后自定义应用,使其更能满足我们的需求。Next we'll explore the app in more detail and then customize it to better suit our needs.

浏览生成的应用Explore a generated app

此主题将更深入地介绍生成的应用,即探索定义应用行为的屏幕和控件。In this topic, we look more closely at the generated app - reviewing the screens and controls that define the app's behavior. 虽然我们的介绍无法面面俱到,但深入了解此应用的工作方式将有助于你生成自己的应用。We won't go through all the details, but seeing more about how this app works will help you to build your own apps. 后续主题将介绍与屏幕和控件结合使用的公式。In a later topic, we'll look at the formulas that work with screens and controls.

在预览模式下运行应用Run the app in preview mode

单击或点击右上角的Click or tap 启动应用预览箭头 运行应用。in the top right to run the app. 如果浏览应用,便会发现其中包含实体数据,并提供令人满意的默认体验。If you navigate through the app, you see that it includes data from the entity and provides a good default experience.


了解 PowerApps 中的控件Understanding controls in PowerApps

控件只是一种与行为相关联的 UI 元素。A control is simply a UI element that has behaviors associated with it. PowerApps 中的许多控件与你在其他应用中用过的控件(如标签、文本输入框、下拉列表、导航元素等)相同。Many controls in PowerApps are the same as controls that you've used in other apps: labels, text-input boxes, drop-down lists, navigation elements, and so on. 不过,PowerApps 具有更为专业化的控件,如“”(显示摘要数据)和“窗体”(显示详细数据并允许创建和编辑项)。But PowerApps has more specialized controls like Galleries (which display summary data) and Forms (which display detail data and enable you to create and edit items). 还有其他一些相当棒的控件,如“图像”、“照相机”和“条形码”。And also some other really cool controls like Image, Camera, and Barcode. 若要查看可用控件,请单击或点击功能区上的“插入”,然后依次单击或点击从“文本”到“图标”的各个选项。To see what's available, click or tap Insert on the ribbon, and then click or tap each of the options in turn, Text through Icons.

PowerApps Studio 功能区上的“控件”选项卡

探索浏览屏幕Explore the browse screen

三个应用屏幕都有一个主控件和一些附加控件。Each of the three app screens has a main control and some additional controls. 应用中的第一屏是浏览屏幕,默认命名为 BrowseScreen1The first screen in the app is the browse screen, named BrowseScreen1 by default. 此屏幕上的主控件是名为 BrowseGallery1 的库。The main control on this screen is a gallery named BrowseGallery1. BrowseGallery1 包含其他控件,例如 NextArrow1(一种图标控件,单击或点击它可以转到详细信息屏幕)。BrowseGallery1 contains other controls, like NextArrow1 (an icon control - click or tap it to go to the details screen). 此屏幕上还有独立控件,例如 IconNewItem1(一种图标控件,单击或点击它可以在编辑/创建屏幕中创建项)。There are also separate controls on the screen, like IconNewItem1 (an icon control - click or tap it to create an item in the edit/create screen).


PowerApps 提供各种库类型,以便你可以使用最能满足应用布局要求的类型。PowerApps has a variety of gallery types so you can use the one that best suits your app's layout requirements. 此部分的后续主题将介绍更多布局控制方法。You will see more ways to control layout later in this section.

PowerApps 库选项

探索详细信息屏幕Explore the details screen

第二屏是详细信息屏幕,默认命名为 DetailScreen1Next is the details screen, named DetailScreen1 by default. 此屏幕上的主控件是名为 DetailForm1 的显示窗体。The main control on this screen is a display form named DetailForm1. DetailForm1 包含其他控件,例如 DataCard1(一种卡片控件,在此示例中用于显示问题类别)。DetailForm1 contains other controls, like DataCard1 (a card control, which displays the question category in this case). 此屏幕上还有独立控件,例如 IconEdit1(一种图标控件,单击或点击它可以在编辑/创建屏幕上编辑当前项)。There are also separate controls on the screen like IconEdit1 (an icon control - click or tap it to edit the current item on the edit/create screen).


虽然有许多库选项,但窗体更简便易用(不是编辑窗体,就是显示窗体)。There are lots of gallery options, but forms are more straightforward - it's either an edit form or a display form.

PowerApps 窗体选项

探索编辑/创建屏幕Explore the edit/create screen

应用中的第三屏是编辑/创建屏幕,默认命名为 EditScreen1The third screen in the app is the edit/create screen, named EditScreen1 by default. 此屏幕上的主控件是名为 EditForm1 的编辑窗体。The main control on this screen is an edit form named EditForm1. EditForm1 包含其他控件,例如 DataCard8(一种卡片控件,在此示例中可方便你编辑问题类别)。EditForm1 contains other controls, like DataCard8 (a card control, which allows you to edit the question category in this case). 此屏幕上还有独立控件,例如 IconAccept1(一种图标控件,单击或点击它可以保存在编辑/创建屏幕上做出的更改)。There are also separate controls on the screen like IconAccept1 (an icon control - click or tap it to save the changes you made on the edit/create screen).


至此,你已大致了解应用包含哪些屏幕和控件,下一主题将介绍如何自定义应用。Now that you have a sense of how the app is composed of screens and controls, we'll look at how you customize the app in the next topic.

自定义应用Customize the app

在此部分的前两个主题中,你生成了基于 Common Data Service 实体的应用,并探索了组成应用的三个屏幕。In the first two topics in this section, you generated an app from a Common Data Service entity and explored the app to get a better understanding of how three screen apps are composed. 虽然 PowerApps 生成的应用非常有用,但经常需要在生成应用后进行自定义。The app that PowerApps generated is useful, but you will often customize an app after it's generated. 此主题将逐步介绍如何对应用的浏览屏幕进行一些更改。In this topic, we'll walk through some changes for the browse screen of the app. 虽然可以自定义任何屏幕,但我们希望以其中一个为重点,更深入一点介绍自定义。You can customize any of the screens, but we wanted to focus on one and provide a bit more depth to the customizations. 建议采用生成的基于实体、Excel 文件或其他源的任何应用,了解如何自定义应用。We encourage you to take any app you generate - from an entity, an Excel file, or another source - and see how you can customize it. 这确实是了解应用的各组成部分的最佳方式。It really is the best way to learn how apps are put together.

PowerApps 在生成应用后,确定了要使用的布局,以及每个屏幕上显示的特定字段。When PowerApps generated the app, it decided on a layout to use, and particular fields to show on each screen. 对于此应用,让我们来选择包含状态栏的库控件(我们很快就会自定义状态栏)。For this app, let's choose a gallery control that has a status bar (we'll customize the status bar shortly). 在右侧窗格的“布局”选项卡上,选择所需的布局。In the right-hand pane, on the Layout tab, select the layout you want. 你会立即看到所选布局,因为 PowerApps 在你执行更改的同时更新应用。You see the results right away because PowerApps updates the app as you make changes.


设置正确的基本布局后,现在让我们来更改显示字段。With the right basic layout, now change the fields that are displayed. 单击或点击第一项中的字段,然后在右侧窗格中更改每一项的显示数据。Click or tap a field in the first item, then in the right-hand pane, change the data that is displayed for each item. 这样一来,便可以改进实体中每一项的摘要。This provides a better summary of each item in the entity.


更改应用主题Change the app theme

PowerApps 提供一系列可用于应用的主题,与 PowerPoint 一样。PowerApps provides a set of themes you can use in your app, much like PowerPoint. 在下面的屏幕中,应用采用了“沙丘色”主题,并粘贴有简单徽标。In the following screen, you see the Dune theme applied, and a simple logo that we pasted into the app. 虽然这些都是基本更改,但却可以有效改善应用外观。These are basic changes, but can do a lot to improve the appearance of your app.


使用公式显示案例状态Use a formula to show the case status

PowerApps 的主要优势之一是,无需编写传统的应用程序代码。也就是说,无需成为开发者也可以创建应用!One of the major benefits of PowerApps is not having to write traditional application code - you don’t have to be a developer to create apps! 不过,仍需要通过一种方法在应用中表达逻辑,并控制应用的导航、筛选、排序及其他功能。But you still need a way to express logic in an app and to control an app’s navigation, filtering, sorting, and other functionality. 这时候公式便派上用场。This is where formulas come in.

如果你用过 Excel 公式,就不会对 PowerApps 采用的方法感到陌生。If you have used Excel formulas, the approach that PowerApps takes should feel familiar. 假设要在案例解决后显示绿色状态栏,否则显示红色状态栏。Suppose you want to show the status bar in green if a case is resolved, or in red otherwise. 为此,请选择屏幕上的状态控件,然后在编辑栏中将此控件的 Fill 属性设置为以下公式:If(Status="Resolved", Color.Green, Color.Red)To do this, you select the status control on the screen, and then set the Fill property of that control to this formula in the formula bar: If(Status="Resolved", Color.Green, Color.Red). 这就像 Excel 公式一样,不同之处在于 PowerApps 公式引用的是控件和其他应用元素,而不是电子表格中的单元格。This is like an Excel formula, but PowerApps formulas refer to controls and other app elements rather than cells in a spreadsheet. 下图展示了在何处设置公式以及应用中的结果。The following image shows where to set the formula, and the result in the app.


按日期排序和筛选Sort and filter based on date

在浏览屏幕中,生成的应用允许搜索案例,并对库中的项列表进行排序。On the browse screen, the generated app lets you search for cases and sort the list of items in the gallery. 我们将删除搜索和排序功能,以支持按日期显示案例。We're going to remove the search and sort functionality in favor of showing cases based on a date. 你可以结合使用这些方法,而我们将重点介绍此应用按日期显示案例的方法。You could combine these methods, but we'll focus on the date-based approach for this app. 下图中展示了我们添加的项:In the image below, you see the items we added:

  • 告诉用户要执行的操作的文本标签(“显示在此日期之后的案例:”):依次转到“插入” > “文本” > “标签”;将“Fill”公式更改为“White”。A text label ("Show cases after:") so users know what to do: Insert > Text > Label; change the Fill formula to White.
  • 日期选取器:依次转到“插入” > “控件” > “日期选取器”。A date picker: Insert > Controls > Date picker.
  • 将浏览库的 Items 属性与日期选取器相关联的公式:Filter(Case, DatePicker1.SelectedDate < LastModifiedDateTime)A formula that connects the browse gallery Items property to the date picker: Filter(Case, DatePicker1.SelectedDate < LastModifiedDateTime).

日期设置为 10 月 20 日,结果就是,应用显示在此日期之后创建的案例。The date is set to Oct 20 and you see the result that the app is showing the cases created after this date. 请注意,默认情况下,实体中所有案例的上次修改日期都相同。Note that by default, all cases in the entity have the same last modified date. 可以更新一个或多个,了解筛选的工作原理。You can update one or more to see how filtering works. 本课程的后面部分将介绍如何处理实体数据。We cover working with entity data later in the course.


显示案例总数Show total number of cases

虽然此主题涉及许多方面,但有关自定义的介绍即将结束。We're covering a lot of ground here, but we're almost done with the customizations. 此主题将介绍的最后一项操作是添加标签来显示两个数字:一个是案例总数,另一个是与基于日期的筛选器匹配的案例数。The last thing we'll do in this topic is add labels that show two numbers: the total number of cases and the number of cases that match our date-based filter.


本视频详细介绍了如何添加两个标签,下面列出了我们为每个标签设置的基本属性:The video goes into detail about how to add the two labels, but here's the basics on which properties we set for each label:

  • Align = CenterAlign = Center
  • Width = Parent.Width/2Width = Parent.Width/2
  • 左框 Text = "Total cases: " & CountRows(Case)Left box Text = "Total cases: " & CountRows(Case). 这会统计实体中的所有案例。This includes all cases that are in the entity.
  • 右框 Text = Filtered cases: " & CountRows(BrowseGallery1.AllItems)Right box Text = Filtered cases: " & CountRows(BrowseGallery1.AllItems). 这只会统计与基于日期的筛选器匹配的案例。This includes only those cases that match the date-based filter.

有关自定义应用的介绍到此结束,下一主题将介绍如何添加数据源和流,并展示完成的应用。OK, that wraps up the app customizations - in the next topic we'll add a data source, and a flow, and show you the finished app.

添加数据源和流Add a data source and flow

到目前为止,在此部分中,我们已经生成基于 Common Data Service 中“案例”实体的应用,探索了应用的各组成部分,并自定义了应用的多个方面。So far in this section, we have generated an app based on the Case entity from the Common Data Service, explored the app to see how it's put together, and customized the app in several ways. 在此部分的最后一个主题中,我们将引入另一个标准实体,并使用 Microsoft Flow 发送电子邮件。In the final topic for this section, we will bring in another standard entity, and use Microsoft Flow to send an email. 应用会触发流,以便在案例更新时通知创建此案例的人员。The app will trigger a flow so that the person who opened a case is notified when the case is updated. 虽然我们将在此主题中完成的是特定方案,但你所掌握的技能可应用于多种类型的应用。We're completing a particular scenario in this topic, but the skills you learn are applicable across many kinds of apps. 让我们从实体入手。Let's get started with the entities.

检查实体关系Review entity relationships

我们很快就会添加“联系人”实体,但先要检查“案例”和“联系人”实体之间的关系。We'll add the Contact entity shortly, but first we'll look at how the Case and Contact entities relate to each other. “案例”实体有一个字段是“当前联系人”,数据类型为“查找”。In the Case entity, you see that one of the fields is CurrentContact, with a data type of Lookup. 也就是说,此字段与另一个表有关联。This means that this field is used in a relationship with another table.


在“关系”选项卡上,你会发现关联的实体为“联系人”。On the Relationships tab, you see that the related entity is Contact. 请务必注意这一点,因为我们将在此主题的后面部分用到此关系。Keep that in mind because we will use this relationship later in this topic.


向应用添加实体Add an entity to the app

在 PowerApps 中添加数据源非常简单。Adding a data source in PowerApps is straightforward. 在右侧窗格中,依次单击或点击“数据源”和“添加数据源”。In the right-hand pane, click or tap Data sources, then Add data source. 在此示例中,依次选择“Common Data Service”连接和“联系人”实体。In this case, then choose the Common Data Service connection and select the Contact entity. 单击或点击“连接”后,便会将此实体添加到应用中。After you click or tap Connect, the entity is added to the app.


请注意,在此示例中,我们添加的是另一实体的数据,你可以在应用中组合多个源的数据。Note that in this example, we're adding data from another entity, but you can combine data from many sources in your apps.

查找联系人信息Look up contact information

至此,我们已有权访问应用中的“联系人”实体数据,是时候将此类数据用起来了。Now that we have access to the Contact entity data in our app, it's time to put it to use. 如引言所述,我们希望在案例更新时发送电子邮件。As mentioned in the introduction, we want to send an email when a case is updated. 为此,我们将使用两个公式和一个流。We will use two formulas and a flow to accomplish this. 第一个公式用于应用的编辑屏幕,尤其是保存按钮的 OnSelect 属性。The first formula is for the edit screen, specifically the OnSelect property of the save button.


默认情况下,当用户在窗体中编辑数据时,此按钮使用公式 SubmitForm(EditForm1) 提交更新。By default, this button uses the formula SubmitForm(EditForm1) to submit the update when a user edits data in the form. 我们需要向此公式添加内容,使其先查找当前案例创建人的联系信息,然后将联系信息存储在本地应用中:We need to add to the formula so that it first looks up the contact information for the person who opened the current case, and then stores that information locally in the app:

UpdateContext({contact:LookUp(Contact, ContactId=BrowseGallery1.Selected.CurrentContact.ContactId)}); SubmitForm(EditForm1)

这确实有点复杂,但 James 从视频的 2:04 开始更精彩详细地介绍了此公式。Yes, it's a little complex, but James does a great job of explaining this formula in more detail, starting at 2:04 in the video.

在应用中触发流Trigger a flow from the app

至此,我们已知道每个案例的联系人,现在可以向他们发送电子邮件了。Now that we know who the contact is for each case, we can send an email to them. 虽然可以直接从应用发送电子邮件,但对于此示例,我们将展示如何在应用中触发流。We could send an email directly from the app, but for this example we'll show you how to trigger a flow from the app. 这就是流,原理非常简单,即根据应用中的操作发送电子邮件。Here's the flow, which is as simple as it gets: send an email based on an action in an app. 在这里,我们不会详细介绍流,但已推出有关 Microsoft Flow 的一整套引导式系列课程。We won't get into more detail on flows here, but there is a whole Guided Learning series for Microsoft Flow.


回到应用,我们需要根据事件调用流。Back in the app, we need to call the flow based on an event. 我们将使用编辑窗体的 OnSuccess 属性,以便在编辑成功时触发流。We'll use the OnSuccess property of the edit form, so the flow is triggered when the edit succeeds. 依次单击或点击编辑窗体和功能区中的“操作” > “”。Click or tap the edit form, then on the ribbon click or tap Action > Flows. 选择要使用的流。Select the flow you want to use.


此时,流与编辑窗体的 OnSuccess 事件相关联,我们可以引用电子邮件的联系人。The flow is now associated with the OnSuccess event of the edit form, and we can refer to the contact for the email. 以下公式调用流,其中包含案例创建者的电子邮件地址、主题行和电子邮件正文。The following formula calls the flow with the email address of the person who opened the case, as well as a subject line and the body of the email.

CaseResolvedEmailConfirmation.Run(contact.EmailPrimary, "Your case has been updated", "Check it out")

有关向应用添加数据源,以及触发用于发送电子邮件的流的介绍到此结束。That's it for adding a data source to the app, and triggering a flow that sends an email. 如果尚未观看此部分中的视频,建议先观看视频。If you haven't watched the videos in this section already, we encourage you to do it. 其中详细介绍了这些主题未深入介绍的许多内容。They fill in lots of the details that we've moved through quickly in the topics.

总结Wrapping it all up

至此,你已阅读完这一部分。This brings us to the end of this section. 我们由衷希望你对此部分感到满意,并学到很多知识。We hope you've enjoyed it and learned a ton. 我们先是生成基于实体的基本应用,然后探索了应用的各组成部分。We started out generating a basic app from an entity, and explored the app a little to understand how it's put together. 我们花费了大量时间自定义应用,然后还添加了数据源并展示了如何触发流。We spent a good deal of time on customizing the app, then added a data source and saw how to trigger a flow. 虽然我们在此部分中生成的是特定案例管理应用,但你所掌握的技能可应用于多种类型的应用。We built out a specific case management app in this section, but the skills you learned could be applied to many types of apps. 正如我们在此部分开头所讲的一样,若要深入了解更复杂的案例管理应用,请务必查看适用于 Windows 的 PowerApps Studio 提供的模板。As we mentioned at the beginning of this section - if you want to dig into a more complex case management app, be sure to check out the template that's available in PowerApps Studio for Windows.

接下来,我们将继续介绍如何管理应用。Next up we'll move into managing apps. 管理部分不仅介绍了如何共享应用和进行版本控制,还介绍了环境(即应用、数据和其他资源的容器)。The management section shows you how to share and version apps, and introduces environments, which are containers for apps, data, and other resources.


你已完成“Microsoft PowerApps 引导式学习课程”中的“利用 Common Data Service 创建应用”部分。You've completed the Creating an app from the Common Data Service section of Microsoft PowerApps Guided Learning.



管理应用Managing apps


  • Michael Blythe
  • olprod