Wednesday, July 30, 2014

Modifying BDC Model Entity in Business Connectivity Services

In this article I am going to customize the BDC model in a way that the entity can be used to add many properties than the 2 that comes by default. This part is a little bit tricky and confusing for a conventional C# developer. But with practice and by understanding the concepts, this can be made easier. 

1. First of all I am going to create an empty SharePoint project using Visual Studio. This example will be done using Visual Studio 2010 and SharePoint 2010, but there will be no difference for Visual Studio 2012/2013 and SharePoint 2013.

2. Then I added a new Business Data Connectivity Model to the project named 'BdcDemoModel'. The project should look like this.

3. Remove the 'Entity1' objected from the BDC Designer. Then drag and drop a new 'Entity' to the BDC Designer from Toolbox. There will be an empty entity without any identifiers or methods. 

4. So my new requirement is to have a list of movies which will contain following types of data.
  • ID - int32
  • Title - string
  • Genre - string
  • Rating - int32

5. I deleted the Entity1Service.cs file and rename EntityService.cs file to MovieService.cs. Click on the Entity in the designer and rename its name in the properties as 'Movie'. Rename the Entity1.cs class to Movie.cs.

6. Replace the properties in the Movie class with following.

public partial class Movie
    public int ID { get; set; }
    public string Title { get; set; }
    public string Genre { get; set; }
    public int Rating { get; set; }

7. Now go to the BDC Designer and right click on the identifiers of the Movie model. Then click 'Add New Identifier'. Rename the newly added identifier and change the 'Type Name' to 'System.Int32'. The environment should look like the image below right now.

8. Select the 'Movie' model in the BDC Designer and navigate to BDC Method Details window. In case if you do not have the BDC Method Details window, make it appear by going View --> Other Windows --> BDC Method Details. 

Expand the <Add a Method> drop down and select 'Create Finder Method'. New set of options would appear in the BDC Method Detail window. Now go and check the MovieService class and you will find a 'ReadList' method created.

Again expand the <Add a Method> drop down and select 'Create Specific Finder Method'. New set of options would appear in the BDC Method detail window as earlier and a new 'ReadItem' method will appear created with an int type parameter named 'id'. 

9. Now select the Movie model in the BDC Designer and go to the BDC Explorer window. Expand the 'ReadList', then 'movieList', then 'MovieList'. 'Movie' will be the end node. Select it and change the 'Type Name' by replacing 'System.String' to 'Movie' from 'Current Project' tab. 

Then right click on 'Movie' and select 'Add type Descriptor'. Then rename it to 'ID' and Type Name to 'System.Int32'. Then set the Identifier property as 'ID'. 

Follow the same steps and add the other 'Type Descriptors' as follows. (Do not change the Identifier property)

Title --> System. String
Genre --> System.String
Rating --> System.Int32

10. Then expand the 'ReadItem' method.

Now you are good to go.

This way you can make the custom entities using the Business Connectivity Services. If you add the other methods for create, update and delete then the same procedure needed to be followed. 

No comments:

Post a Comment