VOOZH about

URL: https://www.cdata.com/kb/tech/salesforce-ado-odserv.rst

⇱ Provide OData Services of Salesforce Data from a WCF Application


Provide OData Services of Salesforce Data from a WCF Application

πŸ‘ Jerod Johnson
Jerod Johnson
Director, Technology Evangelism
In this article, we will demonstrate the process of generating an OData feed for Salesforce data by developing a WCF Service Application.

The CData ADO.NET Provider for Salesforce enables you to rapidly develop service-oriented applications using the Windows Communication Foundation (WCF) framework, providing Salesforce data data to OData consumers. This article guides you through creating an entity data model for connectivity and a WCF Data Service to expose OData services. You can then consume the feed with various OData clients, such as Power Pivot or applications using the CData ADO.NET Provider for OData.

About Salesforce Data Integration

Accessing and integrating live data from Salesforce has never been easier with CData. Customers rely on CData connectivity to:

  • Access to custom entities and fields means Salesforce users get access to all of Salesforce.
  • Create atomic and batch update operations.
  • Read, write, update, and delete their Salesforce data.
  • Leverage the latest Salesforce features and functionalities with support for SOAP API versions 30.0.
  • See improved performance based on SOQL support to push complex queries down to Salesforce servers.
  • Use SQL stored procedures to perform actions like creating, retrieving, aborting, and deleting jobs, uploading and downloading attachments and documents, and more.

Users frequently integrate Salesforce data with:

  • other ERPs, marketing automation, HCMs, and more.
  • preferred data tools like Power BI, Tableau, Looker, and more.
  • databases and data warehouses.

For more information on how CData solutions work with Salesforce, check out our Salesforce integration page.


Getting Started


Create the OData Service

Follow the steps below to create a WCF service application that will provide connectivity to Salesforce data via OData.

  1. Open Visual Studio and create a new project. Select the WCF Service Application template.
  2. Delete the autogenerated IService.cs and Service1.svc.
  3. Install Entity Framework 6:

    Use the Package Manager Console in Visual Studio to install the latest version of Entity Framework. Run the following command to download and install Entity Framework automatically:

    Install-Package EntityFramework
  4. Register the Entity Framework provider:
    1. Add the following provider entry in the "providers" section of your App.config or Web.config file. This section should already exist if the Entity Framework installation was successful.
      <configuration>
      ...
      <entityFramework>
       <providers>
       ...
       <provider invariantName="System.Data.CData.Salesforce" type="System.Data.CData.Salesforce.SalesforceProviderServices, System.Data.CData.Salesforce.Entities.EF6" />
       </providers>
      </entityFramework>
      </configuration>
    2. Add a reference to System.Data.CData.Salesforce.Entities.dll, located in lib/4.0 in the installation directory.
    3. Build the project to complete the setup for using EF6.
  5. Click Project -> Add New Item -> ADO.NET Entity Data Model.
  6. In the Entity Data Model wizard that is displayed, select the 'EF Designer from Database' option.
  7. In the resulting Choose Your Connection dialog, click New Connection.
  8. In the Connection properties dialog, select the CData Salesforce Data Source and enter the necessary credentials.

    A typical connection string is below:

    InitiateOAuth=GETANDREFRESH;MFACode=YourMFACode

    There are several authentication methods available for connecting to Salesforce: OAuth, Login (or basic), and SSO. The Login method requires you to have the username, password, and security token of the user.

    OAuth Authentication (default)

    The default authentication mechanism (and the one preferred by Salesforce) is OAuth. To use OAuth with CData's embedded OAuth application, leave the connection properties blank. If you have configured your own custom OAuth application with Salesforce (see the Help documentation for more information), set OAuthClientId, OAuthClientSecret, and CallbackURL to the properties for you application. Set InitiateOAuth to the desired OAuth flow ("GETANDREFRESH" will have the connector manage the entire OAuth flow).

    Login (or Basic) Authentication

    If you do not wish do not wish to use OAuth authentication, you can use Login (or basic) authentication. Set AuthScheme to Basic, and set the User, Password, and SecurityToken properties. You can configure your security token in Salesforce.

    SSO (single sign-on) Authentication

    SSO (single sign-on) can be used by setting the SSOProperties, SSOLoginUrl, and SSOExchangeURL connection properties, which allow you to authenticate to an identity provider. See the "Getting Started" chapter in the Help documentation for more information.

    Multi-Factor Authentication (MFA)

    If your Salesforce org has MFA enforcement enabled, set MFACode to the time-based one-time passcode (TOTP) generated by your authenticator app (such as Salesforce Authenticator or Google Authenticator). MFACode applies to both OAuth and Login authentication flows.

    πŸ‘ Required connection properties, specified in the Add Connection dialog. (Salesforce is shown.)
  9. Select Salesforce tables and views that you want OData clients to access. πŸ‘ Available tables in the Entity Data Model Wizard. (Salesforce is shown.)
  10. Click Project -> Add New Item -> WCF Data Service.
  11. Specify the data source class and configure access to the new WCF Data Service. In the example below, the Access Rule for the entities is set to All. This means that any user will be able to read and modify data.

     
    using System;
    using System.Collections.Generic;
    using System.Data.Services;
    using System.Data.Services.Common;
    using System.Linq;
    using System.ServiceModel.Web;
    using System.Web;
    
    namespace SalesforceService{
     public class SalesforceDataService : DataService<SalesforceEntities> {
     public static void InitializeService(DataServiceConfiguration config) {
     config.SetEntitySetAccessRule("*", EntitySetRights.All);
     config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
     }
     }
    }
    	
  12. Run the project. Applications that support OData can now access the Salesforce data and reflect any changes. You can access the feed in your browser. The feed will resemble the following: πŸ‘ The raw OData feed. (Salesforce is shown.)

Consume the OData Service from Power Pivot

You can now use the service from any OData client; for example, Excel Power Pivot.

  1. Open Excel and click on the Power Pivot Window button.
  2. A new pop-up will appear. Select the option From Data Feeds.
  3. In the resulting Table Import Wizard, enter the OData URL. For example, http://localhost:12449/SalesforceDataService.svc/. πŸ‘ The OData URL for Salesforce.
  4. After connecting to the OData service, click the Next button at the bottom of the window.
  5. A table listing of the available tables will appear in the next window of the wizard. Select which tables you want to import and click Finish. πŸ‘ Available tables in the Table Import Wizard. (Salesforce is shown.)
  6. Click Close to import the data in Power Pivot. πŸ‘ The table loaded in Power Pivot. (Salesforce is shown.)
CodeProject

Ready to get started?

Download a free trial of the Salesforce Data Provider to get started:

 Download Now

Learn more:

πŸ‘ Salesforce Icon
Salesforce ADO.NET Provider

Rapidly create and deploy powerful .NET applications that integrate with Salesforce account data including Leads, Contacts, Opportunities, Accounts, and more!