Exercises

advertisement
Hands-On Lab
Business Connectivity Services
Lab version:
1.0.0
Last updated:
3/24/2016
Contents
OVERVIEW ................................................................................................................................................... 3
EXERCISE 1: CREATING EXTERNAL CONTENT TYPES WITH SHAREPOINT DESIGNER 2010 ........ 4
Task 1 – Define the External Content Type ........................................................................................... 4
Task 2 – Create an External List ............................................................................................................ 9
Exercise 1 Verification ........................................................................................................................... 9
EXERCISE 2: CREATING EXTERNAL CONTENT TYPES WITH VISUAL STUDIO 2010 ...................... 12
Task 1 – Create the Visual Studio 2010 project .................................................................................. 12
Task 2 – Define the External Data Model describing the data............................................................ 14
Task 3 – Implement the Customer data source object ....................................................................... 22
Task 4 – Create the External Content Type and External List ............................................................. 26
Exercise 2 Verification ......................................................................................................................... 27
EXERCISE 3: WORKING WITH BCS DATA OFFLINE ............................................................................ 28
Task 1 – Take the Customers list offline ............................................................................................. 28
Task 2 – Update the Outlook Form Region ......................................................................................... 28
Exercise 3 Verification ......................................................................................................................... 33
SUMMARY .................................................................................................................................................. 34
Overview
In this lab exercise, you will explore the use of Business Connectivity Services to connect external data to
SharePoint. You will use two different tools, SharePoint Designer 2010 and Visual Studio 2010 to build
the definitions necessary to connect SharePoint to external data. Once the connections are established,
you will extend a simple Outlook Form Region to use the external data offline using a SharePoint
Workspace 2010 cache.
Objectives
In this lab you will:

Learn to create External Content Types in SharePoint Designer 2010

Learn to create External Content Types in Visual Studio 2010

Learn to use the BCS offline data caching API to access external data
System Requirements
This lab assumes that you have SharePoint Server installed in a test environment. 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)

Microsoft® Office Professional Plus 2010 (32-bit or 64-bit)

Microsoft® Visual Studio 2010
Setup
You must perform the following steps to prepare your computer for this lab. This consists primarily of
creating a SharePoint site collection at http://intranet.contoso.com/sites/BCSOffice and installing the
code snippets for this lab.
1. Run the command file Setup.bat located at
%Office2010DeveloperTrainingKitPath%\Labs\BCSOffice\Source\.
Exercises
This Hands-On Lab is comprised of one exercise with the following tasks:
1. Creating External Content Types with SharePoint Designer 2010
2. Creating External Content Types with Visual Studio 2010
3. Working with BCS Data Offline
Estimated time to complete this lab: 60 minutes.
Starting Materials
This Hands-On Lab includes the following starting materials.

Visual Studio solutions. The lab provides the following Visual Studio solutions that you can use
as starting point for the exercises. Lab instructions will reference the Training Kit location after
installation as %Office2010DeveloperTrainingKitPath%.
◦
%Office2010DeveloperTrainingKitPath%\Labs\BCSOffice\[language]\Source\Starter\O
utlookFormRegion\OutlookFormRegion.sln: This is an Outlook form region project used
in Exercise 3 that will retrieve its Customers list from an External Content Type created
in Exercise 2.
Note: Inside the lab’s Source folder, you will find a Solution folder containing an end solution
with the completed lab exercise.
Exercise 1: Creating External Content
Types with SharePoint Designer 2010
In this task you will connect to the customers list in the Adventure Works database. You will start by
defining the External Content Type in SharePoint Designer 2010 and finish by creating a new External
List using the External Content Type.
Task 1 – Define the External Content Type
In this task, you will create a new External Content Type using SharePoint Designer 2010.
1. Open SharePoint Designer and load the existing External Content Types.
a. In Internet Explorer navigate to http://intranet.contoso.com/sites/BCSOffice
b. Click Site Actions -> Edit Site in SharePoint Designer
c. In SharePoint Designer locate the Navigation pane and click External Content Types
Figure 1
External Content Types List
2. Create a new Customer External Content Type
a. Click the External Content Type in the New group in the External Content Types
ribbon tab.
b. Click the Name link and rename the External Content Type to Customer
c. Click the link by the External System label to define the external data source
d. In the Operation Designer, click the Add Connection button
Figure 2
Define Data Source Window
e. In the External Data Source Type Selection dialog, choose SQL Server and click OK
f.
In the Database Server text box enter demo2010a
g. In the Database Name text box enter AdventureWorksLT2008
h. In the Name text box enter AdventureWorks Lite
i.
Click OK to create the data source
Figure 3
Define SQL Server Connection
3. Add the read and write operations to the new External Content Type
a. In the Data Source Explorer tab, expand AdventureWorks Lite and locate the
Customer table.
b. Right click the Customer table and select Create All Operations
Figure 4
Create All Operations
c. In the All operations dialog, click Next to move to the first step
Note: This is the point where you will define the relationship between the data in the
SQL database and the operations SharePoint needs to manage the external data.
d. In the Parameters Configuration page wizard page select the Company Name item
e. Once the Company Name item is select, check the Show In Picker check box
Figure 5
Parameters Configuration
f.
Click Next to move to the next page
Note: Applying a filter of type Limit will ensure performance by limiting the amount of
data SharePoint deals with on each read.
g. Click the Add Filter Parameter button
h. In the Properties section, click the Click to Add link.
Figure 6
Filter Configuration
i.
In the Filter Configuration dialog, choose a Filter Type of Limit and click OK
Figure 7
Limit Filter Configuration
j.
In the Default Value control in the Properties section, enter 100
k. Click Finish to complete the operations definition
4. Save the new Customer External Content Type
a. In the Quick Access Toolbar click the Save button
b. Click the Summary View button in the External Content Types ribbon tab
Figure 8
Completed Customer External Content Type
Task 2 – Create an External List
In this task, you will use the External Content Type from the previous task to create a new External List
that will provide access to the database information from within SharePoint.
1. Create the new External List using the Customer External Content Type.
a. Click the Create Lists & Form button in the External Content Types ribbon tab
b. In the List Item text box enter External Customers
Figure 9
Create External List
c. Click OK to create the new list
Exercise 1 Verification
In order to verify that you have correctly performed all steps of exercise 1, proceed as follows:
Test the External Content Type and External List
Test the External Content Type and External List by accessing the list in SharePoint and updating an item
in the database.
1. Verify the External Customers list is available in the SharePoint site
a. In Internet Explorer navigate to http://intranet.contoso.com/sites/BCSOffice
b. Click the External Customers link in the Navigation bar
c. Verify the customers are visible in the list page
Figure 10
External Customers List
2. Edit one of the customers to verify the Update operation works
a. Select the first item in the list
b. Click the drop down arrow on the item and select Edit Item
Figure 11
Edit Menu Option
c. In the dialog, change the FirstName to John
d. Click Save to commit the changes
Figure 12
Edit External Customer
e. Verify the change is reflected in the customers list
Figure 13
Updated Customer Record
Note: If you would like to verify the updated data in the database, open SQL
Management Studio and perform a query against the SalesLT.Customer table in the
AdventureWorksLT2008 database.
Exercise 2: Creating External Content
Types with Visual Studio 2010
In this exercise you will build a .NET shim that will allow BCS to connect to data in a flat file on the
server. To build the .NET shim and the BCS entity definition files you will use Visual Studio 2010.
Task 1 – Create the Visual Studio 2010 project
In this task, create a new SharePoint 2010 project that will hold the External Data Model object.
1. Open Visual Studio 2010 and create a new project.
a. In the File menu, choose New -> Project
b. In the Templates pane, choose Visual C# \Visual Basic -> SharePoint -> 2010
c. Choose Empty SharePoint Project as the template
d. Set the Name to CustomerExternalContentType
e. Set the Location to
%Office2010DeveloperTrainingKitPath%\Labs\BCSOffice\[language]\Source\Starte
r
f.
Verify the Create directory for solution checkbox is cleared
g. Click OK to create the new project
Figure 14(a)
Figure 14(b)
Create new SharePoint 2010 Project
2. Define the target SharePoint site the project will use
a. Enter http://intranet.contoso.com/sites/BCSOffice as the debugging site
b. Select Deploy as a farm solution
c. Click Finish to close the wizard
Figure 15
Set deployment SharePoint site
Task 2 – Define the External Data Model describing the data
In this task, you will define the data model markup that describes how BCS should access the data from
the .NET shim classes.
1. Create a new Business Data Connectivity Model named External System Model
a. Right click the project in the Solution Explorer and click Add -> New Item
b. Select the Business Data Connectivity Model template
c. Set the Name to ExternalSystemModel and click Add
Figure 16(a)
Figure 16(b)
Create External System Model
2. Define the Customer entity
a. Right click the existing Entity1 entity and select Delete
b. Select Entity1.cs(Entity1.vb in case of VB) and Entity1Service.cs(Entity1Service.vb in
case of VB) in the Solution Explorer and delete them
c. Right click the canvas and select Add -> Entity
d. Right click the new entity and select Properties
e. In the Properties window, set the Name to Customer
f.
Right click Customer entity and select Add -> Identifier
g. Select the identifier and set its Name to Name using the Properties window
Figure 17
Customer Entity
3. Add a ReadList method to the Customer entity
a. Right click the Customer entity and select Add -> Method
b. Rename the method to ReadList
c. In the BDC Method Details pane locate the ReadList method and expand its
Parameters child node
Figure 18
BCD Method Details window
d. Click the drop down in the <Add a Parameter> cell and choose Create Parameter
e. Select the new Parameter and set the following parameters in the Properties
window
i.
Name to returnParameter
ii. Parameter Direction to Return
Figure 19
ReadList Parameter Properties
f.
Locate the Instances child node of the Read List method.
g. Click the drop down in the <Add a Method Instance> and choose Create Finder
Instance
h. In the Properties window, set the following parameters
i.
Name to ReadList
ii. Default to True
iii. Default Display Name to Read List
iv. Return Parameter name to returnParameter
Figure 20
ReadList Instance Properties
4. Define the structure of the return value of the ReadList method
a. Locate the BDC Explorer in a tab next to the Solution Explorer
Note: If the BDC Explorer is not visible, use the View -> Other Windows -> BDC
Explorer menu option to display it
Figure 21
BCD Explorer window
b. Expand the ReadList message and find returnParameterTypeDescriptor
c. In the Properties window, set the following properties
i.
Name to CustomerList
ii. TypeName to
System.Collections.Generic.IEnumerable`1[[CustomerExternalConte
ntType.ExternalSystemModel.Customer, ExternalSystemModel]]
iii. IsCollection to True
Figure 22
ReadList CustomerList Type Descriptor Properties
d. In the BDC Explorer, right click CustomerList and select Add Type Descriptor
e. Select the new type descriptor and set the following properties in the Properties
window
i.
Name to Customer
ii. TypeName to
CustomerExternalContentType.ExternalSystemModel.Customer,
ExternalSystemModel
Figure 23
ReadList Customer Type Descriptor Properties
f.
In the BDC Explorer, right click Customer and select Add Type Descriptor
g. Select the new type descriptor and set the following properties in the Properties
window
i.
Name to Name
ii. TypeName to System.String
iii. Identifier to Name
Figure 24
Completed ReadList Type Descriptors
5. Define the ReadItem method
a. Right click the Customer entity and select Add -> Method
b. Rename the method to ReadItem
c. Switch to the BDC Method Details pane
d. In the ReadItem method’s parameters node, add a new parameter and set the
following parameters in the Properties window
i.
Name to returnParameter
ii. Parameter Direction to Return
e. Add another parameter and set the following parameters in the Properties window
i.
Name to id
ii. Parameter Direction to In
f.
In the ReadItem method’s instances node, add a new instance using Create Finder
Instance and set the following values in the Properties window
i.
Name to ReadItem
ii. Type to SpecificFinder
iii. Default to True
iv. Default Display Name to Read Item
v. Return Parameter name to returnParameter
g. In the BDC Explorer locate the ReadItem parameters and expand them both
h. Select idTypeDescriptor under the ReadItem’s id parameter and set the following
values in the Properties window
i.
Name to CustomerId
ii. TypeName to System.String
iii. Identifier to Name
i.
Right click Customer under ReadList -> returnParameter -> CustomerList ->
Customer and select Copy
j.
Right click returnParameter under ReadItem and select Paste
k. Click Yes when asked if you want to replace the existing type descriptor
Figure 25
ReadItem Type Descriptors
6. Define the Create method
a. Right click the Customer entity and select Add -> Method
b. Rename the method to Create
c. Switch to the BDC Method Details pane
d. In the Create method’s parameters node, add a new parameter and set the
following parameters in the Properties window
i.
Name to returnParameter
ii. Parameter Direction to Return
e. Add another parameter and set the following parameters in the Properties window
i.
Name to name
ii. Parameter Direction to In
f.
In the Create method’s instances node, add a new instance using Create Finder
Instance and set the following values in the Properties window
i.
Name to Create
ii. Type to Creator
iii. Default to True
iv. Default Display Name to Create
v. Return Parameter name to returnParameter
g. In the BDC Explorer locate the Create method’s parameters and expand them
h. Select nameTypeDescriptor under the Create method’s name parameter and set the
following values in the Properties window
i.
Name to Name
ii. TypeName to System.String
iii. Creator Field to True
i.
Select Customer under ReadList -> returnParameter -> CustomerList -> Customer
and select Copy
j.
Right click returnParameter under Create and select Paste
k. Click Yes when asked if you want to replace the existing type descriptor
Figure 26
Create Type Descriptors
Task 3 – Implement the Customer data source object
In this task, you will define the data source classes that will manage the flat file data source.
1. Create the Customer object
a. Right click ExternalSystemModel in the Solution Explorer and select Add -> Class
b. Set the name to Customer.cs(Customer.vb in case of VB) and click Add to create the
new class
c. Implement the class using the following code
C#
public class Customer
{
public string Name { get; set; }
}
Visual Basic
Public Class Customer
Public Property Name() As String
End Class
2. Add the flat file parsing helper methods to the CustomerService class
a. Open the CustomerService.cs(CustomerService.vb in case of VB) file by double
clicking it in the Solution Explorer
b. Add the following using statements to the file
C#
using System.IO;
Visual Basic
Imports System.IO
c. Add the following methods to read and write customer data using a flat file
C#
private const string m_flatFile =
@"C:\Office2010DeveloperTrainingKit\Labs\BCSOffice\Source\Customers.txt"
;
private static IList<string> LoadItems()
{
List<string> results = new List<string>();
using (StreamReader reader = File.OpenText(m_flatFile))
while (!reader.EndOfStream)
results.Add(reader.ReadLine());
return results;
}
private static void SaveItems(IEnumerable<string> items)
{
using (FileStream file =
File.Open(m_flatFile, FileMode.Create, FileAccess.Write))
using (StreamWriter writer = new StreamWriter(file))
foreach (string item in items)
writer.WriteLine(item);
}
Visual Basic
Private Const m_flatFile As String =
"C:\Office2010DeveloperTrainingKit\Labs\BCSOffice\Source\Customers.txt"
Private Shared Function LoadItems() As IList(Of String)
Dim results As New List(Of String)()
Using reader As StreamReader = File.OpenText(m_flatFile)
Do While Not reader.EndOfStream
results.Add(reader.ReadLine())
Loop
End Using
Return results
End Function
Private Shared Sub SaveItems(ByVal items As IEnumerable(Of String))
Using file As FileStream = File.Open(m_flatFile, FileMode.Create,
FileAccess.Write)
Using writer As New StreamWriter(file)
For Each item As String In items
writer.WriteLine(item)
Next item
End Using
End Using
End Sub
3. Implement the ReadList, ReadItem, and Create methods
a. Add the following code to the ReadList method to return all items from the flat file.
Replace the exception handling code in the method with this code.
C#
public static IEnumerable<Customer> ReadList()
{
return LoadItems().Select(n => new Customer { Name = n });
}
Visual Basic
Public Shared Function ReadList() As IEnumerable(Of Customer)
Return LoadItems().Select(Function(n) New Customer With {.Name =
n})
End Function
b. Add the following code to the ReadItem methods to return the item requested.
Replace the exception handling code in the method with this code.
C#
public static Customer ReadItem(string id)
{
return new Customer { Name = id };
}
Visual Basic
Public Shared Function ReadItem(ByVal id As String) As Customer
Return New Customer With {.Name = id}
End Function
c. Add the following method to the Create method to load all items, add an item to the
list, and write it back to the flat file. Replace the exception handling code in the
method with this code.
C#
public static Customer Create(string name)
{
IList<string> items = LoadItems();
items.Add(name);
SaveItems(items);
return new Customer { Name = name };
}
Visual Basic
Public Shared Function Create(ByVal name As String) As Customer
Dim items As IList(Of String) = LoadItems()
items.Add(name)
SaveItems(items)
Return New Customer With {.Name = name}
End Function
4. Add a SiteUrl property to the ExternalSystemModel item
a. Right click ExternalSystemModel in the Solution Explorer and select Properties
b. In the Properties window, click the … button by Feature Properties
Figure 27
Feature Properties
c. Click Add to add a new Property
d. Set the Key to SiteUrl and the Value to http://intranet.contoso.com
Figure 28
SiteUrl Property
5. Build and deploy the project by right clicking CustomerExternalContentType in the Solution
Explorer and clicking Deploy
Task 4 – Create the External Content Type and External List
In this task, you will use the SharePoint Designer to verify the new External Content Type exists and
create a new External List based on it.
1. Open SharePoint Designer and load the existing External Content Types.
a. In Internet Explorer navigate to http://intranet.contoso.com/sites/BCSOffice
b. Click Site Actions -> Edit Site in SharePoint Designer
c. In SharePoint Designer locate the Navigation pane and click External Content Types
d. Double click the Customer item with an External System value of
ExternalSystemModel
2. Create the new External List using the Customer External Content Type.
a. Click the Create Lists & Form button in the External Content Types ribbon tab
b. In the List Item text box enter Flat File Customers
c. Click OK to create the new list
Exercise 2 Verification
In order to verify that you have correctly performed all steps of exercise 2, proceed as follows:
Test your work
Verify you can see items in the browser that exist in the flat file. Add a new item to the flat file using
SharePoint.
1. Verify the External Customers list is available in the SharePoint site
a. In Internet Explorer navigate to http://intranet.contoso.com/sites/BCSOffice
b. Click the Flat File Customers link in the Navigation bar
c. Verify the customers are visible in the list page
Figure 29
Flat File Customers List
Exercise 3: Working with BCS Data
Offline
In this exercise you will use the Customers External List to populate a customer drop down in a custom
Outlook Form Region. Since user’s often take Outlook offline, the offline capabilities of SharePoint
Workspace will cache the customers list locally.
Task 1 – Take the Customers list offline
In this task, add the Customers list to SharePoint Workspace so customers are available when no
connection to the SharePoint site exists.
1. Sync the Flat File Customers list with SharePoint Workspace
a. In Internet Explorer navigate to http://intranet.contoso.com/sites/BCSOffice
b. Click the Flat File Customers link in the Navigation list to view the flat file data
c. In the List ribbon tab, click the Sync to SharePoint Workspace button located in the
Connect & Export group
d. When asked if you want to allow the sync, click Yes
e. If you are asked if you want to install an add-in, click the Install button
Task 2 – Update the Outlook Form Region
In this task, you will update the code in the Outlook Form Region and allow it to access the cached
customer list using the BCS APIs.
1. Open the starter OutlookFormRegion project in Visual Studio 2010
a. Open the OutlookFormRegion.sln file in the
%Office2010DeveloperTrainingKitPath%\Labs\BCSOffice\[language]\Source\Starte
r\OutlookFormRegion folder
b. Once the solution is loaded, right click OutlookFormRegion in the Solution Explorer
and select Add Reference
c. Click the Browse tab and navigate to the C:\Program Files\Microsoft
Office\Office14 folder and add references to the following assemblies
i.
Microsoft.BusinessData.dll
ii. Microsoft.Office.BusinessData.dll
iii. Microsoft.Office.BusinessApplications.Runtime.dll
d. Right click OutlookFormRegion in the Solution Explorer and click Properties
e. In the Application tab confirm that the Target framework is .NET Framework 4 if
not, make it so. (VB, Compile tab, Advanced compile options… button)
i.
Click Yes when asked if you want to change the framework version
2. Create a new class named CustomerDataSource that will wrap all communication to the cached
customer data
a. Right click OutlookFormRegion in the Solution Explorer and click Add -> Class
b. In the Add New Item dialog, set the Name to CustomerDataSource and click Add
c. In the new class add the following using statements
C#
using Microsoft.Office.BusinessData.MetadataModel;
using Microsoft.BusinessData.Runtime;
using Microsoft.BusinessData.MetadataModel;
Visual Basic
Imports Microsoft.Office.BusinessData.MetadataModel
Imports Microsoft.BusinessData.Runtime
Imports Microsoft.BusinessData.MetadataModel
d. Add the following fields to the CustomerDataSource class to track the BCS entities
C#
private IEntity m_entity;
private ILobSystemInstance m_lobSystemInstance;
Visual Basic
Private m_entity As IEntity
Private m_lobSystemInstance As ILobSystemInstance
e. Add a constructor that will use the RemoteSharedFileBackedMetadataCatalog to
access the metadata for the flat file customer entity
C#
public CustomerDataSource()
{
RemoteSharedFileBackedMetadataCatalog catalog =
new RemoteSharedFileBackedMetadataCatalog();
m_entity = catalog.GetEntity(
"CustomerExternalContentType.ExternalSystemModel",
"Customer");
m_lobSystemInstance = m_entity.GetLobSystem().
GetLobSystemInstances()[0].Value;
}
Visual Basic
Public Sub New()
Dim catalog As New RemoteSharedFileBackedMetadataCatalog()
m_entity =
catalog.GetEntity("CustomerExternalContentType.ExternalSystemMode
l", "Customer")
m_lobSystemInstance =
m_entity.GetLobSystem().GetLobSystemInstances()(0).Value
End Sub
f.
Add a GetCustomers method that returns the name of every customer in the flat file
C#
public IEnumerable<string> GetCustomers()
{
IEntityInstanceEnumerator items = m_entity.FindFiltered(
m_entity.GetDefaultFinderFilters(),
"ReadList",
m_lobSystemInstance,
OperationMode.CachedWithImmediateRefresh);
List<string> results = new List<string>();
while (items.MoveNext())
results.Add(items.Current["Name"].ToString());
return results;
}
Visual Basic
Public Function GetCustomers() As IEnumerable(Of String)
Dim items As IEntityInstanceEnumerator =
m_entity.FindFiltered(m_entity.GetDefaultFinderFilters(),
"ReadList", m_lobSystemInstance,
OperationMode.CachedWithImmediateRefresh)
Dim results As New List(Of String)()
Do While items.MoveNext()
results.Add(items.Current("Name").ToString())
Loop
Return results
End Function
g. Add an AddCustomer method that uses the BCS objects to create a new customer in
the flat file
C#
public void AddCustomer(string name)
{
IFieldValueDictionary values =
m_entity.GetCreatorView("Create").GetDefaultValues();
values["Name"] = name;
m_entity.Create(values, m_lobSystemInstance);
}
Visual Basic
Public Sub AddCustomer(ByVal name As String)
Dim values As IFieldValueDictionary =
m_entity.GetCreatorView("Create").GetDefaultValues()
values("Name") = name
m_entity.Create(values, m_lobSystemInstance)
End Sub
3. Update the BillableTaskRegion code to use the customer list from BCS instead of a static list
a. Right click BillableTaskRegion.cs(BillableTaskRegion.vb in case of VB) in the
Solution Explorer and select View Code
b. Add the following private field to the BillableTaskRegion class to initialize the
CustomerDataSource object
C#
private CustomerDataSource m_customers = new CustomerDataSource();
Visual Basic
Private m_customers As New CustomerDataSource()
c. Add the following code to the end of the BillableTaskRegion_FormRegionShowing
method to initialize the lstCustomer drop down based on the BCS data
C#
lstCustomer.Items.Clear();
lstCustomer.Items.AddRange(m_customers.GetCustomers().ToArray());
Visual Basic
lstCustomer.Items.Clear()
lstCustomer.Items.AddRange(m_customers.GetCustomers().ToArray())
4. Update the BillableTaskRegion code write any new customers to BCS when the form is closed
a. Add the following code to the BillableTaskRegion_FormRegionClosed method
C#
private void BillableTaskRegion_FormRegionClosed(
object sender, System.EventArgs e)
{
if (m_taskItem.Saved)
if (lstCustomer.SelectedItem == null &&
!string.IsNullOrEmpty(lstCustomer.Text))
if (!m_customers.GetCustomers().Any(
n => n == lstCustomer.Text))
m_customers.AddCustomer(lstCustomer.Text);
}
Visual Basic
Private Sub BillableTaskRegion_FormRegionClosed(ByVal sender As Object,
ByVal e As
System.EventArgs)
If m_taskItem.Saved Then
If lstCustomer.SelectedItem Is Nothing AndAlso (Not
String.IsNullOrEmpty(lstCustomer.Text)) Then
If Not m_customers.GetCustomers().Any(Function(n) n
= lstCustomer.Text) Then
m_customers.AddCustomer(lstCustomer.Text)
End If
End If
End If
End Sub
Note: This code only adds an item if the Outlook task was saved, the item that was
selected is new, and the item does not already exist in the list
Exercise 3 Verification
In order to verify that you have correctly performed all steps in the above exercise, proceed as follows:
Test your work
Open Outlook and verify the customers list loads from data in BCS. Once the data is loaded, add a new
value by manually entering a name in to the drop down combo box and verify the data exists in the flat
file data source.
1. Start the add-in and verify the list of customers is loading from BCS
a. Start Outlook by clicking Debug -> Start Without Debugging
b. Create a new task in Outlook by clicking New Items -> Task in the Home ribbon tab
c. In the new Task form, check the billable check box and open the Customer drop down
d. Verify the following values are displayed from the flat file
Figure 30
Customers from Flat File
2. Add a new customer and verify it’s added to the flat file
a. In the new task item, enter a customer of Contoso Inc.
b. Save the task by clicking Save on the Task ribbon tab
c. Open %Office2010DeveloperTrainingKitPath%\Labs\BCSOffice\Source\Customers.txt
in notepad
d. Verify that your company name. has been added to the end of the file
Figure 31
Updated Customers Flat File
Summary
In this exercise you learned how to expose external data from databases and flat files using BCS. You
first exposed data in a database using the SharePoint Designer and later exposed flat file data using
Visual Studio 2010. When all the data was exposed you then accessed it through the BCS object model
allowing offline access to the external data.
Download