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 Changes
The new books list:
static List<string> books = new List<string>
{
"0470099410,Book1,Author1,$25.50,Publisher1",
"0470099411,Book2,Author2,$35.50,Publisher2",
"0470099412,Book3,Author3,$45.50,Publisher3",
"0470099413,Book4,Author4,$55.50,Publisher4",
"0470099414,Book5,Author5,$65.50,Publisher5"
};
The updated ReadItem method:
public static BookEntity ReadItem(string id)
{
string book = books.FirstOrDefault(s => s.Contains(id));
if (book != null)
{
string[] b = book.Split(',');
return new BookEntity
{
ISBN = b[0],
Name = b[1],
Author = b[2],
Price = b[3],
Publisher = b[4]
};
}
return null;
}
The updated ReadList method:
public static IEnumerable<BookEntity> ReadList()
{
//TODO: This is just a sample. Replace this simple sample with valid code.
List<BookEntity> booksList = new List<BookEntity>();
BookEntity bookEntity;
foreach (string book in books)
{
string[] b = book.Split(',');
bookEntity = new BookEntity()
{
ISBN=b[0],
Name=b[1],
Author=b[2],
Price=b[3],
Publisher=b[4]
};
booksList.Add(bookEntity);
}
return booksList;
}
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:
public static void Update(BookEntity bookEntity, string parameter)
{
string book = books.FirstOrDefault(s => s.Contains(parameter));
if (book != null)
{
int index = books.IndexOf(book);
books.RemoveAt(index);
books.Insert(index,
String.Format("{0},{1},{2},{3},{4}",
bookEntity.ISBN,
bookEntity.Name,
bookEntity.Author,
bookEntity.Price,
bookEntity.Publisher));
}
}
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.
Edit Item
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