Creating a SharePoint 2016 External Content Type trough OData in VS 2015 with EF6

A few weeks ago, I started playing with Hybrid SharePoint Server 2016 scenarios, to see if there are any differences with SharePoint 2013. As I am mostly an IT Pro, usually I never did the OData Source myself, but had a developer set that up for me. This time, I was in my lab, and I was both the DEV and the SharePoint Admin that had to do the IT part. (Talk about DevOps). That and the fact that I had quite a lot of problems with getting it to work with Entity Framework 6, I decided to do a blog post for all the SharePoint Admins and Developers out there that want to create a SharePoint 2016 External Content Type with an OData Source.

This blog will really be a step by step that everyone could follow, so if you are a more experienced dev, you can probably skip most of the screenshots, but I am sure that if you’re an IT Pro and first time doing this, you will find it valuable.

Intro

Our Goal for this blog post is to get the following table, in SharePoint Server 2016. The Hybrid configuration will be done in another blog post, for this one, we simply want to make it work in SharePoint 2016 On-Premises.

SQL Server Management Studio results grid of the CUSTOMERS table with ID, NAME, AGE, ADDRESS and SALARY columns for six rows

You will need to have access to a SharePoint Development machine with Visual Studio, as well as a IIS server where you can deploy your WebService at after (This can be done on the SharePoint Server).

Note, and this one is especially for people (like me) who will simply do this for testing. You need to have a Primary Key in your table for this to work. Without a Primary Key, you will get strange errors. For production, I hope this will not be a problem, but if you create a quick Database for testing, make sure your table has a PK defined.

Creating the OData Source

Create a new Project of type ASP.NET Web Application and name it as you wish. (For this scenario, we disabled the checkbox for Application Insights , since we don’t want to use /configure it).

Creating a SharePoint 2016 External Content Type trough OData in VS 2015 with EF6

In the next page, select Empty, since we want an Empty Web Application where we will add our own stuff.

Creating a SharePoint 2016 External Content Type trough OData in VS 2015 with EF6

Now, we will need to start adding items in this Project, so right click on the project name, Add, and Add New Item.

Visual Studio Solution Explorer context menu on the ContosoWeb project with Add and New Item highlighted

Select ADO.Net Entity Data Model under the Data tab, and give it a proper name such as “ContosoModel”

Add New Item dialog in Visual Studio with ADO.NET Entity Data Model selected under Data and named ContosoModel.edmx

In the Entity Data Model Wizard, choose EF Designer from Database.

Creating a SharePoint 2016 External Content Type trough OData in VS 2015 with EF6

On the “Choose your Data Connection” page, click on “New Connection”

Entity Data Model Wizard Choose Your Data Connection page with an empty connection list and the New Connection button

Enter the Connection Information for your business needs. If you use Windows Authentication, it will use the account that the Application Pool you run your Web Services Site runs it to access the Database. (Once deployed). There are multiple other ways to configure it depending on your business needs, but for this demo, we will use Windows Authentication.

Creating a SharePoint 2016 External Content Type trough OData in VS 2015 with EF6

Authentication dropdown in the connection dialog listing Windows Authentication, SQL Server Authentication and two Active Directory options

After you configure it, click on Test Connection to make sure that everything is configured correctly.

Microsoft Visual Studio message box saying Test connection succeeded

Back to the “Choose Your Data Connection” Page, you will have your connection selected, and you can optionally change the connection setting in the Web Config.

Creating a SharePoint 2016 External Content Type trough OData in VS 2015 with EF6

Choose Entity Framework 6.x so we use the latest version available for our project.

Entity Data Model Wizard Choose Your Version page with Entity Framework 6.x selected over Entity Framework 5.0

On the next page, choose all the tables that you want included in the OData Service and give a good name to your Model Namespace.

Entity Data Model Wizard Choose Your Database Objects page with the CUSTOMERS and Employees tables checked and Model Namespace set to ContosoModel

If everything worked correctly, you will see a next page, with the columns of your table(s) in a designer. If you don’t, check out the error log at the bottom of your Visual Studio

ContosoModel.edmx designer in Visual Studio showing the CUSTOMER and Employee entities with their properties

After this is done, we need to add another item to our Project!

Creating a SharePoint 2016 External Content Type trough OData in VS 2015 with EF6

This one is the WCF Data Service, which should be under the Web Tab. Make sure to give it a proper name.

Add New Item dialog in Visual Studio with WCF Data Service 5.6.4 selected under Web and the name set to Contoso

A page will appear, mostly Pre-populated

Generated WcfDataService1 class in Visual Studio with the DataService TODO placeholder and the InitializeService method

And here is where it gets a bit tricky. The first thing we have to do is replace the “public
class
WcfDataService1 : DataService< /* TODO: put your data source class name here */ >”
Where we will have to replace it with DataService . (The connection string we created in Web.Config when creating our connection earlier). However, this will fail. The reason is that, Visual Studio wrongly defaults to using “DataService”, when this will not work with Entity Framework 6. We will need to make some changes!

In the Top Bar, under Tools > NuGet Package Manager, open the Package Manager Console

Visual Studio Tools menu with NuGet Package Manager expanded and Package Manager Console highlighted

In the Console, run the following command to get the Entity Framework Provider Package “Install-Package Microsoft.OData.EntityFrameworkProvider –Pre”. It should successfully install as seen in the following screenshot.

Package Manager Console after Install-Package Microsoft.OData.EntityFrameworkProvider -Pre, reporting the beta2 package installed to ContosoWeb

At the top of your file, add the “using System.Data.Services.Providers;” line to load the proper classes.

Contoso.svc.cs using statements in Visual Studio with the new using System.Data.Services.Providers line added at line 13

A bit lower in the file, change the DataService to EntityFrameworkDataService (Where ContosoEntities, is the connection string name you defined earlier in this blog post)

Contoso class declaration changed to inherit from EntityFrameworkDataService of ContosoEntities

Lower again, in this example, I used the “UserVerboseErros = true”, for debugging, but you can delete that line for production. More important, make sure to do a config.SetEntityAccesRules(“TableName”) EntitySetRights.(rights); . In my case, I gave them AllRead.

InitializeService method with config.UseVerboseErrors set to true and SetEntitySetAccessRule granting AllRead on CUSTOMERS and Employees

The Project is now ready, so I created a IIS Site called WebServices, as well as an Application called Contoso.

IIS Add Application dialog for the WebServices site with alias Contoso and physical path C:\inetpub\wwwroot\WebServices

From Visual Studio, Click on “Publish ProjectName”

Visual Studio Build menu with the Publish ContosoWeb option highlighted

From now on, the procedure might vary depending on where you’re publishing your project to. The screenshots I will put here are for deploying on localhost. On the First Page, select “Custom”

Visual Studio Publish Web dialog on the Profile step with Custom selected as the publish target

Enter a Profile Name for this custom Profile

Visual Studio New Custom Profile dialog with the publish profile name ContosoService entered

Enter the Server where you wish to deploy it (in my case localhost) and the Site name in format IISSiteName\ApplicationName

Publish Web Connection step using Web Deploy with server localhost, site name WebServices\Contoso and a destination URL

Also enter what the destination name will be, depending on the bindings you use on your IIS Site. Once you click on Validate Connection, you should see a green checkbox that appears near the button.

Publish Web Connection step showing the green check mark next to the Validate Connection button

In the Database, select the available Connection String.

Publish Web Settings step with the ContosoEntities connection string selected under Databases and Release configuration

On the next page, you will see the items that will be deployed, and since it’s a new deployment we will see a bunch of them. Simply click on Publish and it should only take a few seconds.

Publish Web Preview step listing the DLL files to be added to localhost WebServices\Contoso before clicking Publish

If Everything works correctly, You should be able to navigate to the URL where you deployed your webservice /Servicename.svc and see something similar to this. An XML with your tables inside!

Contoso.svc OData service opened in Internet Explorer showing an XML workspace with the CUSTOMERS and Employees collections

Next step, is to go to webservice/servicename.svc/TableName?TOP10 (Tablename IS Case Sensitive!) For example. https://webservices.learn-sp2016.com:55124/Contoso/Contoso.svc/CUSTOMERS?top10 . The result will depend on the browser. On IE you will see a RSS Feed Screen, with as you see, 6 results, but we can’t see the results.

Internet Explorer showing the CUSTOMERS OData query as an RSS feed page displaying 6 of 6 items without visible data

On Chrome, you will see an XML, and if you look closely, you will actually see the customer data in there!

Raw Atom XML from the CUSTOMERS OData feed in Chrome with customer entries including ID, NAME, AGE, ADDRESS and SALARY

Cool , so our OData Service works, and is able to show data from the SQL Database in the Browser. You can now close that Visual Studio solution, and start a new one! This time, is of type “SharePoint Add-in”. And don’t worry, we will not actually deploy it, so you don’t need to have Add-ins Configured for this to work. You will however need a Dev Site Collection!

Visual Studio 2015 New Project dialog with the SharePoint Add-in template selected under Office/SharePoint Web Add-ins, named ContosoECT

Enter the URL of your Dev Debugging Site, and then SharePoint-Hosted.

New SharePoint Add-in wizard with the debugging site URL https://sharepoint.learn-sp2016.com/sites/dev/ and SharePoint-hosted selected

For the API version, I will select SharePoint 2016, since that’s where I want to deploy this Add-in

New SharePoint Add-in wizard asking for the target SharePoint version with SharePoint 2016 selected

We will now need to add a New Item, Content Type for an External Data Source. And strangely, this is not in the “New Item” pop up as before, but directly on the Add!

Visual Studio Solution Explorer context menu with Add expanded and Content Types for an External Data Source highlighted

For the OData Service URl, enter the URL to your Service, and give it a name!

SharePoint Customization Wizard Specify OData Source step with the Contoso.svc service URL and the data source name ContosoDataSource

Select the Table(s) on which you want to create External Content Types and make sure to leave the checkbox at the bottom checked.

SharePoint Customization Wizard Select the Data Entities step with CUSTOMERS and Employees checked and Create list instances enabled

After this is done, you should have two .ECT files in your Project.

Solution Explorer External Content Types folder showing ContosoDataSource with CUSTOMERS.ect and Employees.ect files

Open the ECT file with the built in XML text editor in Visual Studio

Open With dialog for CUSTOMERS.ect with XML (Text) Editor selected from the list of editors

In the top of the document, within the Model element, you will see a Name attribute. This Name attribute is the name you selected when you connected to the OData source, such as NorthwindCustomersModel. The value of this name is the same in all of the ECT files created from the entities, but it has to be unique in order to use it in SharePoint. You will need to change the name based on the ECT you are using , for example CustomersTable

CUSTOMERS.ect XML in Visual Studio with the Model Name attribute and the Entity Namespace both highlighted as CustomersTable

Now, navigate to your project, copy this ECT File to somewhere on your desktop

File Explorer showing the ContosoDataSource project folder with the CUSTOMERS.ect and Employees.ect files

You can then go into Central Admin, BCS Service Application and upload the ECT File. Also, If you didn’t already do it, make sure you have set the permissions!

Import BDC Model page in SharePoint Central Administration with CUSTOMERS.ect selected as the BDC Model File and file type Model

Afterwards, make sure to use “Set Metadata Store”, to give it the required permissions

Set Metadata Store Permissions button on the Business Data Connectivity Service Application ribbon with the CustomersTable BDC model selected

After that is done, go to any Site Collection, and add an “External List”

Your Apps page in SharePoint 2016 Site Contents with the External List app highlighted under Apps you can add

Select your External Content Type

Adding External List dialog with the name ContosoCustomers and the CUSTOMERS (ContosoDataSource) External Content Type selected

And everything should work!

ContosoCustomers external list in SharePoint 2016 showing customer rows with ID, Name, Age, Address and Salary columns from the OData source

You now managed to show data in SharePoint, by using Business Connectivity Services consuming an OData Source! If you want to configure Hybrid BCS , you will need to follow a future blog post that will start from this point. I will link to it once it’s live!