Share via


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.
    1. In the File menu, choose New -> Project
    2. In the Templates pane, choose Visual C# \Visual Basic -> SharePoint -> 2010
    3. Choose Empty SharePoint Project as the template
    4. Set the Name to CustomerExternalContentType
    5. Set the Location to %Office2010DeveloperTrainingKitPath%\Labs\BCSOffice\[language]\Source\Starter
    6. Verify the Create directory for solution checkbox is cleared
    7. 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
    1. Enter https://intranet.contoso.com/sites/BCSOffice as the debugging site
    2. Select Deployas a farm solution
    3. 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 ConnectivityModel named External System Model
    1. Right click the project in the Solution Explorer and click Add -> New Item
    2. Select the Business Data Connectivity Model template
    3. Set the Name to ExternalSystemModel and click Add

      Figure 16(a)

      Figure 16(b)

      Create External System Model

  2. Define the Customer entity
    1. Right click the existing Entity1 entity and select Delete
    2. 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
    3. Right click the canvas and select Add -> Entity
    4. Right click the new entity and select Properties
    5. In the Properties window, set the Name to Customer
    6. Right click Customer entity and select Add -> Identifier
    7. 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
    1. Right click the Customer entity and select Add -> Method
    2. Rename the method to ReadList
    3. In the BDC Method Details pane locate the ReadList method and expand its Parameters child node

      Figure 18

      BCD Method Details window

    4. Click the drop down in the <Add a Parameter> cell and choose Create Parameter
    5. Select the new Parameter and set the following parameters in the Properties window

      1. Name to returnParameter
      2. Parameter Direction to Return

      Figure 19

      ReadList Parameter Properties

    6. Locate the Instances child node of the Read List method.
    7. Click the drop down in the <Add a Method Instance> and choose Create Finder Instance
    8. In the Properties window, set the following parameters

      1. Name to ReadList
      2. Default to True
      3. DefaultDisplay Name to Read List
      4. Return Parameter name to returnParameter

      Figure 20

      ReadList Instance Properties

  4. Define the structure of the return value of the ReadList method
    1. 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

    2. Expand the ReadList message and find returnParameterTypeDescriptor
    3. In the Properties window, set the following properties

      1. Name to CustomerList
      2. TypeName to System.Collections.Generic.IEnumerable`1[[CustomerExternalContentType.ExternalSystemModel.Customer, ExternalSystemModel]]
      3. IsCollection to True

      Figure 22

      ReadList CustomerList Type Descriptor Properties

    4. In the BDC Explorer, right click CustomerList and select Add Type Descriptor
    5. Select the new type descriptor and set the following properties in the Properties window

      1. Name to Customer
      2. TypeName to CustomerExternalContentType.ExternalSystemModel.Customer, ExternalSystemModel

      Figure 23

      ReadList Customer Type Descriptor Properties

    6. In the BDC Explorer, right click Customer and select Add Type Descriptor
    7. Select the new type descriptor and set the following properties in the Properties window

      1. Name to Name
      2. TypeName to System.String
      3. Identifier to Name

      Figure 24

      Completed ReadList Type Descriptors

  5. Define the ReadItem method
    1. Right click the Customer entity and select Add -> Method
    2. Rename the method to ReadItem
    3. Switch to the BDC Method Details pane
    4. In the ReadItem method’s parameters node, add a new parameter and set the following parameters in the Properties window
      1. Name to returnParameter
      2. Parameter Direction to Return
    5. Add another parameter and set the following parameters in the Properties window
      1. Name to id
      2. Parameter Direction to In
    6. In the ReadItem method’s instances node, add a new instance using Create Finder Instance and set the following values in the Properties window
      1. Name to ReadItem
      2. Type to SpecificFinder
      3. Default to True
      4. DefaultDisplay Name to Read Item
      5. Return Parameter name to returnParameter
    7. In the BDC Explorer locate the ReadItem parameters and expand them both
    8. Select idTypeDescriptor under the ReadItem’s id parameter and set the following values in the Properties window
      1. Name to CustomerId
      2. TypeName to System.String
      3. Identifier to Name
    9. Right click Customer under ReadList -> returnParameter -> CustomerList -> Customer and select Copy
    10. Right click returnParameter under ReadItem and select Paste
    11. Click Yes when asked if you want to replace the existing type descriptor

      Figure 25

      ReadItem Type Descriptors

  6. Define the Create method
    1. Right click the Customer entity and select Add -> Method
    2. Rename the method to Create
    3. Switch to the BDC Method Details pane
    4. In the Create method’s parameters node, add a new parameter and set the following parameters in the Properties window
      1. Name to returnParameter
      2. Parameter Direction to Return
    5. Add another parameter and set the following parameters in the Properties window
      1. Name to name
      2. Parameter Direction to In
    6. In the Create method’s instances node, add a new instance using Create Finder Instance and set the following values in the Properties window
      1. Name to Create
      2. Type to Creator
      3. Default to True
      4. DefaultDisplay Name to Create
      5. Return Parameter name to returnParameter
    7. In the BDC Explorer locate the Create method’s parameters and expand them
    8. Select nameTypeDescriptor under the Create method’s name parameter and set the following values in the Properties window
      1. Name to Name
      2. TypeName to System.String
      3. Creator Field to True
    9. Select Customer under ReadList -> returnParameter -> CustomerList -> Customer and select Copy
    10. Right click returnParameter under Create and select Paste
    11. 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
    1. Right click ExternalSystemModel in the Solution Explorer and select Add -> Class
    2. Set the name to Customer.cs(Customer.vb in case of VB) and click Add to create the new class
    3. 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
    1. Open the CustomerService.cs(CustomerService.vb in case of VB) file by double clicking it in the Solution Explorer
    2. Add the following using statements to the file

      C#

      using System.IO;

      Visual Basic

      Imports System.IO

    3. 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
    1. 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()
      FakePre-5d082c5f811b4d1ebf29c9d6a7889eaa-bcd994e063264116b4a7a2687b9baaf0 return LoadItems().Select(n => new Customer { Name = n });FakePre-481dccb3c601420c8cd5fa43c1defd35-b4fda4a85b354269b58c086e4eb9e742

      Visual Basic

      Public Shared Function ReadList() As IEnumerable(Of Customer) Return LoadItems().Select(Function(n) New Customer With {.Name = n}) End Function

    2. 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)
      FakePre-4fb8a023d2b541c8bf4d1d9e85e5fce5-e2b70cfae81f4f8a87de0cb372442867return new Customer { Name = id };FakePre-0bb188c0b08647f5a05de4c4e3d22161-e26e1e2634ae4277a1fe06c5af172337

      Visual Basic

      Public Shared Function ReadItem(ByVal id As String) As Customer Return New Customer With {.Name = id} End Function

    3. 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)
      FakePre-14302106436748b7b6b29ac9f0318c45-697d7359a50c47d98b72c62fe02663ac IList<string> items = LoadItems(); items.Add(name); SaveItems(items); return new Customer { Name = name };FakePre-3bd04fc70cb34aaa9cf045b48fa395d3-502e5b63e5a24f2da347b7438eda09b1FakePre-865de560e6174cfcb0c5bfb631c678cd-75e49f82aa0841809aa93ac58e82bb32

      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}FakePre-5e7d21b1a5a849448bb9a0865ba7c320-d8440e4abb614dc2b8fd2f585b75bf46FakePre-6bed12998c3742038be51a6c304a5a84-861a2db342f24140832f5e58e0e8fa95

  4. Add a SiteUrl property to the ExternalSystemModel item
    1. Right click ExternalSystemModel in the Solution Explorer and select Properties
    2. In the Properties window, click the button by Feature Properties

      Figure 27

      Feature Properties

    3. Click Add to add a new Property
    4. Set the Key to SiteUrl and the Value to https://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.
    1. In Internet Explorer navigate to https://intranet.contoso.com/sites/BCSOffice
    2. Click Site Actions -> Edit Site in SharePoint Designer
    3. In SharePoint Designer locate the Navigation pane and click External Content Types
    4. Double click the Customer item with an External System value of ExternalSystemModel
  2. Create the new External List using the CustomerExternal Content Type.
    1. Click the Create Lists & Form button in the External Content Types ribbon tab
    2. In the List Item text box enter Flat File Customers
    3. 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
    1. In Internet Explorer navigate to https://intranet.contoso.com/sites/BCSOffice
    2. Click the Flat File Customers link in the Navigation bar
    3. Verify the customers are visible in the list page

      Figure 29

      Flat File Customers List