in SharePoint 2010

BDC .NET Assembly Connector: Updating a BCS Entity

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:

View Operation

Adding the Update Method

In your BDC diagram pane, click on the entity to reveal the BDC Method Details pane.

BDC Method Details

At the bottom you can see the option to Add a Method. Click on that to reveal the available options:

Methods Available

Click on Create Updater Method. This will add the Update operation to the BDC model.

Update Method

And the diagram pane is also updated to hold the new Update method:

BookEntity

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.

Update Method

Switch to BDC Explorer and you can see the new parameter added:

BDC Model

Click on the new parameterTypeDescriptor. In the Properties pane, change:

  1. Name to ISBN 
  2. Set the Pre-Updater Field to True
  3. Select the Identifier as ISBN

Parameter Properties

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

Entity

Click on Edit Item and you will be presented with the dialog window to edit the current item 🙂

Edit Entity

Change the Author from Author1 to New Author and click Save

You should see the updated value for that entity!

Updated 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

Write a Comment

Comment

Time limit is exhausted. Please reload the CAPTCHA.

  1. Chak,

    Great post, good work!

    For the issue with the entity not updating, try deleting the entity from Central Admin prior to redeploying. While testing, I noticed the logs reported that the entity was not updated since it already existed, and deleting the entity prior to redeploying the solution forced the update.

    .b

  2. Hi,

    nice job!
    I’ve just try to add a Update method in mein Projct. It’s very helpful!
    But your code didn’t work with my Sharepoint. The Problem is :
    first, you can’t set both "ISBN" and "parameter" map to Identifier or they reference the same identifier. All data source dlements must habe a unique identifer value;
    if you set "ISBN" map to Identifier, you get a list, which does’t support updating exisiting items.
    if you set "parameter", u get the error when you save the modified items: "The TypeDescriptor with Name ‘parameter’ in the Method ‘Microsoft.SharePoint.BusinessData.MetadataModel.Static.Method’ on Entity (External Content Type) with Name ‘BooksListBDCModel.BdcModel1’ in Namespace ‘BookEntity’ is marked ‘PreUpdaterField’, but is contained by another TypeDescriptor marked ‘PreUpdaterField’. "

    Have you some idee and help me pls??

    Thankyou verymuch!

  3. Hi, the paragraph “Update Operation Parameters” save my life after one week I tried to implement Update Method for my BCS model in SharePoint 2013.
    Thank you so much!!!