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.
- Sync the Flat File Customers list with SharePoint Workspace
- In Internet Explorer navigate to https://intranet.contoso.com/sites/BCSOffice
- Click the Flat File Customers link in the Navigation list to view the flat file data
- In the List ribbon tab, click the Sync to SharePoint Workspace button located in the Connect & Export group
- When asked if you want to allow the sync, click Yes
- If you are asked if you want to install an add-in, click the Install button
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.
- Open the starter OutlookFormRegion project in Visual Studio 2010
- Open the OutlookFormRegion.sln file in the %Office2010DeveloperTrainingKitPath%\Labs\BCSOffice\[language]\Source\Starter\OutlookFormRegion folder
- Once the solution is loaded, right click OutlookFormRegion in the Solution Explorer and select Add Reference
- Click the Browse tab and navigate to the C:\Program Files\Microsoft Office\Office14 folder and add references to the following assemblies
- Microsoft.BusinessData.dll
- Microsoft.Office.BusinessData.dll
- Microsoft.Office.BusinessApplications.Runtime.dll
- Right click OutlookFormRegion in the Solution Explorer and click Properties
- 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)
- Click Yes when asked if you want to change the framework version
- Create a new class named CustomerDataSource that will wrap all communication to the cached customer data
- Right click OutlookFormRegion in the Solution Explorer and click Add -> Class
- In the Add New Item dialog, set the Name to CustomerDataSource and click Add
In the new class add the following using statements
using Microsoft.Office.BusinessData.MetadataModel; using Microsoft.BusinessData.Runtime; using Microsoft.BusinessData.MetadataModel;
Imports Microsoft.Office.BusinessData.MetadataModel Imports Microsoft.BusinessData.Runtime Imports Microsoft.BusinessData.MetadataModel
Add the following fields to the CustomerDataSource class to track the BCS entities
private IEntity m_entity; private ILobSystemInstance m_lobSystemInstance;
Private m_entity As IEntity Private m_lobSystemInstance As ILobSystemInstance
Add a constructor that will use the RemoteSharedFileBackedMetadataCatalog to access the metadata for the flat file customer entity
public CustomerDataSource() { RemoteSharedFileBackedMetadataCatalog catalog = new RemoteSharedFileBackedMetadataCatalog(); m_entity = catalog.GetEntity( "CustomerExternalContentType.ExternalSystemModel", "Customer"); m_lobSystemInstance = m_entity.GetLobSystem(). GetLobSystemInstances()[0].Value; }
Public Sub New() Dim catalog As New RemoteSharedFileBackedMetadataCatalog() m_entity = catalog.GetEntity("CustomerExternalContentType.ExternalSystemModel", "Customer") m_lobSystemInstance = m_entity.GetLobSystem().GetLobSystemInstances()(0).Value End Sub
Add a GetCustomers method that returns the name of every customer in the flat file
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; }
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
Add an AddCustomer method that uses the BCS objects to create a new customer in the flat file
public void AddCustomer(string name) { IFieldValueDictionary values = m_entity.GetCreatorView("Create").GetDefaultValues(); values["Name"] = name; m_entity.Create(values, m_lobSystemInstance); }
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
- Update the BillableTaskRegion code to use the customer list from BCS instead of a static list
- Right click BillableTaskRegion.cs(BillableTaskRegion.vb in case of VB) in the Solution Explorer and select View Code
Add the following private field to the BillableTaskRegion class to initialize the CustomerDataSource object
private CustomerDataSource m_customers = new CustomerDataSource();
Private m_customers As New CustomerDataSource()
Add the following code to the end of the BillableTaskRegion_FormRegionShowing method to initialize the lstCustomer drop down based on the BCS data
lstCustomer.Items.Clear(); lstCustomer.Items.AddRange(m_customers.GetCustomers().ToArray());
lstCustomer.Items.Clear() lstCustomer.Items.AddRange(m_customers.GetCustomers().ToArray())
- Update the BillableTaskRegion code write any new customers to BCS when the form is closed
- Add the following code to the BillableTaskRegion_FormRegionClosed method
private void BillableTaskRegion_FormRegionClosed(
FakePre-d5d7e8d16b0a453682e813cf0670bcc6-fd083ccc03ae40a18fa8feb9f993b6e9FakePre-723e34dfad8d48149049a81da7a7c461-182f0bd20ab84bd1ac49afb93a423118 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);FakePre-9662c9c4a8284d3ba76b525b00ee5eff-ae7c2715c01a495d90716dbbef42a6d2
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 IfFakePre-b1a1511b584f4f36a62004605c1317dc-c8f0595f42d04dea9340697a84ef98cd
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.
- Start the add-in and verify the list of customers is loading from BCS
- Start Outlook by clicking Debug -> Start Without Debugging
- Create a new task in Outlook by clicking New Items -> Task in the Home ribbon tab
- In the new Task form, check the billable check box and open the Customer drop down
Verify the following values are displayed from the flat file
Figure 30
Customers from Flat File
- Add a new customer and verify it’s added to the flat file
- In the new task item, enter a customer of Contoso Inc.
- Save the task by clicking Save on the Task ribbon tab
- Open %Office2010DeveloperTrainingKitPath%\Labs\BCSOffice\Source\Customers.txt in notepad
Verify that your company name. has been added to the end of the file
Figure 31
Updated Customers Flat File