Thursday 14 October 2010

Business Data Connectivity and Web Services in Sharepoint 2010 - Creating a Currency FX list.

Business Data Connectivity services are probably one of the most powerful tools in Sharepoint 2010.  Basically Microsoft Business Connectivity Services solutions use a set of standardized interfaces to provide access to business data. As a result, developers of solutions do not have to learn programming practices that apply to a specific system or adapter for each external data source. Microsoft Business Connectivity Services also provide the run-time environment in which solutions that include external data are loaded, integrated, and executed in supported Office client applications and on the Web server.

In this sample we are going to navigate to the basics using a Currency FX web service based in SOAP 1.1 .

BDC can be used with Sharepoint Designer 2010 and Visual Studio 2010. The new designer is really powerful, but assuming we want to have flexibility so we are going to the Visual Studio 2010 path.

1- Open VS2010
2- File->Project->Sharepoint->2010->Business Data Connectivity Model

3- Write BdcModelCurrencyConverter as a name of the project.

4- Click Ok and a new window will ask you where to deploy the project, type the path of where you want to display your list.


5- Now it is time to rename our project, this is not a easy task, you can not just rename the BdcModel1 with the name you want, because you will en breaking everything. So! what are we going to do is remove the whole BdcModel1 tree and go to our project and right click "Add->New Item".

6- Then you will choose (under Sharepoint 2010) Business Data Connectivity Model, below type the name you want for your connector.

7- Once is there we will need to rename our Entity1 with the name we want. An entity is basically a class or a table where we will display, keep and update the data with the default events:
 -ReadList
 -ReadItem
 -UpdateItem
 -...

8- Go to Edit->Find and Replace and replace Entity1Service by EntityFXService and Entity1 by EntityFX.

9- Now we are going to add a Web Reference not a Service reference! . This is the address:
http://www.webservicex.net/CurrencyConvertor.asmx?WSDL . This web service provides real-time currency exchange.

10- We will call the "Web Reference" WSCurrencyConvertor.

11- The Solution should look like this:

12- Copy this code and paste it on EntityFX.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BdcModelCurrencyConverter.BdcModelCurrencyConverterModel
{
    /// <summary>
    /// This class contains the properties for EntityFX. The properties keep the data for EntityFX.
    /// If you want to rename the class, don't forget to rename the entity in the model xml as well.
    /// </summary>

    public partial class EntityFX
    {
        public string ExchangeRateTitle { get; set; }
        public string ExchangeRateValue { get; set; }
        public string ExchangeRateLastUpdate { get; set; }
    }
}


13- Then copy this code into EntityFXService.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BdcModelCurrencyConverter.BdcModelCurrencyConverterModel
{
   
    public class EntityFXService
    {
        public static EntityFX ReadItem(string id)
        {
            //TODO: This is just a sample. Replace this simple sample with valid code.
            EntityFX EntityFX = new EntityFX();
            EntityFX.ExchangeRateTitle = id;
            EntityFX.ExchangeRateValue = "0";
            EntityFX.ExchangeRateLastUpdate = "0";
            return EntityFX;
        }
      
        public static IEnumerable<EntityFX> ReadList()
        {
            WSCurrencyConvertor.CurrencyConvertor WS = new WSCurrencyConvertor.CurrencyConvertor();
            WS.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

            EntityFX[] entityList = new EntityFX[10];
            EntityFX EntityFX = null;

            //##EUR->GBP
            EntityFX = new EntityFX();
            EntityFX.ExchangeRateTitle = "EUR->GBP";
            EntityFX.ExchangeRateValue = WS.ConversionRate(WSCurrencyConvertor.Currency.EUR, WSCurrencyConvertor.Currency.GBP).ToString();
            EntityFX.ExchangeRateLastUpdate = DateTime.Now.ToShortTimeString();
            entityList[0] = EntityFX;

            //##EUR->USD
            EntityFX = new EntityFX();
            EntityFX.ExchangeRateTitle = "EUR->USD";
            EntityFX.ExchangeRateValue = WS.ConversionRate(WSCurrencyConvertor.Currency.EUR, WSCurrencyConvertor.Currency.USD).ToString();
            EntityFX.ExchangeRateLastUpdate = DateTime.Now.ToShortTimeString();
            entityList[1] = EntityFX;

            //##GBP->USD
            EntityFX = new EntityFX();
            EntityFX.ExchangeRateTitle = "GBP->USD";
            EntityFX.ExchangeRateValue = WS.ConversionRate(WSCurrencyConvertor.Currency.GBP, WSCurrencyConvertor.Currency.USD).ToString();
            EntityFX.ExchangeRateLastUpdate = DateTime.Now.ToShortTimeString();
            entityList[2] = EntityFX;


            //##USD->CAD
            EntityFX = new EntityFX();
            EntityFX.ExchangeRateTitle = "USD->CAD";
            EntityFX.ExchangeRateValue = WS.ConversionRate(WSCurrencyConvertor.Currency.USD, WSCurrencyConvertor.Currency.CAD).ToString();
            EntityFX.ExchangeRateLastUpdate = DateTime.Now.ToShortTimeString();
            entityList[3] = EntityFX;

            //##USD->JPY
            EntityFX = new EntityFX();
            EntityFX.ExchangeRateTitle = "USD->JPY";
            EntityFX.ExchangeRateValue = WS.ConversionRate(WSCurrencyConvertor.Currency.USD, WSCurrencyConvertor.Currency.JPY).ToString();
            EntityFX.ExchangeRateLastUpdate = DateTime.Now.ToShortTimeString();
            entityList[4] = EntityFX;

            //##USD->INR
            EntityFX = new EntityFX();
            EntityFX.ExchangeRateTitle = "USD->INR";
            EntityFX.ExchangeRateValue = WS.ConversionRate(WSCurrencyConvertor.Currency.USD, WSCurrencyConvertor.Currency.INR).ToString();
            EntityFX.ExchangeRateLastUpdate = DateTime.Now.ToShortTimeString();
            entityList[5] = EntityFX;

            //##EUR->INR

            EntityFX = new EntityFX();
            EntityFX.ExchangeRateTitle = "EUR->INR";
            EntityFX.ExchangeRateValue = WS.ConversionRate(WSCurrencyConvertor.Currency.EUR, WSCurrencyConvertor.Currency.INR).ToString();
            EntityFX.ExchangeRateLastUpdate = DateTime.Now.ToShortTimeString();
            entityList[6] = EntityFX;

            //##EUR->JPY
            EntityFX = new EntityFX();
            EntityFX.ExchangeRateTitle = "EUR->JPY";
            EntityFX.ExchangeRateValue = WS.ConversionRate(WSCurrencyConvertor.Currency.EUR, WSCurrencyConvertor.Currency.JPY).ToString();
            EntityFX.ExchangeRateLastUpdate = DateTime.Now.ToShortTimeString();
            entityList[7] = EntityFX;

            //##EUR->AUD
            EntityFX = new EntityFX();
            EntityFX.ExchangeRateTitle = "EUR->AUD";
            EntityFX.ExchangeRateValue = WS.ConversionRate(WSCurrencyConvertor.Currency.EUR, WSCurrencyConvertor.Currency.AUD).ToString();
            EntityFX.ExchangeRateLastUpdate = DateTime.Now.ToShortTimeString();
            entityList[8] = EntityFX;

            //##GBP->JPY
            EntityFX = new EntityFX();
            EntityFX.ExchangeRateTitle = "GBP->JPY";
            EntityFX.ExchangeRateValue = WS.ConversionRate(WSCurrencyConvertor.Currency.GBP, WSCurrencyConvertor.Currency.JPY).ToString();
            EntityFX.ExchangeRateLastUpdate = DateTime.Now.ToShortTimeString();
            entityList[9] = EntityFX;

            return entityList;
        }
    }
}

14- After doing all of this, go to BdcModelCurrencyConverterModel double click and be sure you can select the "BDC Explorer". Rename the EntityFX fields as below.

15- Press F5 to deploy the BDC and go to your admin website, select the BDC and add All users permissions.



16- Go to your website click on Lists and Create a new list.

17-Select External List:

18- Type the name for the list Currency FX, select the new BDC you want and the list will be created automatically:

19- A list like this should be displayed:

Any problems write a comment.