Bilbro Bloggins

Musings of Brian Bilbro

Yes, Virginia, you can write through the BDC – Part Two.

This is the second part of a three part blog series. 

  1. Part One - Using the Microsoft Business Data Definition Editor to add a table and display the data in a Web Part.
  2. Part Two - How to use the GenericInvoker to write data back through the BDC.
  3. Part Three – Download of all project source files.

For background information on this blog, I recommend reading the first part of this series if you have not done so already.  Part Two starts off with the BDC metadata file created in part one.  You will need that file to continue on.

 

Step 1 – Add a GenericInvoker method to perform inserts into the DimCurrency entity.

a. Open your BDC Editor with the AdventureWorks metadata definition file as you left it in part one.

b. Select the Add Method button with the DimCurrency method node selected.

Step01.AddMethod

c. Set the Name and DefaultDisplayName to Insert_DimCurrency.

d. Add the following INSERT statement to the RdbCommandType property.

INSERT INTO DimCurrency (CurrencyAlternateKey, CurrencyName) VALUES (@CurrencyAlternateKey, @CurrencyName)

Your Insert_DimCurrency method should look something like this:

Step01.b.InsertMethod

 

Step 2 – Add the input parameters for CurrencyAlternateKey and CurrencyName.

a. On the Insert_DimCurrency node, select the Add Parameter button.

b. Select In for the Parameter Type dialog and Ok button.

c. Set the Name and DefaultDisplayName to @CurrencyAlternateKey.

Step02.a.AltKeyParam

d. On the @CurrencyAlternateKey node select Create Root TypeDescriptor button.

Step02.b.AltKeyType

e. Set the Name and DefaultDisplayName to CurrencyAlternateKey.

Step02.c.AltKeydone

f. On the Insert_DimCurrency node, select the Add Parameter button.

g. Select In for the Parameter Type dialog and Ok button.

h. Set the Name and DefaultDisplayName to @CurrencyName.

i. On the @CurrencyName node select Create Root TypeDescriptor button.

j. Set the Name and DefaultDisplayName to CurrencyName.

Step02.d.done

This is about as far as the BDC Editor can take us.  One of the quirks of the BDC is that a return parameter is required for a method instance.   So, we are going to step out of the BDC Editor and use a text editor to add our final XML to the metadata definition file.

 

Step 3 – Add the Dummy return parameter and the method instance.

a.  Export the metadata file.   As before, I’m going to give it a new version on the filename.

b. Open the metadata file with your favorite text editor (notepad, Visual Studio, etc…).

c. Locate the Insert_DimCurrency method node.  It should look like this:

<Method Name="Insert_DimCurrency">
  <Properties>
    <Property Name="RdbCommandText" Type="System.String">INSERT INTO DimCurrency (CurrencyAlternateKey, CurrencyName) VALUES (@CurrencyAlternateKey, @CurrencyName)</Property>
    <Property Name="RdbCommandType" Type="System.Data.CommandType, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">Text</Property>
  </Properties>
  <Parameters>
    <Parameter Direction="In" Name="@CurrencyAlternateKey">
      <TypeDescriptor TypeName="System.String" Name="CurrencyAlternateKey" />
    </Parameter>
    <Parameter Direction="In" Name="@CurrencyName">
      <TypeDescriptor TypeName="System.String" Name="CurrencyName" />
    </Parameter>
  </Parameters>
</Method>

d. Add the following dummy return <parameter> in the <Parameters> node.

<Parameter Direction="Return" Name="DimCurrency_DummyReturnParam">
  <TypeDescriptor TypeName="System.Int32" Name="DimCurrency_DummyReturnParam" />
</Parameter>

e. Add the following GenericInvoker method instance below the <Parameters> node.

<MethodInstances>
  <MethodInstance Type="GenericInvoker" 
                  ReturnParameterName="DimCurrency_DummyReturnParam" 
                  ReturnTypeDescriptorName="DimCurrency_DummyReturnParam" 
                  ReturnTypeDescriptorLevel="0" 
                   Name="Insert_DimCurrency_Instance" />
</MethodInstances>

Your final Insert_DimCurrency method node should look like this:

<Method Name="Insert_DimCurrency">
  <Properties>
    <Property Name="RdbCommandText" Type="System.String">INSERT INTO DimCurrency (CurrencyAlternateKey, CurrencyName) VALUES (@CurrencyAlternateKey, @CurrencyName)</Property>
    <Property Name="RdbCommandType" Type="System.Data.CommandType, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">Text</Property>
  </Properties>
  <Parameters>
    <Parameter Direction="In" Name="@CurrencyAlternateKey">
      <TypeDescriptor TypeName="System.String" Name="CurrencyAlternateKey" />
    </Parameter>
    <Parameter Direction="In" Name="@CurrencyName">
      <TypeDescriptor TypeName="System.String" Name="CurrencyName" />
    </Parameter>
    <Parameter Direction="Return" Name="DimCurrency_DummyReturnParam">
      <TypeDescriptor TypeName="System.Int32" Name="DimCurrency_DummyReturnParam" />
    </Parameter>
  </Parameters>
  <MethodInstances>
    <MethodInstance Type="GenericInvoker"
                    ReturnParameterName="DimCurrency_DummyReturnParam"
                    ReturnTypeDescriptorName="DimCurrency_DummyReturnParam"
                    ReturnTypeDescriptorLevel="0"
                     Name="Insert_DimCurrency_Instance" />
  </MethodInstances>
</Method>

 

Step 4 – Update the version of the BDC metadata file.  Locate the <LobSystem> node at the top of the file.  Change the version attribute to 1.0.2.0.

Version="1.0.2.0"

 

Step 5 – Verify your file in BDC Editor.  Import your file into the BDC Editor.

If successful, it should look something like this:

step05.finalbdc

 

Step 6 – Update the metadata definition file in SharePoint.

a. Export your metadata definition.

step03.a.export

b. Open your Shared Services.  In the Business Data Catalog section, select Import application definition.

Browse to the file you just exported and then select Import.

Hopefully, everything went okay and you see the Application Definition Import Successful screen.

Step04.d.ImportSuccess_thumb1

 

Step 7 – Use the BDC API to call your GenericInvoker method to insert data into the DimCurrency table.

a. Open Visual Studio and create a new Console Application project.

Step06.NewConsoleApp

b.  In the main method add some variables that will be used to access the Insert GenericInvoker method.

string sspName = "SharedServices1";
string lobSystemName = "AdventureWorksDW";
string lobSystemInstanceName = "AdventureWorksDWInstance";
string entityName = "DimCurrency";
string methodInstanceName = "Insert_DimCurrency_Instance";
  • sspName – The name of your Shared Service provider
  • lobSystemName – The LOB System name of the Adventure Works BDC definition.  If you have been following along step-by-step with this blog this value specified above should be correct.
  • lobSystemInstanceName – The LOB System Instance name of the Adventure Works BDC definition.  If you have been following along step-by-step with this blog this value specified above should be correct.
  • entityName – The Entity Name of the Currency table.  If you have been following along step-by-step with this blog this value specified above should be correct.
  • methodInstanceName– The instance name of the GenericInvoker for the Insert method.  If you have been following along step-by-step with this blog this value specified above should be correct.

b.  Next, we’ll add some code to locate the AdventureWorks BDC instance.

// Connect to the BDC Metadata Instance
SqlSessionProvider.Instance().SetSharedResourceProviderToUse(sspName);
NamedLobSystemDictionary lobSystems = ApplicationRegistry.GetLobSystems();
LobSystem lobSystem = lobSystems[lobSystemName];
NamedLobSystemInstanceDictionary lobInstances = lobSystem.GetLobSystemInstances();
LobSystemInstance lobInstance = lobInstances[lobSystemInstanceName];

c. Find the specific method instance we are looking for:

// Locate the method desired
Entity entityObject = lobInstance.GetEntities()[entityName];
NamedMethodInstanceDictionary methodInstances = entityObject.GetMethodInstances();
MethodInstance methInstance = methodInstances[methodInstanceName];

d. Get the parameters of the method instance:

// Get the parameters to the method
Object[] methodArgs = methInstance.GetMethod().CreateDefaultParameterInstances(methInstance);

e.  Pass in the parameters values.  In this case, I’ve hard-coded the Serbian Dinar but in the more realistic scenario you would probably be reading the input values from a “New Currency” form.

// RSD, Serbian Dinar
methodArgs[0] = "RSD";
methodArgs[1] = "Serbian Dinar";

f. And finally, execute the method.

// Execute the method
entityObject.Execute(methInstance, lobInstance, ref methodArgs);
Console.WriteLine(String.Format("Method {0} Executed",methodInstanceName));

g. To complete this sample, we’ll need to add a couple of project references

  • Microsoft.Office.Server - C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI\Microsoft.Office.Server.dll
  • microsoft.sharepoint.portal - C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI\microsoft.sharepoint.portal.dll

Your paths may vary but those are the specific DLLs you will need to reference.

and then add the following to your using statements:

using Microsoft.Office.Server.ApplicationRegistry.MetadataModel;
using Microsoft.Office.Server.ApplicationRegistry.Infrastructure;
using Microsoft.Office.Server.ApplicationRegistry.Runtime;
using Microsoft.Office.Server.ApplicationRegistry.SystemSpecific.Db;

 

Step 8 – Compile and run this console app on your SharePoint server where you loaded the BDC metadata definition file.

Step08.run

(note: if you run this application more than once, you’ll get a SQL insert constraint error on the CurrencyAlternateKey.  The CurrencyAlternativeKey doesn't allow duplicate entries.)

 

Step 9 – If you view the BDC Data List web part you added in part one, you should see the newly added Serbian Dinar.

Step09.Verify

 

This is just a simple example of writing data back through the BDC.   The same techniques could be applied to UPDATE and DELETE statements.    However, it would probably be best to utilize stored procedures that has return values (success/fail results or for INSERTs, the newly added key).   Looking at the big picture you could write complete CRUD pages against your BDC data store following these guidelines:

  • Display a list of results from the BDC (as the BDC web part does today).
  • Add three custom action to have a selected item go your your custom edit form, delete form or new form.
  • The edit form brings up the item in a form edit mode.
  • The new form brings up an empty form.
  • The delete form is probably just a confirm dialog.
  • Use the GenericInvoker to perform UPDATE (edit) and INSERT (new) and DELETE statements (or Stored Procedure calls). 

It’s possible in the future I’ll blog with a specific example of the above process.

 

The console application and completed BDC metadata definition file can be downloaded from Part Three of this blog series.

» Similar Posts

  1. Yes, Virginia, you can write through the BDC – Part One.
  2. Creating a Lookup List for SharePoint with VSeWSS
  3. Dropdown Filter Web Part in SharePoint – Part Three

» Trackbacks & Pingbacks

    No trackbacks yet.
Trackback link for this post:
http://bilbroblog.com/trackback.ashx?id=13
 

» Comments

  1. Nick Swan avatar

    Hi,

    thanks for the link in the previous article. With BDC Meta Man we can generate the web part files for you that you need to do the write back from within SharePoint. We've got an article and screencast about it here:

    www.lightningtools.com/.../business-data-c

    Thanks

    Nick Swan

    Nick Swan — December 12, 2008 5:48 PM
  2. Brian Bilbro avatar

    No problem. Everywhere I go everybody recommends your tool. I've played with the Developer version you have but you can't really do much with the dev version (IMHO, it's more of a trial version than dev version).

    I'm purposely staying away from your tool right now so that I can learn the BDC inside and out. :) After that, I'll probably use it going forward.

    Brian Bilbro — December 12, 2008 6:40 PM
  3. Antony Henry avatar

    Hello mate,

    That was a really good article about write backs on the BDC. Just what a Sharepoint developed would need if a situation for write back arises.

    I agree that the BDC Metaman has some wonderful features, but this is the sort of thing a good developer should be able to achieve without much sweat. Once again...thanks for the aticle. Cheers.

    Antony Henry — May 27, 2009 12:57 PM
  4. Addis Abebayehu avatar

    I am impressed. This is excellent. I used to think BDC is read only.

    Addis Abebayehu — June 23, 2009 10:30 PM
  5. Tanmayee Nayak avatar

    Excellent Article...Thanks

    Tanmayee Nayak — June 16, 2010 10:41 AM

» Leave a Comment