This example illustrates how to create a Business Connectivity Services (BCS) with an external content type using Visual Studio and utilize this content type in a SharePoint external list.
Feature Overview
An external list is a list
based on data from an external system outside of SharePoint such as a CRM system
or relational database.
Task 1: Create a new Business
Data Catalog Model project.
1. Open Visual Studio 2010 from the Start |
Programs | Visual Studio 2010 menu.
2. Create a new project by using File | New
Project.
3. Pick the SharePoint | 2010 templates.
4. From the SharePoint | 2010 templates select the
Business Data Connectivity Model
template.
5. Use BCSExample as the name.
6. Set the location to be C:\SPHOLs.
7. Press OK
to create the project.
8. On the SharePoint Customization Wizard dialog,
type the address of the site you want to deploy to, and select to deploy this as
a farm solution.
9. Click Finish to proceed.
Task 2: Extend the custom
entity to allow updating.
1. Click on the Entity1 | Methods area in the
designer.
2. This brings up the BDC Method Details window for
Entity1.
3. At the bottom of this window (you may need to scroll)
find the Add a Method area.
4. Open the combo box on the Add a Method area and
select Create Updater Method.
5. Select the newly created method called Update to
retrieve the properties pane. Change Is Static = True.
Task 3: Add the code to store
list data in an in memory collection.
1. Right click on Update and select View Code.
2. Add the following variable to the Entity1Service
class;
static
Dictionary<string,
string>
EntityCollection = new
Dictionary<string,
string>();
This variable will be used to
hold the lists state information. A real world application would use a durable
system such as a relational database instead.
3. Replace the existing method implementations with the
following code:
public
Entity1Service()
{
if
(EntityCollection.Count == 0)
{
// Create some
test list items.
Entity1
e1 = new
Entity1();
e1.Identifier1
= "e1";
e1.Message
= "e1
Item Data";
EntityCollection.Add(e1.Identifier1,
e1.Message);
Entity1
e2 = new
Entity1();
e2.Identifier1
= "e2";
e2.Message
= "e2
Item Data";
EntityCollection.Add(e2.Identifier1,
e2.Message);
}
}
public
static
Entity1 GetEntityById(string id)
{
// Looks up
the Entity information based on the id passed
Entity1
entity1 = new
Entity1();
entity1.Identifier1
= id;
entity1.Message
= EntityCollection[id];
return
entity1;
}
public
static
IEnumerable FindAllEntities()
{
// Iterates
through our backing
List
entities = new
List();
foreach
(String key in
EntityCollection.Keys)
{
Entity1
entity1 = new
Entity1();
entity1.Identifier1
= key;
entity1.Message
= EntityCollection[key];
entities.Add(entity1);
}
return
entities;
}
public
static
void
Update(Entity1 inParameter1)
{
// Updates the
corresponding item in the collection
EntityCollection[inParameter1.Identifier1]
= inParameter1.Message;
}
Task 4: Deploy and create an
entity backed list based on this example.
1. Right click on the BusinessDataCatalog1 project and
select deploy.
2. In the web browser click Site Actions -> View All
Site Content -> Create.
3. Beneath Custom Lists click External List.
4. Specify CustomList as the name of the new list.
Beneath Entity Type click the browse icon. Select the newly created Business
Data Type in the list and click OK.
The list will render the sample
list items we added in the entity’s constructor.
Selecting an item and pressing
edit allows changing of the field which in turn calls the Update method we
defined in the custom entity.
No comments:
Post a Comment