In my final post in this article on the new Business Data Connectivity Model in Visual Studio 2010 for SharePoint 2010, I am going to finish up by completing my BDC model to include Create, Update and Delete methods before packing the solution and deploying it to a production server. I’ll upload the final solution as well.
First of all, if I am going to add manipulation capability to the data, I need some persistency. For the sake of “quick and easy”, I have amended the Customer and Address classes to be Xml serialisable so I can persist to an xml file. Not a best practise approach I admit but this is not the focus of the blog.
Having added the necessary logic for creating, updating and deleting a customer record in my LOB project, I now need to set about mapping this with the necessary BDC model changes.
First up: Create.
In the BDC Method Details Window, drop down the Add a Method list and select “Create Creator Method”:
This will automatically generate the expected parameters and the code behind method. Go to the BDC Model explorer and expand the Create method tree to expose the properties of the entity (NewCustomerEntity in my case). Select the Identifier property (CustomerId), and then in the Property Window, set the Creator Field property to TRUE:
Now, hook up the code behind method to the LOB method call. You will also need to ensure your entity class (CustomerEntity) as a parameterless constructor else this will cause a runtime error – I missed out his step initially and hit the error,
“Constructor on type ‘CrmBdcModel.CRMCustomerModel.CustomerEntity’ not found.”
Hit F5 to deploy. Before you jump into your new functionality, I found I had to remove the list and create a new external list in order to successfully use the new functionality. Once I had done this, I could click on the Create button and fill in the automatically generated form to create a new customer:
It is worth noting that the method generated for Create returns the new instance. In the more likely situation where you would not provide the Id, this would be create by your back-end system and returned to you as the return parameter. You can modify the field settings so that this filed does not appear in the Create dialog. In my example, I simply adding in the Id manually, and clicked Save.
The setup for Update and Delete is very similar. Just be aware that you will need to set the Pre Updater and Updater fields to CustomerId when setting up the Update Method as below:
The Delete method simply takes a CustomerId (string) and the In parameter. These two methods will then enable your Edit and Delete buttons when viewing a single item as well as adding them as options in the item the dropdown menu.
Deployment
Now we have finished the BDC Model methods and verified that it all works when debugging, we can move on to packaging the entire solution for deployment on a production server. You can test this out on your development machine too as you will perform the same steps.
When you debug your solution, it automatically creates a wsp file in the bin directory. This is the file that needs to be deployed. If it is not present, then right click on your BDC project and select “Package”. I found there were a couple of stumbling blocks to deployment that you may or may not experience yourself.
Firstly, on both my SharePoint 2010 Foundation systems, the SharePoint Administration Service was not set to start automatically. Without this service, your deployment of the solution will fail. So it is worth checking your services and starting this service if necessary:
Second quirk was that the deployment of the solution in Powershell would added as a scheduled job but this would never seem to run so I had to cancel the job and perform the deployment inside the Central Administration Console. Before you get to this stage though, you need to add the solution using the SharePoint PowerShell. You will find this in Start -> Programs -> Microsoft SharePoint 2010 Products -> SharePoint 2010 Management Shell (you will need to run it with Admin privileges). In the console, navigate to where you have copied your wsp file, and add the solution into your SharePoint Farm with the following command:
STSADM –o addsolution –filename CrmBdcModelPackage.wsp
You can then try to deploy the solution with the following command, however this did not work for me:
STSADM.EXE -o deploysolution -name CrmBdcModelPackage.wsp -immediate –allowgacdeployment
What I had to was log into the Central Administration console and take the following steps:
1. Go to System Settings -> Manage Farm Solutions
You should then see your added solution (but not yet deployed):
2. Click on your solution, then click “Deploy Solution” in the next window
3. You will then be presented with options for deployment, but if you leave everything as it is, this should deploy the solution immediately:
4. Once you have completed these steps your solution should be successfully deployed and ready to use to create an external list:
That’s about it. I’d love to hear from anyone else who has gone through the same experience and get any thoughts or suggestions on where this process and be improved. If you do download the solution, remember to set the SharePoint project to point at your local install of SharePoint and to change the path to the temporary xml customer data file as necessary.
The solution can be downloaded here