BCS model – a simple editable model

28 Aug

Let’s adjust the previous model to enable adding new items and edit existing ones. 

Open the previous project and open the design view of the model (.bdcm). In the BDC Method Details window select ‘Add a method’ and ‘Create Creator Method’. Two parameters are created, one for input, one as return parameter. Check the type names of both Type Descriptors. Both have to be set to the Customer entity. Probably they are set correctly already, because of the previous ReadList and ReadItem methods. Open the BDC Explorer and make sure the other type descriptors (the ones that make up a Customer) are present at the input and return parameter. Probably they also are there already.
Check the properties of the type descriptors of the input parameter ‘NewCustomer’. The property CreatorField is set to True here. With this parameter you can control the fields displayed on the new form. Since our CustomerId is an auto increment field, delete this type descriptor from the input parameter.
That’s it for the configuration, the only thing left is to update the code at the CustomerService class. As in the previous post I use Linq to Sql, an example:

        public static Customer CreateCustomer(Customer newCustomer)
        {
            using (LinqToSQLClassCustomerDataContext db = new LinqToSQLClassCustomerDataContext(CONNECTION_STRING))
            {
                CustomerBasic newItem = new CustomerBasic();
                newItem.CustomerName = newCustomer.CustomerName;
                newItem.CustomerCity = newCustomer.CustomerCity;
                db.CustomerBasics.InsertOnSubmit(newItem);
                db.SubmitChanges(); 

                Customer returnCust = new Customer
                {
                    CustomerId = newCustomer.CustomerId
                };

                return returnCust;
            }
        }

Deploy the solution and check out the list created on the previous version of this solution. The list seems fine and at the List Tools tab, Items, the New Item button is enabled. Select this button.
When expecting the new form, a Runtime Error occurs.
Open SharePoint Designer and select the list. In the next screen several items of the list are displayed, e.g. Settings, Views and… Forms! As you can see at the picture below there is no NewForm, that’s why the error occurs.
 

And checking the logfile:
SPException thrown: Message: Unable to find the default new form for list
Says the same, no New Form present. 

To get a full functioning list, create a new list based on the External Content Type and the NewForm is present:
  

Ofcourse SharePoint Designer can help you out without creating a new list by selecting NewForm at the Forms section:

When selecting ‘New Item’ in the browser a new Customer can be added.
    

The updater method is just as simple.
In the BDC Method Details window select ‘Add a method’ and ‘Create Updater Method’. One input parameter is created ‘customer’. Check the type name of the Type Descriptor. This has to be set to the Customer entity. Probably it is set correctly already, because of the previous defined methods. Open the BDC Explorer and make sure the other type descriptors (the ones that make up a Customer) are present at the input parameter. Probably they also are there already. Set the Read Only property of the CustomerId type descriptor to false and leave the Updater Field property set to True.    

Now update the code for the updated method to actually update the Customer.

        public static void UpdateCustomer(Customer customer)
        {
            using (LinqToSQLClassCustomerDataContext db = new LinqToSQLClassCustomerDataContext(CONNECTION_STRING))
            {
                CustomerBasic updateItem = (from c in db.CustomerBasics
                                            where c.CustomerId == customer.CustomerId
                                            select c).FirstOrDefault();
                updateItem.CustomerName = customer.CustomerName;
                updateItem.CustomerCity = customer.CustomerCity;
                db.SubmitChanges();
            }
        }

 

Deploy the solution and take a look at the list created with the previous version of the solution. Same story: Runtime Error; Edit Form not present. SharePoint Designer to the rescue: create a new Edit Form and voila!

3 Replies to “BCS model – a simple editable model

  1. Pingback: Tweets die vermelden BCS model – a simple editable model -- Topsy.com

  2. Hello!I am checking your blog for some weeks now. I have to admit that it is very informative. It is added in my favourite list and i will try to follow it frequently. Thanks for the interesting inputs . Moreover , i really like your theme and the way you have organised your site . Could you the name of your template ? Cheers

Comments are closed.