Creating a BCS .NET Connector using Visual Studio


 

  1. Launch Visual Studio.NET 2010 and create a new Empty SharePoint project. The project should be deployed as a Farm Solution.
  2. Add a new Business Data Connectivity Model item to the project and name it as NorthwindModel.
  3. Since this model is going to connect to the Northwind SQL Server database, we will use LINQ to SQL to establish this connectivity. Right-click on the NorthwindModel item in the project and Add a new item. Click on the Visual C# node and select LINQ to SQL Classes template. Name the file as NorthwindEntityClasses.

  1. Click on the Server Explorer link in the dbml designer. In the Server Explorer window that appears, right click on the Data Connections node and click Add Connection.

  1. In the Choose Data Source dialog, select SQL Server as the data source and click Continue.
  2. Specify the Server name where the Northwind database is available and select the Northwind database from the dropdown.
  3. Expand the list of tables and select the Product table and drop it in the designer surface.

  1. Delete all columns from the table except ProductID, ProductName and UnitPrice. This will give us a class named Product which will have just 3 properties.

  1. Since we are going to be working with the Product class generated by the LINQ to SQL designer and not the Entity1 class generated automatically by Visual Studio, delete the Entity1.cs file from the project.

  1. Open the Entity1Service.cs file. This class is going to be the .NET connector that the bdcm (BDC Model) file is going to connect to. Delete all the methods that are currently present in this class.
  2. Add the following code to the class. The code contains two methods – ReadItem and ReadList. The ReadItem method takes in a ProductID as an input parameter and returns one product object as output. The ReadList method returns all the products present in the database. (Note : Verify the connection string and make sure it is valid for your environment)

public
static
Product ReadItem(int ProductId)

{


NorthwindEntityClassesDataContext northwind = new
NorthwindEntityClassesDataContext(“server=localhost;initial catalog=Northwind;integrated security=true”);

 


var query = from p in northwind.Products


where p.ProductID == ProductId


select p;


return query.ToList()[0];

}

 


public
static
IEnumerable<Product> ReadList()

{


NorthwindEntityClassesDataContext northwind = new
NorthwindEntityClassesDataContext(“server=localhost;initial catalog=Northwind;integrated security=true”);

 


var query = from p in northwind.Products


select p;


return query.ToList();

}

 

  1. Save and close the Entity1Service.cs file and open the NorthwindModel.bdcm file.
  2. In the BDC Explorer window, change the value of the Name property of Entity1 to Product.

  1. Since the Identifying attribute for our Product entity is ProductID and it’s datatype is Integer, make the necessary changes as shown below.

  1. In the BDC Explorer window, expand the ReadItem method and click on the id parameter. Change its name to ProductID.

  1. Since ProductID is an integer, expand the type identifier of ProductID and change its Type Name property to System.Int32.

  1. Expand the returnparameter and rename the Entity1 node to Product. Change the value of its Type Name property to point to the Product class instead of Entity1.

  1. Change the name and Type of the first type descriptor to ProductID and System.Int32.
  2. Change the name and Type of the second type descriptor to ProductName and System.String.
  3. Add a third type descriptor and specify UnitPrice as its Name and System.Decimal as its Type.

  1. Now expand the ReadList method. This method doesn’t have any input parameters and has just a return parameter. Click Entity1List and change its Type Name property to point to the Product class instead of Entity1. Also ensure that the IsEnumerable checkbox is enabled. You can also optionally rename Entity1List to ProductList.

  1. Delete the Entity1 node under the current node.

  1. Copy the Product node from the ReadItem method under the Entity1List node.

  1. Deploy the project.

Configuring and using the External Content Type

 

  1. Launch SharePoint Central Administration. Under the Application Management Group, click Manage Service Applications.
  2. Click on the Business Data Connectivity Service Application. You should be able to see your external content type that you just deployed in this list.
  3. If necessary Create a Profile Page for this content type.
  4. If necessary Set Permissions on this content type.
  5. Go to any valid SharePoint site and create a new External List. Link your external list with the external content type you deployed in the previous step (Product).
  6. The list should execute your ReadList method and display the list of products.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: