Monday, June 30, 2014

Extending External List Capabilities to Add and Edit List Items

In my previous article I mentioned how to create an external list using SharePoint 2013 to read list and read a particular list item. in this article I am planning to show how to allow adding new list items and editing list items which were disabled earlier.

I will be referring to the Visual Studio solution I created in previous article which can be found here. The step by step guide is as follows.

1. Double click on VSDemoModel to open the VSDemoModel.bdcm in the designer. The Entity1 object will be available. Select it. In the 'BDC Method Details' window you will find the 'ReadList' and 'ReadItem' methods. Scroll down the window till you find a cubic icon followed by a text '<Add a Method>'. 

Upon clicking on it, a drop down list will appear. Select 'Create creator method' option. A new method will be available as 'Create'. 

If you check the 'Entity1Service.cs' class, you will realize a new static method named 'Create' is created inside the class.

2. Replace the method body which currently throws a NotImplementedException with the following code. 

return newEntity1;

The new class will look like the following. 

Now deploy the solution by right clicking the solution in the solution explorer. 

3. Go back to the 'BCSVS List' list created in the previous article and click the 'New Item' button which is now enabled in the Items tab of the ribbon. An error will be shown saying a default new form is not available. 

This is because of creating new items was not supported the time the list was created. The default display form is assigned during the time a list is created only if necessary. The solution is creating a new list using the same content type. 

4. Create a new list using the External List template named 'BCSVS List 1' using the same External Content Type with the data source named 'VSDemoModel'.

5. Go to the newly created list and click the 'New Item' button in the Items tab of the ribbon. There will not be an error this time and the form will be displayed. 

Define 'Identifier1' as '3' and 'Message' as 'New Welcome' and click 'Save' button. The list view will now be shown with 3 list items populated.

This is how the adding new items facilitated. Adding the update method is same as this. All you need to do is 'Create Updater Method' in the model and writing the implementation code. As it was earlier, a new method named  'Update' will be created in the Entity1Service class.

I have not added the implementation code to the Update method in the sample below. Feel free to write the implementation. 

The updated Demo Solution can be downloaded from here.

No comments:

Post a Comment