1 out of 4 rated this helpful - Rate this topic

Walkthrough: Creating an External List in SharePoint by Using Business Data

The Business Data Connectivity (BDC) service enables SharePoint to display business data from back-end server applications, Web services, and databases.

This walkthrough shows you how to create a model for the BDC service that returns information about contacts in a sample database. You will then create an external list in SharePoint by using this model.

This walkthrough illustrates the following tasks:

  • Creating a project.

  • Adding an entity to the model.

  • Adding a finder method.

  • Adding a specific finder method.

  • Testing the project.

You need the following components to complete this walkthrough:

  • Supported editions of Microsoft Windows and SharePoint. For more information, see Requirements for Developing SharePoint Solutions.

  • Visual Studio 2010 Professional or an edition of Visual Studio Application Lifecycle Management (ALM).

  • Access to the AdventureWorks sample database. For more information about how to install the AdventureWorks database, see SQL Server Sample Databases.

First, create a project that contains a BDC model.

To create a project

  1. Start Visual Studio 2010.

  2. Open the New Project dialog box, expand the SharePoint node under the language that you want to use, and then click 2010.

  3. In the Templates pane, select Business Data Connectivity Model. Name the project AdventureWorksContacts, and then click OK.

    The SharePoint Customization Wizard appears. This wizard enables you to select the site that you will use to debug the project and the trust level of the solution.

  4. Click Finish to accept the default local SharePoint site and default trust level of the solution.

To add data access classes to the project

  1. On the Tools menu, click Connect to Database.

    The Add Connection dialog box opens.

  2. Add a connection to the SQL Server AdventureWorks sample database. For more information, see Add/Modify Connection (Microsoft SQL Server).

  3. In Solution Explorer, click the project node.

  4. On the Project menu, click Add New Item.

  5. In the Installed Templates pane, select the Data node.

  6. In the Templates pane, select LINQ to SQL Classes.

  7. In the Name box, type AdventureWorks, and then click Add.

    A .dbml file is added to the project and the Object Relational Designer (O/R Designer) opens.

  8. On the View menu, click Server Explorer.

  9. In Server Explorer, expand the node that represents the AdventureWorks sample database, and then expand the Tables node.

  10. Drag the Contact (Person) table onto the O/R Designer.

    An entity class is created and appears on the design surface. The entity class has properties that map to the columns in the Contact (Person) table.

The Business Data Connectivity Model project adds a default entity named Entity1 to the model. Remove this entity. Later, you will add a new entity. Starting with an empty model reduces the number of steps required to complete the walkthrough.

To remove the default entity from the model

  1. In Solution Explorer, expand the BdcModel1 node, and then double-click the BdcModel1.bdcm file.

  2. The Business Data Connectivity model file opens in the BDC designer.

  3. In the designer, right-click Entity1, and then click Delete.

  4. In Solution Explorer, right-click Entity1.vb (in Visual Basic) or Entity1.cs (in C#), and then click Delete.

  5. Right-click Entity1Service.vb (in Visual Basic) or Entity1Service.cs (in C#), and then click Delete.

Add an entity to the model. You can drag entities from the Visual Studio Toolbox onto the BDC designer.

To add an Entity to the model

  1. On the View menu, click Toolbox.

  2. From the BusinessDataConnectivity tab of the Toolbox, drag an Entity onto the BDC designer.

    The new entity appears on the designer. Visual Studio adds a file to the project named EntityService.vb (in Visual Basic) or EntityService.cs (in C#).

  3. On the View menu, click Properties Window.

  4. In the Properties window, set Name to Contact.

  5. On the designer, right-click the entity, click Add, and then click Identifier.

    A new identifier appears on the entity.

  6. In the Properties window, change the name of the identifier to ContactID.

  7. In the Type Name drop-down list, select System.Int32.

To enable the BDC service to display a specific contact, you must add a Specific Finder method. The BDC service calls the Specific Finder method when a user selects an item in a list and then clicks the View Item button in the Ribbon.

Add a Specific Finder method to the Contact entity by using the BDC Method Details window. To return a specific entity, add code to the method.

To add a Specific Finder method

  1. On the BDC designer, select the Contact entity.

  2. On the View menu, click Other Windows, and then click BDC Method Details.

  3. In the BDC Method Details window, from the Add a Method drop-down list, select Create Specific Finder Method.

    Visual Studio adds the following elements to the model. These elements appear in the BDC Method Details window.

    • A method named ReadItem.

    • An input parameter for the method.

    • A return parameter for the method.

    • A type descriptor for each parameter.

    • A method instance for the method.

  4. In the BDC Method Details window, click the drop-down list that appears for the Contact type descriptor, and then click Edit.

    The BDC Explorer opens. The BDC Explorer provides a hierarchical view of the model.

  5. In the Properties window, click the drop-down list that appears next to the TypeName property, click the Current Project tab, and then select Contact.

  6. In the BDC Explorer, right-click the Contact, and then click Add Type Descriptor.

    A new type descriptor named TypeDescriptor1 appears in the BDC Explorer.

  7. In the Properties window, set the Name property to ContactID.

  8. Click the drop-down list next to the TypeName property, and then select Int32.

  9. Click the drop-down list next to the Identifier property, and then select ContactID.

  10. Repeat step 6 to create a type descriptor for each of the following fields.

    Name

    Type Name

    FirstName

    System.String

    LastName

    System.String

    Phone

    System.String

    EmailAddress

    System.String

    EmailPromotion

    System.Int32

    NameStyle

    System.Boolean

    PasswordHash

    System.String

    PasswordSalt

    System.String

  11. In the BDC designer, on the Contact entity, double-click the ReadItem method.

    The Contact service code file opens in Code Editor.

  12. In the ContactService class, replace the ReadItem method with the following code. This code performs the following tasks:

    • Retrieves a record from Contact table of the AdventureWorks database.

    • Returns a Contact entity to the BDC service.

    Note Note

    Replace the value of the ServerName field with the name of your server.

    
    public static Contact ReadItem(int contactID)
    {
        const string ServerName = "MySQLServerName";
        AdventureWorksDataContext dataContext = new AdventureWorksDataContext
              ("Data Source=" + ServerName + ";" +
               "Initial Catalog=AdventureWorks;Integrated Security=True");
    
        Contact Contact =
            (from contacts in dataContext.Contacts.AsEnumerable().Take(20)
             where contacts.ContactID == contactID
             select contacts).Single();
        return Contact;
    }
    
    
    

To enable the BDC service to display the contacts in a list, you must add a Finder method. Add a Finder method to the Contact entity by using the BDC Method Details window. To return a collection of entities to the BDC service, add code to the method.

To add a Finder method

  1. In the BDC designer, select the Contact entity.

  2. In the BDC Method Details window, collapse the ReadList node.

  3. From the Add a Method drop-down list that appears below the ReadList method, select Create Finder Method.

    Visual Studio adds a method, a return parameter, and a type descriptor.

  4. In the BDC designer, on the Contact entity, double-click the ReadList method.

    The Contact service code file opens in Code Editor.

  5. In the ContactService class, replace the ReadList method with the following code. This code performs the following tasks:

    • Retrieves data from the Contacts table of the AdventureWorks database.

    • Returns a list of Contact entities to the BDC service.

    Note Note

    Replace the value of the ServerName field with the name of your server.

    
    public static IEnumerable<Contact> ReadList()
    {
        const string ServerName = "MySQLServerName";
        AdventureWorksDataContext dataContext = new AdventureWorksDataContext
              ("Data Source=" + ServerName + ";" +
               "Initial Catalog=AdventureWorks;Integrated Security=True");
    
        IEnumerable<Contact> Contacts =
            from contacts in dataContext.Contacts.Take(20)
            select contacts;
        return Contacts;
    
    }
    
    
    

When you run the project, the SharePoint site opens and Visual Studio adds your model to the Business Data Connectivity service. Create an external list in SharePoint that references the Contact entity. The data for contacts in the AdventureWorks database appear in the list.

Note Note

You might have to modify your security settings in SharePoint before you can debug your solution. For more information, see Designing a Business Data Connectivity Model.

To test the project

  1. Press F5.

    The SharePoint site opens.

  2. On the Site Actions menu, click More Options

  3. In the Create page, click External List, and then click Create.

  4. Name the custom list Contacts.

  5. Click the browse button next to the External Content Type field.

  6. In the External Content Type Picker dialog box, select AdventureWorksContacts.BdcModel1.Contact, and then click Create.

  7. Click Create to create the contacts list.

    SharePoint creates an external list. Contacts from the AdventureWorks sample database appear in that list.

  8. To test the Specific Finder method, click a contact in the list.

  9. On the Ribbon, click the Items tab.

  10. In the Items tab, click View Item.

    The details of the contact that you selected appear on a form.

You can learn more about how to design models for the BDC service in SharePoint from these topics:

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Error for every TypeDescriptor added

TypeDescriptor with Name 'FirstName' has a type 'System.String' that is not compatible with the type 'System.Int32' of the referenced Identifier 'ContactID'. A TypeDescriptor referencing an Identifier should have a TypeName that is one of the following: primitive type, GUID, Nullable form of a primitive type, or Nullable form of GUID. 
Please advise.

 

ReadItem
Please remove the .AsEnumerable().Take(20) from the linq in readitem. This will prevent using a where clause in the query (and not return the right item in some cases)
BDC is NOT a subset of MOSS
The deployment fails with SharePoint Foundation 2010 because there's a requirement for MOSS DLLs.  Can someone who knows what they are doing create a demonstration of BDC without the additional requirement of SharePoint MOSS.  BDC is NOT a subset of MOSS!!!!   Error is:

Error occurred in deployment step 'Add Solution': Failed to load receiver assembly "Microsoft.Office.SharePoint.ClientExtensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" for feature "AdventureWorksContracts_Feature1" (ID: d2eb56d1-52e4-43d6-969d-943c9038be69).: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Office.SharePoint.ClientExtensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' or one of its dependencies. The system cannot find the file specified.
File name: 'Microsoft.Office.SharePoint.ClientExtensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'

I tried a hotfix that included the DLLs and got a message, not applicable to my system.. yeah great!


Re: Inconsistant Walkthrough
Yes you are correct. I have fixed the issue. This fix will appear on MSDN in late May.
Inconsistant Walkthrough
This walk through seems to be a bit of a mess.

For instance clicking "Create Specific Finder Method" results in a ReadItem Method not a ReadList method. For that you need to select "Create Finder Method".

However, later on the walkthru it refers to the ReadItem method - so which does it want?

Never did get it to work. Not impressed at all.
Things need to check if the creating an external list with DBC is failed.
1- Central Admin Site --> Application Manangement --> Manange Service Applications --> BDC Service --> Select DBC Object --> Set Permissions.
2- Make sure ReadList and ReadItem return the same object type of entity class. These must be macthed in Names and Types
Is there anyone who have created an External List successfully according to this?
6- In the External Content Type Picker dialog box, select AdventureWorksContacts.BdcModel1.Contact, and then click Create.

If this step is failed, you need to take a look closely at "Contact" entity and the returned Object type of ReadList and ReadItem methods of  the DBC model. These must be mactched in Names and Types. For example: 

In the Contact entity class:
Public partial class Contact
{
      public Int32 ContactId {get;set;}
      public string LastName { get; set; }
      public string FirstName { get; set; }
}

In the ReadList and ReadItem methods under "returnParameter
Contact
    ContactId
(System.Int32)
    LastName (System.String)
    FirstName (System.String)

You also need to make sure the permissions of DBC service in the Central Admin Site.
Central Admin Site --> Application Manangement --> Manange Service Applications --> BDC Service --> Select DBC Object --> Set Permissions.

Happy coding.    
Creating external lists from code
Once you have defined your entity in BDC, you can create the external list programmatically as well. See my blog post for code examples and for my experience with that code
http://pholpar.wordpress.com/2010/08/23/creating-external-lists-from-code/
Another solution to the "Unable to display this Web Part." error
Hi,

It could also be mismatches in the BDM entity metadata versus the entity that you are mapping it to.

Have a look here: http://social.msdn.microsoft.com/Forums/en/sharepoint2010setup/thread/6449d01f-171b-49df-bfb6-c1090de3b598

Cheers
Mike
Found video explaining the same, which may solve the issue
Please watch the video at http://channel9.msdn.com/posts/kmcgrath/Creating-an-External-List-in-SharePoint-by-Using-Business-Data/

It is the same thing in video, however, I made one mistake while following up the walk through. I have created a solution which was deployed with errors.

Will have to fix it at home today. Will let you know later.

Ramesh
followed it to a tee and get same error as reported
Unable to display this Web Part. To troubleshoot the problem, open this Web page in a Microsoft SharePoint Foundation-compatible HTML editor such as Microsoft SharePoint Designer. If the problem persists, contact your Web server administrator.

Correlation ID:11b2a9e3-ea12-4960-bcef-b3a429f30e83

Has anyone been able to successfully create an external list using Business Data?
Re: Is there anyone who have created an External List successfully according to this?

It is likely that the TypeName of the Contact type descriptor is still set to "System.String". I would look at the Contact type descriptor in your Finder and/or Specific Finder method to ensure that the TypeName property is set to the Contact class.