SharePoint Solution using BCS: Part II

In my initial post on this, I created a read-only application that used a Web service. The original post provided a step-by-step walkthrough of how to use the VS 2010 BDC Metadata model template and integrate it with Web services.

https://blogs.msdn.com/steve_fox/archive/2009/12/26/sharepoint-2010-development-using-bcs.aspx

In this post, I’ll add a couple of things to complement the original post: 1) a new web method on the service to update the external data source, and 2) the amendment you need to make to the BCS metadata model in VS 2010.

First, the code to add a customer to the external data source from the Web service is as follows:

[WebMethod]
public string updateCustomerData(string customerID, string custName, string custEmail, string custRegion, string custFY08Sales, string custFY09Sales)
{
int TempCustomerID = Int32.Parse(customerID);
string recordUpdate = "record(s) successfully updated.";

    CustomerSale myUpdatedCustomer = myCustomerData.CustomerSales.First(e => e.CustomerID == TempCustomerID);

    myUpdatedCustomer.CustomerName = custName;
myUpdatedCustomer.CustomerEmail = custEmail;
myUpdatedCustomer.CustomerRegion = custRegion;
myUpdatedCustomer.CustomerFY08Sales = Int64.Parse(custFY08Sales);
myUpdatedCustomer.CustomerFY09Sales = Int64.Parse(custFY09Sales);

    int numRecordUpdated = myCustomerData.SaveChanges();
return numRecordUpdated.ToString() + " " + recordUpdate;
}

In the code, you’ll note that the application that calls this web method must pass a number of parameters across to the service including the Customer ID, Name, Email, Region, and sales for FY 08 and FY 09. The method then takes the parameters and because in the db the CustomerID is an Int, I had to translate this using the Int32.Parse() method.

Using a straightforward lambda expression, I then created the myUpdatedCustomer object which got the first record in the db that equalled the customerID being passed into the web method. And then I updated the fields for this record and called the SaveChanges() method to commit the changes to the external data source.

After you’ve added your changes, you build and republish your changes to IIS. If you remember, we had created a site/web application in IIS and that’s where we deployed (that is referenced) our Service1.asmx file and accompanying code-behind files.

To build the appropriate update method in the BCS model, open the original VS 2010 project and first right-click the service reference and Refresh it so you have the latest service metadata loaded into the project. Then add the following code to your BDC model (the .bdcm file) and then find the Add a Method option in the Details pane and select Create Creator Method. This will add the appropriate method and stubbed metadata in your external content type model.

image

Make sure that your model (in the BDC Explorer) maps to the entity that you’ll pass across with the method call you’ll create below. 

image

And then the last thing you’ll want to do is to amend the new Create method with the following code to ensure that you can call the web service and pass the appropriate data across to create a new record in the external data source.

public static void Create(Entity1 newEntity1)
{
CustomerSalesWS.Service1SoapClient myWSProxy = new CustomerSalesWS.Service1SoapClient();
string[] sendingData = new string[6];

    newEntity1.Identifier1 = sendingData[0];
newEntity1.custName = sendingData[1];
newEntity1.custEmail = sendingData[2];
newEntity1.custRegion = sendingData[3];
newEntity1.custFY08Sales = sendingData[4];
newEntity1.custFY09Sales = sendingData[5];

    myWSProxy.addACustomer(sendingData[0], sendingData[1], sendingData[2], sendingData[3], sendingData[4], sendingData[5]);
myWSProxy.Close();
}

Redeploy the BDC Model project by clicking Build and Deploy. You should now be able to load the external list and then click Add new item to add a new item to the external list.

image