3. Create an External Content Type
Now that you understand what BCS is and how the BDC
service uses metadata to connect to external systems, you’re ready to
put this knowledge into practice by creating an external content type
using SharePoint Designer.
In
SharePoint Designer, connect to the new site that we created earlier.
From the Site Objects menu, choose External Content Types: From
the ribbon, in the New section, select External Content Type. In the
page that appears, double-click New External Content Type next to the
Name label and change the name to Model.
From
the ribbon, select Operations Design View. You’ll notice that the title
of the window changes to Model, confirming that the name change has
been applied. In the
window that appears, click the Add Connection button. In the dialog
that appears, set the Data Source Type to SQL Server, as shown; then
click OK to continue. In
the SQL Server Connection dialog, enter the name of your SQL server in
the Database Server text box. For example, if you’re using the local
SQL Express instance, you will type .\SQLExpress. In the Database Name text box, type AdventureWorks. Accept the default connection option of Connect With User’s Identity. Click on OK to create the connection.
Define SpecificFinder Operation
After
the connection has been verified, in the Data Source Explorer tab,
you’ll be able to see the objects in the database. Expand the Tables
node and then scroll down to the ProductModel table. Right-click the
table, and in the context menu, you’ll see a number of options for
defining operations on the ProductModel entity. Select New Read Item
Operation. The
Read Item Wizard will start, where you can define the metadata for a
new SpecificFinder operation. In the Operation Name text box, type Read Item. Click Next to continue. On the Input Parameters page, you’ll notice a few important things: ProductModelID
is highlighted as an identifier. Each entity must define at least one
identifier, though more than one identifier can be defined. For
example, if you have a many-many relationship between two tables in a
database, you may have a join table with a compound primary key. In
this case, you’d have two identifiers. Each
item has a Display Name and Default Value option. Changing the Display
Name in this page has no effect when creating external lists since the
display name used by SharePoint is taken from the return values.
Changing the Default Value will apply a default if the input parameter
is null.
Accept the default settings by clicking Next to move to the next page. On the Return Parameter page, notice a few more options: Each
field can be checked or unchecked, although unchecking the
ProductModelID yields an error since each entity must have an
identifier. Unchecking other fields may raise a warning if the field is
not nullable in the underlying table, because updates and additions
will be impossible since a value is required by the database schema. Remember
that even though we’re defining a SpecificFinder operation, unchecking
columns here affects the overall definition of the entity. This means
that it will not be possible to add or update values in fields that are
unchecked even though the operations to perform these actions are
defined separately. In effect, the SpecificFinder defines the columns
of any external lists created from the External Content Type and
consequently the columns used when adding and editing data. A
number of parameters appear for each field, including a Map to
Identifier checkbox. Identifiers are defined in metadata separately and
must be mapped to fields using this option. The
Display Name property defines the column name as it appears in External
Lists created from the external content type. It also defines the text
on the label that appears next to the item in add and edit forms. Click Finish to complete the creation of the Read Item operation.
Note
If we had an additional entity that could be
correlated by using a particular field—such as Name—you would imagine
that we could flag Name as an identifier, allowing associations between
the entities to be made. However, this change has some undesirable
implications: since Name is now an identifier, we effectively have a
compound primary key. This means that all associations would be created
based on both the Name and ProductModelID fields. It would not be
possible to create an association based on one field or the other in
isolation, thus defeating the object of the change.
Define Finder Operation
Now
that we have defined a SpecificFinder operation to retrieve individual
items from our data source, the next requirement for creating an
external list is to define a Finder operation. The external list, like
all other lists in SharePoint, makes use of the XsltListViewWebPart to
render the contents of the list. However, rather than retrieving the
list contents from the SharePoint database, the View definition
contains a Method element specifying the name of a Finder operation
that’s been defined on the external content type.
Creating a Finder operation follows a similar process to the creation of the SpecificFinder operation:
Right-click
a table on which to define the operation and then select the type of
operation from the context menu. In this case, we’ll create a new Read
List operation on the ProductModel table. In the wizard that appears, set the Operation Name to Read List, and then click Next to continue. On
the Filter Parameters Configuration page, you’ll notice in the Errors
and Warnings section a warning message relating to the creation of a
limit filter. By default, a limit filter is not created, and this has
implications when creating external lists. The default maximum number
of rows that can be supported by an external list is 2000.
If the Finder operation returns more rows than this, we’ll end up with
a pretty cryptic web part error when we try to view the data in our
external list.
Tip
Even when external lists are not required,
by not setting a filter, we’re allowing the BDC service to return all
rows in a table. In most cases, this would represent a significant
waste of system resources.
|