InfoPath2010_And_ExternalData

advertisement
Hands-On Lab
Accessing External Data Sources in InfoPath
2010
Lab version:
1.0.0
Last updated:
3/24/2016
CONTENTS
OVERVIEW ................................................................................................................................................... 4
EXERCISE 1: INFOPATH DATA CONNECTIONS AND UNIVERSAL DATA CONNECTION (UDC)
FILES ............................................................................................................................................................ 5
Section 1 – Create a Template Part (XTP) that receives data from an XML document ........................ 6
Section 2 – Create a Data Connection Library and Convert the XTP Data Connections to UDC ........ 17
Section 3 – Reusing the UDC in another InfoPath form...................................................................... 21
Section 4 – Reusing the XTP in another form ..................................................................................... 23
Section 5 – Publish your Form Template to a SharePoint library ....................................................... 25
EXERCISE 2: REST-ENABLING A WCF WEB SERVICE ........................................................................ 26
Task 1 – Open and Inspect the University Directory Web Service...................................................... 26
Task 2 – Modify the Web Service Interface to Specify URI Templates ............................................... 27
Task 3 – Modify web.config ................................................................................................................ 29
Exercise 1 Verification ......................................................................................................................... 31
EXERCISE 3: CREATING AN INFOPATH FORM THAT PULLS DATA FROM A REST WEB SERVICE
.................................................................................................................................................................... 32
Task 1 – Create the Data Connections ................................................................................................ 32
Task 2 – Changing REST URLs Dynamically ......................................................................................... 35
Task 3 – Display an Excel Chart ........................................................................................................... 40
Task 4 – Convert the Data Connections to UDCX ............................................................................... 42
Task 5 – Publish your Form Template to a SharePoint Library ........................................................... 44
EXERCISE 4: CONNECTING AN INFOPATH FORM TO AN ODATA PRODUCER ............................... 45
Task 1 – Create the Data Connections ................................................................................................ 46
Task 2 – Create the Form Controls...................................................................................................... 47
Task 3 – Wire Up the Drop Down Controls ......................................................................................... 51
Task 4 – Changing the OData URLs Dynamically ................................................................................. 57
Task 5 – Preview the Form .................................................................................................................. 64
Task 6 – Modify the Products Data Connection to Add an $orderby System Query Option.............. 67
Task 7 – Further Form Refinements and Learning Opportunities ...................................................... 68
Task 8 – Convert the Data Connections to UDCX ............................................................................... 71
Task 9 – Publish your Form Template to a SharePoint Library ........................................................... 72
Overview
In these lab exercises, you will discover how to use data connections in InfoPath 2010 to connect your
forms with external data sources. This is not only a powerful concept for connecting to data from an onpremises SharePoint deployment, but external data connections in the form of Universal Data
Connection (UDC) files can be used in SharePoint Online in your Office 365 tenancy as well!
Objectives
In this lab you will:

Learn to use data connections and universal data connections in InfoPath

Learn how to REST-Enable a SOAP-based WCF Service

Learn leverage REST data connections in InfoPath

Learn how to connect to OData publishers in InfoPath
System Requirements
This lab assumes that you have SharePoint Server installed in a test environment or that you have an
Office 365 tenancy with SharePoint Online. For guidance on how to setup SharePoint Server see
http://msdn.microsoft.com/en-us/library/ee554869(office.14).aspx. Note that any URL referred to in
this lab must be adjusted for use with your local setup. You must have the following items to complete
this lab:

Microsoft® Windows® Vista SP1 or Microsoft® Windows Server 2008 (64-bit)

Microsoft® SharePoint Server 2010 (64-bit) or Office 365 tenancy with SharePoint Online.

Microsoft® Office Professional Plus 2010 (32-bit or 64-bit)

Microsoft® Visual Studio 2010
Exercises
This Hands-On Lab is comprised of the following exercises:
1. InfoPath Data Connections and Universal Data Connection (UDC) Files
2. REST-enabling a WCF Web Service
3. Creating an InfoPath Form that Pulls Data from a REST Web Service
4. Connecting an InfoPath Form to an OData Producer
Estimated time to complete this lab: 60 minutes.
Starting Materials
This Hands-On Lab includes the following starting materials.

InfoPath Templates. The lab provides the following Source files/ InfoPath Templates that you
can use as starting point for the exercises. The lab instructions assume that you installed the
training kit to a location on your computer, C:[YourDownloadLocation]\Source\. Where
appropriate, each HOL exercise has a folder that contains a starter solution and a completed
solution. You can always check the solution folder if you have a question about how something
should be done.
Note: Inside the lab’s Source folder, you will find a Solution folder containing an end solution
with the completed lab exercise.
Exercise 1: InfoPath Data Connections
and Universal Data Connection (UDC)
Files
InfoPath is the bridge that connects your data with your Office 365 business projects. Using the InfoPath
Designer you can quickly create data connections to dynamically pull information into your forms.
Convert these data connections into Universal Data Connections (UDC) files and you can even reuse
them in all of your InfoPath forms. No need to recreate a data connection, just add a UDC. Best of all,
UDC files help your forms function in the browser.
To properly handle authentication for browser-based InfoPath forms, you need to use a Universal Data
Connection. After creating a data connection in the InfoPath Designer, you can easily convert it into a
UDC file using the InfoPath Designers Data Connections wizard. Placing the data connection in an
external UDC file (instead of in the form template) has the added benefit of letting you reuse the data
connection so you don’t have to remember how to recreate them for your other InfoPath forms. In
short, data connections are the bridges that connect InfoPath to the data for your online business
processes.
This MSDN article summarizes the benefits of UDCs as follows:

Form designers can configure form template data connections that will work in both the
InfoPath client and InfoPath Forms Services.

Form designers can publish a form template to multiple servers and have different data
connection settings for each server without modifying the data connection information in the
form template.

Form designers can publish a domain security form template that can access data sources in a
different domain.

Administrators can redirect data connections without modifying form templates that reference
the UDC file.

Administrators can designate which connections are approved for cross-domain access.

Administrators can publish data connections on a single server which can be shared across
multiple servers.
To get familiar with data connections, this lab steps you through creating a data connection for an
external data file used in your form. It then shows you how to convert it into a UDC file. As a bonus,
we’ll do it all in an XML Template Part (XTP). XTPs let you reuse parts of your form. Once you create an
XTP you can easily add it to another form. In addition to wrapping data source and view settings, XTPs
also remember data connections. You can add data connection to your form by simply dragging and
dropping an XTP control onto the view. Is that cool or what?
The sample scenario for this lab is scheduling classes at Contoso University. At the end of the lab you
will have created an XTP that has a UDC file connection for an XML file to retrieve a list of Colleges,
Departments and Courses. Using this XTP, you will be able to create an InfoPath form that functions
similar to a class picker form. Students can then create their own course curriculum. We’ll call our
sample InfoPath form template the “Classes” form.
Section 1 – Create a Template Part (XTP) that receives data from an XML document
Included in this lab is a sample external data file (XML) that we will call “Config XML”. Your Classes form
will use Config XML as configuration data. As its name implies, Config XML includes a list of Colleges,
Departments and Courses.
In this first section, you will upload the Config XML file to a location where users can access it, such as a
SharePoint library; and then create an XTP that retrieves data from this XML file.
1. Create a document library in SharePoint.
2. Under Library Tools > Documents tab, click Upload Document.
Figure 1
3. Upload the Config.xml file that’s included in this lab – note the URL to this XML file, e.g.
http://YourSharePointSite/YourSharePointLibrary/Config.xml.
Note: When you create an InfoPath template that uses the XML from the library, users will
need permission to the library and will need to be online to use the form with the data from
the XML.
4. Open InfoPath Designer to create a blank Template Part.
Figure 2
5. Create a data connection that retrieves data from the XML document.
a. Go to Data > Data Connections > Add and add a connection that receives data from an
XML document.
Figure 3
b. Enter the URL to the uploaded Config.xml file.
Figure 4
c. Select the option ‘Access the data from the specified location’ – we don’t want to
include the data as that would defeat the purpose of uploading to SharePoint.
Figure 5
d. Click Next (make sure that it is set to automatically retrieve data when form is opened) >
Finish > Close.
Figure 6
You should now see the new Config secondary data source in the Fields task pane:
Figure 7
6. Design your XTP:
a. Name your root node “Classes” and then add the following fields and groups into the
main data source:
Figure 8
b. Create the layout.
i.
Insert a two-column table on your canvas:
Figure 9
ii. Merge the cells in the last two rows by first selecting them, and then clicking
Merge Cells under the Layout tab:
Figure 10
iii. Adjust the table/cell sizes to your liking.
Figure 11
iv. Use “Classes” for the subheading.
v. Type in “College” for the first label.
c. Create the form controls.
i.
Drag the College field beside its label – this will place a text box for the field by
default.
ii. Right-click on the text box, select Change Control > Drop-Down List Box.
Figure 12
iii. Next, drag the Class repeating group as a repeating table in the last row of your
table.
iv. Change both the Department and the Course text boxes to drop-down list
boxes.
Your XTP should now look something similar to this:
Figure 13
7. Configure the dropdowns to get choices from the Config XML and make them cascading.
a. College dropdown
i.
Starting with the College dropdown, right-click on it and select Drop-Down List
Box Properties.
ii. In the Properties dialog, change the selection to “Get choices from an external
data source” – the Config data source will be selected by default.
iii. Click on the Select XPath icon beside Entries and then select the College node.
Figure 14
iv. Change the Value to point to @id and the Display name to point to @name.
Figure 15
v. Check the box “Show only entries with unique display names”.
b. Department dropdown
i.
Following the same steps, set the Department dropdown to get choices from the
Config data source.
ii. For the Entries, select the Department node and then click Filter Data.
Figure 16
iii. Add a filter condition to display only the data which CollegeId is equal to the
selected College in the main data source.
Figure 17
You should have this in the Entries field:
/Config/Departments/Department[@CollegeId = xdXDocument:getDOM()/my:Classes/my:College]
iv. Change the Value to point to @id and the Display name to point to @name and
only show entries with unique display names.
Figure 18
c. Course dropdown
i.
Using the same procedure, set the Course dropdown to get choices from the
Config data source, Entries being the Course node, filtered such that
DepartmentId is equal to the selected Department in the main data source:
Figure 19
ii. Change the Value to point to @id and the Display name to point to @name and
only show entries with unique display names.
8. Test in Preview mode.
Previewing the form allows you to verify the functionality, and make decisions about what you
like and what you want to change.
a. From the Home tab of the Ribbon, on the far right you should see the Preview button.
Click to preview the XTP – you’d receive an InfoPath security notice warning about
accessing the SharePoint location, click Yes.
b. Notice the Department and the Course dropdowns – since we haven’t selected a
College yet, they should be empty
c. Select a College.
Figure 20
d. Click on the Department dropdown – it should list only the departments for the selected
college.
Figure 21
e. Similarly, selecting a department will populate the Course dropdown with a list of
courses that are available only in that department.
f.
Continue inserting classes as desired:
Figure 22
9. You can go ahead and add some logic such as conditional formatting and rules where you see
fit. When you’re happy with how your XTP appears and functions, go ahead and save it.
Section 2 – Create a Data Connection Library and Convert the XTP Data Connections to UDC
In this section, we will convert the Config XML data connection we created in Section 1 to a UDC file.
Because the UDC file lives outside of the template, it lets us change the data connection without having
to republish the form template or recreate and reinsert the XTP into forms that use it. For this reason
alone, XTPs should use UDC files for all data connections. Of course, you also get browser support, and
that’s key.
1. Create the Data Connection Library.
a. Go to your SharePoint 2010 site > Site Actions > More Options
Figure 23
b. Select Data Connection Library, give it a name you want, and then click Create.
Figure 24
c. Note the URL of the data connections library you just created; it will be in the format
similar to: http://SERVER/Data Connections/ where SERVER is your actual SharePoint
server.
2. Convert your XTP data connection to UDC.
a. Back in your XTP in InfoPath Designer, go to Data > Data Connections.
b. Click on the Convert to Connection File button.
Figure 25
c. Enter the URL of the data connection file to be created, e.g.
Figure 26
d. Save your changes to the XTP.
3. Approve the UDC file in SharePoint.
a. Go to your Data Connections Library in SharePoint and locate the new UDCX file that got
added when you converted your data connection earlier. Notice that it is in a Pending
state:
Figure 27
b. Click on the down arrow key and select Approve/Reject.
Figure 28
c. Select Approve in the dialog that appears, and then click OK.
Figure 29
Section 3 – Reusing the UDC in another InfoPath form
Now, let’s see how we can easily reuse this UDC file to simplify creation of a new InfoPath form
template.
1. Open InfoPath Designer 2010, click Blank Form, and then click Design Form.
Note: suppose you want to use a data connection file to specify the main data source for your
form template? InfoPath only lets you do this for Web service connections. To specify the main
data source from one of these kinds of connections, click Data Connection File instead of
Blank Form, and then proceed to Step 3.
2. On the Data menu, click Data Connections, and then click Add.
3. In the Data Connection Wizard, click Search for connections on a Microsoft Office SharePoint
Server, and then click Next.
Figure 30
4. If the site where you created your data connection library is not listed in the Site drop-down list,
click Manage Sites, and then click Add to add it to the list.
5. Select the site where you created your data connection library in the Site drop-down list,
expand the name of the data connection library, select the Config data connection, and then
click Next.
Figure 31
6. Complete configuring the data connection in the Data Connection Wizard as required – here
again, we’d like to access data from the specified location; click Next > Finish > Close.
7. Test that your data returns by first dragging the Config secondary data source’s nodes College,
Department and Course as repeating tables on your canvas (to avoid confusion, you may delete
the last columns of each table, like so):
Figure 32
8. Preview your form and verify that the data is being returned correctly.
Section 4 – Reusing the XTP in another form
Using an XTP to add a data connection is even easier, but keep in mind that it will add more than just the
data connection. The XTP adds groups and fields added your data source, controls to your view, and any
rules that you associated with them. In most cases, that’s exactly what you want, but if not, you can
always delete them and use the XTP just to quickly add a data connection. 
1. Open InfoPath Designer 2010, click Blank Form, and then click Design Form.
2. In the Controls section under the Home tab, click Add or Remove Custom Controls.
Figure 33
3. Click Add, select Template Part and click Next.
4. Browse to the location where you saved the XTP you’ve created earlier, and click Finish, then
Close.
5. The XTP should now appear in the list of Custom Controls – click on it to add it on your form.
Figure 34
Note: if you don’t like the default puzzle icon for your newly created XTP, you can customize it
to be your own icon. Just open the XTP up in the InfoPath Designer and go to File-> Template
Part Properties->Template Part Icon.
The layout and controls will appear on your canvas:
Figure 35
Groups and fields should be added into your main and secondary data sources:
Figure 36
In Data > Data Connections, you will also see the UDC file connection to the Config XML file
located in SharePoint:
Figure 37
6. Preview your form and verify that it works the same way the XTP does, populating the
dropdowns with data based on the selected values.
That’s it! If this were a real form we would probably want to add fields for the student’s contact
information and a button to submit the form to a SharePoint library.
Section 5 – Publish your Form Template to a SharePoint library
To share your form with others, the final step is publishing it to a location where those users have
access, such as a SharePoint form library.
1. Go to File > Publish and then select SharePoint Server.
Figure 38
2. Enter the URL to your SharePoint site.
3. Leave the default to Form Library and make sure that it is browser-enabled, then click Next.
Figure 39
4. If you already have an existing SharePoint library where you’ve set your forms to submit to,
select ‘Update existing form library’ and select your SharePoint library from the list. Otherwise,
create a new one and give it a name of your choosing.
5. Click click Next twice, click Publish, and then click Close.
After you have published, verify that your form works in the browser by clicking new to create a form in
your SharePoint library.
Exercise 2: REST-enabling a WCF Web
Service
In this exercise, you will modify a WCF web service to change it from a SOAP web service to a REST web
service. This will allow dynamically retrieving data from the web service simply by changing the request
URL, and without the need for SOAP protocols.
Requirements
This lab requires the following:

That you have Visual Studio 2010 installed on your machine

That you have installed the Windows Azure SDK and Windows Azure Tools for Microsoft Visual
studio, which can be obtained via the following link:
Windows Azure SDK and Windows Azure Tools for Microsoft Visual Studio
Task 1 – Open and Inspect the University Directory Web Service
In this task, you will open the source for a SOAP-based web service in Visual Studio, and have a look at
the web methods it provides. In Tasks 2 and 3, you will further modify this web service to be a RESTbased service.
1. Open Visual Studio 2010.
2. Open the Contoso University web service solution
a. From the File menu in Visual Studio, select Open  Project/Solution…
b. Navigate to the location of the Contoso University Web Service’s source and doubleclick the ContosoUniversity.sln file.
3. Inspect the current web service interface.
a. If the Solution Explorer pane is not already visible, show it by selecting Solution
Explorer from the View menu.
b. In the solution explorer, expand the UniversityDirectory project if it is not already
expanded, and double-click IUniversityDirectory.cs to open it in the code viewer.
c. In the IUniversityDirectory interface, defined between lines 17 and 35, you can see
seven methods declared:
Figure 40
Each of these declares a web method in the University Directory web service. Note that four of
these methods accept parameters.
Task 2 – Modify the Web Service Interface to Specify URI Templates
The next step is to indicate which URL formats should invoke which web methods. As demonstrated in
Step 3c of Task 1, there are seven different methods, and the URL formats we will be using to access
each of them are as follows:
GetColleges() - /Colleges
GetDepartments() - /Departments
GetCollegeDepartments() - /Colleges({collegeID})/Departments
GetProfessors() - /Professors
GetDepartmentProfessors() - /Departments({departmentID})/Professors
GetDepartmentCourses() - /Departments({departmentID})/Courses
GetProfessorCourses() - /Professors({professorName})/Courses
Note: Methods that take an input parameter have a parameter of the same name indicated within the
URI format.
1. In Visual Studio’s Solution Explorer, double-click the IUniversityDirectory.cs file to open it in
the code editor again, if it is not still open.
2. Immediately before the declaration of the GetColleges() method, add the following attribute:
C#
[WebGet(UriTemplate = "/Colleges", ResponseFormat = WebMessageFormat.Xml)]
a. After doing so, the code should look like this:
Figure 41
b. Here, the value specified for the UriTemplate parameter indicates the URL format
that should correspond to the web method, and specifying WebMessageFormat.Xml
for the ResponseFormat parameter indicates that results should be returned as
XML.
3. Repeat Step 2 for each of the six remaining web methods, using the appropriate
UriTemplate value for each one, as indicated at the beginning of this task.
Note: The parameters in curly braces must have the same spelling and capitalization as the
corresponding method parameters.
When you are finished, the interface should look like this:
Figure 3
4. Save the changes to IUniversityDirectory.cs.
Task 3 – Modify web.config
In this task, you will modify the web service’s web.config file to indicate that the service is a REST-based
service, and modify the set of allowable URL characters.
1. In Visual Studio’s Solution Explorer pane, double click the Web.config file to open it in the code
editor.
2. After the <serviceBehaviors> node at the location
/configuration/system.serviceModel/behaviors/serviceBehaviors, add the following node:
web.config
<endpointBehaviors>
<behavior name="REST">
<webHttp/>
</behavior>
</endpointBehaviors>
This instructs the web service to act as a REST web service. After this modification, the whole
<behaviors> node should look like this:
Figure 4
3. After the <behaviors> node that you modified in step 2, and before the
<serviceHostingEnvironment> node, add the following node:
web.config
<services>
<service name="UniversityDirectory.UniversityDirectory">
<endpoint address="/" binding="webHttpBinding"
contract="UniversityDirectory.IUniversityDirectory" behaviorConfiguration="REST" />
</service>
</services>
This instructs the service host to use the UniversityDirectory.IUniversityDirectory interface as
the definition of the interface for its web service.
4. In the <system.web> node, add the following node after the <compilation> node: node:
web.config
<httpRuntime requestPathInvalidCharacters="<,>,*,%,:,\,?" />
By default, WCF services restrict against requests that contain the characters <, >, *, %, &, :, \,
and ? in the URL. Since some university departments contain & in their title, we can’t restrict
against this character, so we’ll use this setting to indicate that all of the default restricted
characters except & should be restricted. service.
5. Save the Web.config file changes.
Exercise 1 Verification
In order to verify that you have correctly performed all steps of Exercise 1, proceed as follows:
Test out the modified web service
It’s now time to run the web service you’ve modified and see how it works. In this task, you’ll be
accessing the URLs provided by this service to first get values from a method that doesn’t require any
input parameters, and then use those values as input to another method.
1. Run the web service.
a. In Visual Studio’s Solution Explorer pane, right click the header for the
UniversityDirectory project, and select Debug  Start new instance
b. This should open a new browser window located at http://localhost:port#/, where
port# is some number chosen by Visual Studio.
c. The service name is UniversityDirectory.svc, so all URLs will be relative to
http://localhost:port#/UniversityDirectory.svc/
2. List all colleges
a. In your browser, navigate to http://localhost:port#/UniversityDirectory.svc/Colleges
b. Your browser should display XML containing an ID and name for five different
colleges.
Figure 5
c. Make note of one college ID from this list, to use as an input value in step 3.
3. List all departments in a college
a. Using the college ID selected in step 2, navigate to
http://localhost:port#/UniversityDirectory.svc/Colleges(ID)/Departments in your
browser.
b. The browser should display XML data describing all of the departments in the
specified college, and none from any of the other colleges.
4. Carry out similar actions to steps 2 and 3 to test out all of the remaining web methods:
a. List all departments in the system by navigating to /Departments, and then use
/Departments(ID)/Professors and /Departments(ID)/Courses to list all of the
professors and courses in one of those departments.
b. List all professors in the system by navigating to /Professors, and then use
/Professors(Name)/Courses to list all of the courses that that professor teaches.
5. Finish this lab.
a. Close the browser window that you used for testing the web service.
b. Close Visual Studio.
By now, you should have learned how to REST-enable a WCF web service by simply defining access URL
formats and modifying web.config. You may now use this knowledge in creating more intuitive solutions
with simpler, wider-support protocol that requires lower bandwidth and is easier to troubleshoot.
Exercise 3: Creating an InfoPath Form
that Pulls Data from a REST Web Service
In this exercise you will create a sample InfoPath form that queries the REST Web Services in the
Contoso University directory to return data such as a list of all the colleges, departments, professors,
etc. You can expand this sample form as desired and come up with a form that functions like a Course
Finder.
Task 1 – Create the Data Connections
In this first task, you will add the REST Web Service data connections specifying URLs that return XML
documents listing the data you’d want to return.
1. Open InfoPath Designer and design a blank form template.
2. Switch to the Data tab on the ribbon and then click Data Connections.
3. Add the REST Web Service data connections.
a. List all colleges:
i.
In the Data Connections window, click Add.
ii. Keep the default selection to create a new connection to receive
data, and then click Next.
Figure 1
iii. Select REST Web Service and click Next.
Figure 2
iv. Enter the REST Web Service URL that returns an XML document
listing all the colleges in Contoso University: http://[yourservice].cloudapp.net/UniversityDirectory.svc/Colleges
Figure 3
v. Click Next, name your data connection RESTColleges, let it
automatically retrieve data on form open, and then click Finish.
Figure 4
b. List all departments
i.
Add another data connection that receives data from a REST web
service via the URL: http://[yourservice].cloudapp.net/UniversityDirectory.svc/Departments
ii. Name this data connection RESTDepartments and uncheck the box
‘Automatically retrieve data when form is opened’.
Figure 5
c. List all professors
i.
Add another data connection that receives data from a REST web
service via the URL: http://[yourservice].cloudapp.net/UniversityDirectory.svc/Professors
ii. Name this data connection RESTProfessors and uncheck the box
‘Automatically retrieve data when form is opened’.
The next data connections we will be adding will all use temporary input values in the URL in
order to return the XML document. We will be changing these URLs using rules later.
d. List departments in a college
i.
Add another data connection that receives data from a REST web
service via the URL: http://[yourservice].cloudapp.net/UniversityDirectory.svc/Colleges(A&S)/Depar
tments
Note: “A&S” is one of the college IDs.
ii. Name this data connection RESTDepartmentsInCollege and uncheck
the box ‘Automatically retrieve data when form is opened’.
e. List courses for a department
i.
Add another data connection that receives data from a REST web
service via the URL: http://[yourservice].cloudapp.net/UniversityDirectory.svc/Departments(ANTH)
/Courses
Note: “ANTH” is one of the Department IDs .
ii. Name this data connection RESTCoursesForDepartment and uncheck
the box ‘Automatically retrieve data when form is opened’.
f.
List courses for a professor
i.
Add another data connection that receives data from a REST web
service via the URL: http://[yourservice].cloudapp.net/UniversityDirectory.svc/Professors(Simpson,
David)/Courses
Note: “Simpson, David” is one of the Professor names.
ii. Name this data connection RESTCoursesForProfessor and uncheck the
box ‘Automatically retrieve data when form is opened’.
g. List professors in a department
i.
Add another data connection that receives data from a REST web
service via the URL: http://[yourservice].cloudapp.net/UniversityDirectory.svc/Departments(ANTH)
/Professors
Note: “ANTH” is one of the Department IDs.
ii. Name this data connection RESTProfessorsInDepartment and
uncheck the box ‘Automatically retrieve data when form is opened’.
We are done adding the data connections. You may now close the Data Connections window.
Task 2 – Changing REST URLs Dynamically
When adding rules in an InfoPath form that uses REST Web Service data connections, there would be a
new action in the list called ‘Change REST URL’.
Figure 6
This allows you to specify the parameters to be used for querying data. In this task, we will add rules
that will dynamically change these parameters and then query the data connection accordingly.
1. In the Fields taskpane, switch to the RESTColleges (Secondary) data source.
2. Drag the College node as a repeating table on your form canvas.
3. Insert another column to the right of the repeating table.
4. Add a button inside the third column’s cell and label it ‘Get Departments’.
Figure 7
5. With the button selected, click on Manage Rules in the Rules menu to display the Rules
taskpane.
Figure 8
6. In the Rules taskpane, click New > Action, and then Add > Change REST URL.
7. In the Rule Details window, select the RESTDepartmentsInCollege data connection and then
click on the fx button.
8. Enter the formula: concat("http://[yourservice].cloudapp.net/UniversityDirectory.svc/Colleges(", ID, ")/Departments")
Figure 9
Note: ID is a child of the College repeating group.
Figure 10
9. Click OK. Your rule action should be similar to this:
Figure 11
10. Click OK once more to accept and close the dialog.
11. In the same rule set, click Add > Query for Data and select the RESTDepartmentsInCollege data
connection.
Figure 12
12. Click OK to accept and close the dialog.
Your rule set should be similar to this:
Figure 13
13. To display the returned data on the canvas, first switch to the RESTDepartmentsInCollege
(Secondary) data source in the Fields taskpane, and then drag the Department group as a
repeating table on your form canvas.
When you preview your form, you should first see a list of all the colleges in Contoso University. Clicking
on any of the ‘Get Departments’ buttons will return a list of the departments in the corresponding
college, like such:
Figure 14
You may continue doing this process to return data for the other REST Web Services that we have added
in Task 1.
Task 3 – Display an Excel Chart
In this task, we will create a spreadsheet with a chart that we can use to graphically represent some of
our form data.
Create a new Excel Workbook and add some dummy data and a chart that displays that data:
Figure 15
1. Make a note of your sheet name and the cells with your data, and then upload your Workbook
to a SharePoint Library with Excel Services enabled.
2. The URL to your file will look something like this:
http://<ServerName>/<DocumentLibrary>/<FileName>.xslx
3. The REST URL to the Excel REST Service model looks like this:
http://<ServerName>/_vti_bin/ExcelRest.aspx/<DocumentLibrary> /<FileName>.xlsx/model
Verify your URL is correct in your browser:
Figure 16
4. Click on Charts to see the name of your chart:
Figure 17
In your form, add a picture control to your template and set the control to read only under the control
properties:
Figure 18
5. Set the default value for the control to pass in values from the RESTDepartments data
connection.
a. The URL to the chart will look like:
http://<ServerName>/_vti_bin/ExcelRest.aspx/<DocumentLibrary>/<FileName>.xlsx/
model/Charts('Chart 1')?$format=image
b. We can pass in a range to set values, like this:
Ranges(‘Sheet!A1’)=1
c. Given that information, we can use a formula in the field for our picture to pass in
values for generating our chart. The formula will look something like this:
d. Preview your form – you may need to set your RESTDepartments data connection to
return data on load, or add a button that you can use to execute the query to ensure
you have data in that data source. Your chart should display the counts of the
departments you passed in:
Figure 19
e. If you add the secondary data source to your form canvas, you can experiment with
changing the college for some departments and see the chart change dynamically.
More information on the Excel Services REST API can be found here.
Task 4 – Convert the Data Connections to UDCX
In this task, we will convert the REST data connections to UDCX files that we can easily configure when
we plan to make changes to the connection at some point.
Figure 20
1. If you have not done so, create the data connection library.
a. Go to your Office 365 site > Site Actions > More Options
b. Select Data Connection Library, give it a name you want, and then click Create.
c. Note the URL of the data connections library you just created; it will be in the format
similar to: http://SERVER/Data Connections/ where SERVER is your actual O365 server.
2. Convert your REST data connections to UDCX files.
a. Back in your XSN in InfoPath Designer, go to Data > Data Connections.
b. Click on the Convert to Connection File button.
c. Enter the URL of the data connection file to be created, e.g.
Figure 421
d. Convert all other REST data connections to UDCX.
e. Save your changes to the XSN.
3. Approve the UDC file in O365.
a. Go to your Data Connections Library in O365 and locate the new UDCX files that got
added when you converted your data connections earlier. Notice that they are in a
Pending state.
b. Click on the down arrow key and select Approve/Reject.
Figure 22
c. Select Approve in the dialog that appears, and then click OK.
Figure 23
d. Approve all the other UDCX data connections.
In a real-world scenario, for example if you wish to create a form that functions like a Course Finder, you
would want to add fields in your main data source that will store the values such as the college,
department, course, professor, etc. You may then use these fields as parameters to query your REST
Web Services with.
You may continue customizing your form template however you like. For example, you might want to
add a submit functionality that will submit the form to a SharePoint library.
Task 5 – Publish your Form Template to a SharePoint Library
In order to access the form template and submit forms, you need to publish it to a location where users
can access it from, such as a SharePoint form library.
1. Go to File > Publish and then select SharePoint Server.
Figure 24
2. Enter the URL to your SharePoint site.
3. Leave the default to Form Library and make sure that it is browser-enabled, then click Next.
Figure 25
4. If you already have an existing SharePoint library where you’ve set your forms to submit to,
select ‘Update existing form library’ and select your SharePoint library from the list. Otherwise,
create a new one and give it a name of your choosing.
5. Click click Next twice, click Publish, and then click Close.
When done publishing, verify that your form works in the browser by creating a new form in your
SharePoint library. Check that the functionality works correctly, and that the REST data connections
successfully return data.
Exercise 4: Connecting an InfoPath Form
to an OData Producer
In this exercise, you will create a sample InfoPath Supplier Information form that connects to an OData
producer and pulls in data. Using REST Web Service data connections, we will return a catalog of data
and use filtering to return successive subsets based on dropdown field selections in the form, also
known as “cascading filtering”. This will reduce the amount of data being queried from our form, which
in turn will reduce load and wait times for our users. This also allows users fewer items to select from in
dropdown lists, providing them with a better experience.
Task 1 – Create the Data Connections
In this task, you will connect to REST Web Service data connections specifying the base URLs to return
data.
1. Open InfoPath Designer and design a blank form template
2. Switch to the Data tab on the ribbon.
3. Add the REST Web Service data connections.
a. Return categories:
i.
From the Data tab in the ribbon, expand the From Web Service menu
and select From REST Web Service
Figure 43
ii. Enter the REST Web Service URL that returns an XML document
listing all the categories in the OData zNorthwind service. We are
using a $select System Query Option to return only the fields we
need:
http://services.odata.org/Northwind/Northwind.svc/Categories?$sel
ect=CategoryID,CategoryName
iii. Click Next. Name your data connection Categories and leave the
checkbox that automatically retrieves data on open selected. Click
Finish:
Figure 44
b. Return products for a category:
i.
Add another REST Web Service data connection, using the URL:
http://services.odata.org/Northwind/Northwind.svc/Categories(1)/Pr
oducts?$select=ProductID,ProductName
This returns all products for the first category. The category key of
(1) is a placeholder, as we will change the URL via rules later.
ii. Name this data connection Products. Uncheck the box ‘Automatically
retrieve data when form is opened’.
c. Return the supplier for a product:
i.
Add another REST Web Service data connection, using the URL:
http://services.odata.org/Northwind/Northwind.svc/Products(1)/Sup
plier
This the supplier for a product. The product key of (1) is a
placeholder, as we will change the URL via rules later. We’ll return all
fields for supplier.
ii. Name this data connection Supplier. Uncheck the box ‘Automatically
retrieve data when form is opened’.
Task 2 – Create the Form Controls
In this task, you will create form controls that will be used for cascading drop downs
1. From the Insert tab in the Ribbon, select a two column table:
Figure 45
2. Merge the cells in the bottom row.
a. Select the cells, then select the Table Tools Layout tab on the ribbon:
Figure 46
b. Click Merge Cells.
3. Add two labels, one for Category and one for Product, in the first column:
Figure 47
4. From the Home tab on the ribbon, add two drop down controls in the second column:
Figure 48
5. In the properties for the first drop down, rename the field “Category”:
Figure 49
6. In the properties for the second drop down, rename the field “Product”:
Figure 50
7. Your form data source should now look like this:
Figure 51
8. Change the drop down in the fields task pane to display your Supplier data source:
Figure 52
9. With your cursor in the third row of your table, right click on the entry group in the Supplier
data source and select Section:
Figure 53
10. Your form will now look something like:
Figure 54
11. From the Supplier data source, expand the content group so the m:properties group is visible:
Figure 55
12. From the m:properties group, drag the d:SupplierID, d:CompanyName, and d:Phone nodes into
the section created in step 9:
Figure 56
Task 3 – Wire Up the Drop Down Controls
In this task, you will set the drop down controls to use secondary data sources for their values
1. Set up the Category drop down:
a. Right-click your Category drop down and select Drop-Down List Box Properties
b. On the Data tab, in the List box choices section, select ‘Get choices from an external
data source’:
Figure 57
c. Select the Categories data source:
Figure 58
d. Click the Select XPath button next to the Entries field:
Figure 59
e. Select the m:properties group:
Figure 60
f.
Leave the Value field as is, with the d:CategoryID field selected:
Figure 61
g. For the Display Name field, click Select XPath and select the d:CategoryName field:
Figure 62
h. Check the box ‘Show only entries with unique display names’.
Your properties for your Category drop down will now look like:
Figure 63
2. Set up the Product drop down:
a. Right-click your Product drop down and select Drop-Down List Box Properties
b. On the Data tab, in the List box choices section, select ‘Get choices from an external
data source’:
Figure 64
c. Select the Products data source:
Figure 65
d. Click the Select XPath button next to the Entries field:
Figure 66
e. Select the m:properties group:
Figure 67
f.
Leave the Value field as is, with the d:ProductID field selected:
Figure 68
g. For the Display Name field, click Select XPath and select the d:ProductName field:
Figure 69
h. Check the box ‘Show only entries with unique display names’.
Your properties for your Product drop down will now look like:
Figure 70
Task 4 – Changing the OData URLs Dynamically
As in Exercise 2, we will be changing our URLs to return only the data we need. Since you have added
REST data connections to your form to return data from the OData provider, you’ll find a new rule action
of ‘Change REST URL’ available to you:
Figure 71
Action rule that will allow you to dynamically change the URLs for the data connections to add filtering.
Rules will be added to both the Category and the Product field.
1. Add a rule to the Category field:
a. In the Fields task pane, click on the Category field:
Figure 72
b. From the Home tab on the Ribbon, select Manage Rules:
Figure 73
c. In the Rules taskpane, click New > Action.
d. Name the rule ‘Get Products in Category’:
Figure 74
e. Add an action to Change REST URL:
Figure 75
f.
Select Products for the Data Connection:
Figure 76
g. Click the fx button next to the REST Web Service URL field to open the formula
editor:
Figure 77
h. You will use a concat formula to pass in the value of the currently selected category:
i.
Paste the following into the formula editor:
concat("http://services.odata.org/Northwind/Northwind.svc/Categ
ories(", ., ")/Products?$select=ProductID,ProductName")
The first string in the concat formula is the path to the Categories
followed by an open parenthesis. The second string in the concat
formula is the XPath to the current node (in this case, our Category
node, which will have the value of a CategoryId), which is the
KeyPredicate for the Categories. The third string is the closing
parenthesis for the KeyPredicate and the rest of the path to the
Products, including our $select System Query Option.
ii. The final rule will look like:
Figure 78
i.
Add a second action to query the Products data connection:
Figure 79
Figure 80
j.
Your completed rule will look like this:
Figure 81
2. Add a rule to the Product field:
a. Copy the rule you just created for the Category field:
Figure 82
b. Click on the Product field in the Fields task pane:
Figure 83
c. And click on the clipboard icon to paste your rule:
Figure 84
d. Change the name to ‘Get Supplier for Product’:
Figure 85
e. Modify the first action to change the REST URL for the Supplier data source:
Figure 86
f.
And paste the following into the formula editor for the REST Web Service URL field:
concat("http://services.odata.org/Northwind/Northwind.svc/Products(", .,
")/Supplier")
Since we are now setting a rule on the Product field, we are passing in the value of
the selected Product ID as the KeyPredicate for the Products in order to return the
Supplier.
g. This action should look like:
Figure 87
h. Modify the second action to query the Supplier data source:
Figure 88
3. Your completed rule will look like:
Figure 89
Task 5 – Preview the Form
Previewing the form allows you to verify the functionality, and make decisions about what you like and
what you want to change.
1. From the Home tab of the Ribbon, on the far right you should see the Preview button. Click to
preview the form:
Figure 90
2. You will probably see a security warning about connecting to data sources. Verify that the data
source is one you wish to connect to (http://services.odata.org), and click Yes.
Figure 91
3. Click the Product drop down. Since we haven’t selected a Category yet, it should be empty:
Figure 92
4. The section with Supplier information should not be displayed:
Figure 93
5. Select a Category:
Figure 94
6. Depending on the speed of your connection, you may see a dialog showing the URL that is being
used for the Products data connection:
Figure 95
7. Click on the Product drop down:
Figure 96
8. Upon selecting a Product, the Supplier information will be displayed:
Figure 97
Task 6 – Modify the Products Data Connection to Add an $orderby System Query Option
On previewing the form, you may have noticed that the Products are not alphabetized:
Figure 98
The OData URI conventions provide a way of returning sorted data that can be implemented in your
form.
1. From the Fields task pane, click on the Category node. If the Rules task pane is not showing,
select Manage Rules from the Home tab on the Ribbon to display it.
2. Modify the first rule action that is changing the REST URL. Open the formula editor for the REST
Web Service URL field and add the following to the concat formula after ProductName:
&$orderby=ProductName
The formula should look like:
Figure 99
3. Save your changes and preview the form again. Select a Category, and the Products drop down
should now be ordered by Product Name ascending:
Figure 100
Task 7 – Further Form Refinements and Learning Opportunities
You may have noticed on previewing your form that if you change Category after selecting a Product,
your form will display a Product ID in the Product dropdown, and the Supplier information still shows:
Figure 101
This can be addressed by clearing the Product field every time the Category field changes and hiding the
supplier information if the Product field is blank.
1. Add one more action to the rule on the Category field that sets the Product field to blank:
Figure 102
2. Your rule now looks like:
Figure 103
3. Modify the rule on the Product field to not execute if Product is blank, as the data connection
will fail with a blank KeyPredicate:
Figure 104
4. Add a conditional formatting rule to the section with the supplier information:
Figure 105
With the condition that if the Product field in the Main data source is blank, the section should
be hidden.
5. Preview your form again, and try changing categories after having selected a product.
In Task 6, we took advantage of the $orderby System Query Option. There are a number of other very
valuable System Query Options available. http://www.odata.org/developers/protocols/uri-conventions
has more information.
These can be used to filter and sort data, and there are even functions available that can be used for
such things as converting data to upper or lower case prior to comparison. This gives you even more
control over the quantity and relevance of the data you are pulling into your form, and since you can
modify REST web service URLs in rules, you can use form values to determine what data to return.
Task 8 – Convert the Data Connections to UDCX
In this task, we will convert the REST data connections to UDCX files that we can easily configure when
we plan to make changes to the connection at some point.
Figure 64
1. If you have not done so, create the data connection library.
a. Go to your Office 365 site > Site Actions > More Options
b. Select Data Connection Library, give it a name you want, and then click Create.
c. Note the URL of the data connections library you just created; it will be in the format
similar to: http://SERVER/Data Connections/ where SERVER is your actual O365 server.
2. Convert your REST data connections to UDCX files.
a. Back in your XSN in InfoPath Designer, go to Data > Data Connections.
b. Click on the Convert to Connection File button.
c. Enter the URL of the data connection file to be created, e.g.
Figure 65
d. Convert all other REST data connections to UDCX.
e. Save your changes to the XSN.
3. Approve the UDC file in O365.
a. Go to your Data Connections Library in O365 and locate the new UDCX files that got
added when you converted your data connections earlier. Notice that they are in a
Pending state.
b. Click on the down arrow key and select Approve/Reject.
Figure 66
c. Select Approve in the dialog that appears, and then click OK.
Figure 67
d. Approve all the other UDCX data connections.
You may continue customizing your form template however you like. For example, you might want to
add a submit functionality that will submit the form to a SharePoint library.
Task 9 – Publish your Form Template to a SharePoint Library
In order to access the form template and submit forms, you need to publish it to a location where users
can access it from, such as a SharePoint form library.
1. Go to File > Publish and then select SharePoint Server.
Figure 68
2. Enter the URL to your SharePoint site.
3. Leave the default to Form Library and make sure that it is browser-enabled, then click Next.
Figure 106
4. If you already have an existing SharePoint library where you’ve set your forms to submit to,
select ‘Update existing form library’ and select your SharePoint library from the list. Otherwise,
create a new one and give it a name of your choosing.
5. Click click Next twice, click Publish, and then click Close.
When done publishing, verify that your form works in the browser by creating a new form in
your SharePoint library. Check that the functionality works correctly, and that the REST data
connections successfully return data.
Download