In my previous post, I showed you how to build a simple BDC model using the .NET Assembly Connector. SharePoint 2010 not only allows to perform read operations from external data sources, but also supports write operations to external data sources. This allows us to update or delete an entity (based on the Identifier). In this post, we will extend our previous BooksList BDC Model to include write operations.
To make the update process simpler, I have slightly modified the previous code. I have now introduced a static List that holds the list of all books and build the entity in ReadList and ReadItem methods from the static List.
The new books list:
The updated ReadItem method:
The updated ReadList method:
Current Operations in the external list
If you browse to your list and open the ECB menu, you can see there is only View option available:
Adding the Update Method
In your BDC diagram pane, click on the entity to reveal the BDC Method Details pane.
At the bottom you can see the option to Add a Method. Click on that to reveal the available options:
Click on Create Updater Method. This will add the Update operation to the BDC model.
And the diagram pane is also updated to hold the new Update method:
Update Operation Parameters
By default, the update method adds the entity as the only parameter of type In which means that this will be coming into the .NET Assembly Connector from SharePoint list. But for our demo, we have set the ISBN property as the Identifier and this is going to be the key field in terms of finding the required book. So, we should have another parameter of type In which is of type String and will be the ISBN property.
Click on Add a Parameter and select Create Parameter in the Update method in the BDC Method Details pane. This will create a new parameter.
Switch to BDC Explorer and you can see the new parameter added:
Click on the new parameterTypeDescriptor. In the Properties pane, change:
- Name to ISBN
- Set the Pre-Updater Field to True
- Select the Identifier as ISBN
We have now set that this parameter of name ISBN is the Identifier and a pre-updater field which essentially tells BDC to preserve the field’s previous value when the entity is being updated.
In the diagram pane, double click on Update method to open the code behind
Adding Update Code
Below is my update code:
We are only updating the List and when BDC calls ReadList operation for refreshing, it will pick up the updated books List, iterate and build the entity.
Build and deploy your solution.
Now you can see that you have the option of Edit Item
Click on Edit Item and you will be presented with the dialog window to edit the current item 🙂
Change the Author from Author1 to New Author and click Save
You should see the updated value for that entity!
P.S: If you encounter any errors clicking Edit Item, please re-create the External List. I am not sure the cause of the problem, but will investigate and update on this issue in this post as soon as I find the cause.
You can download the source code here