SharePoint Developer Team Blog
Brought to you by Microsoft teams working on SharePoint developer content, Visual Studio tools, and of course the platform itself!
In this article, we will see how to design a SharePoint BCS entity from a SQL stored procedure that takes some inputs and returns a list of records. The scope of the article is to show the use of OOB features provided by SharePoint Server and SharePoint designer to achieve this. We will also see how to use the OOB Business Data List web part to display the output of the stored procedure.
Contents
Overview.. 1
Verify Web Application settings. 2
Configure Business Data Connectivity service. 3
Create Stored Procedure. 4
Create the External Content Type. 5
Verify the ECT in the Central Admin. 11
Use Business Data List web part to show the external data. 14
Connecting Business Data List web part with other web parts. 16
Additional Resources. 19
Business Connectivity Services are a set of services and features that provide a way to connect SharePoint solutions to sources of external data and to define external content types that are based on that external data. This article contains a step by step guide to design an External Content Type from SQL stored procedures. The article will broadly cover the below topics,
· Define stored procedures to be used as a base for the external content type.
· Create External Content Type from the stored procedures. Add a “Read List” operation and show use of filters.
· Create a custom page to show a list of items from the content type. Show use of “Business Data List” web part and filters.
· Customizing the Business Data List web part by connecting it to custom Filter web parts.
Before beginning, you should have the following:
· Access to SharePoint 2010 Central Administration, including the business data connectivity service.
· Access to SharePoint Designer.
· A SharePoint 2010 web application with a site collection (blank site template). For more information, see Create a site collection (Office SharePoint Server). We will be using the Sharepoint - 80 application for these examples.
· Access to Microsoft SQL Server. For our examples we will consider that the SQL server is installed locally.
· A sample database for Microsoft SQL Server (“AdventureWorks2008R2” will be used in this example. See Sample Databases for Microsoft SQL Server 2008R2 to install.)
The objective of this exercise is to write a Stored Procedure that returns a list of Sales Orders from the AdventureWorks database and display these results by using the Business Data List web part. We will also see how we can use various filters (Sales Territory and Sales Year for our example) to limit the result set returned by the stored procedure.
Follow these steps to verify that the web application that will be used to display the external data is properly configured to use the Business Data Connectivity service.
1. Start SharePoint 2010 Central Administration.
2. Click Manage web applications under the Application Management heading.
3. Select the web application you will be using for these examples (e.g. Sharepoint - 80).
4. Click Service Connections in the ribbon.
5. You need to have checks by Business Data Connectivity Service. If it is not checked, check it and click OK. If you cannot check them, use the drop down at the top to change from default to custom first.
Follow these steps to verify the settings for Business Data Connectivity service.
In this step, we will be using the ‘AdventureWorks2008R2’ database to create a stored procedure that takes two optional parameters (Status and Territory) and returns a list of Sales Orders. The code to create the procedure is below.
USE [AdventureWorks2008R2]
GO
/****** Object: StoredProcedure [dbo].[GetSalesOrders] ******/
IF EXISTS (SELECT * FROM sys.objects WHERE
object_id = OBJECT_ID(N'[dbo].[GetSalesOrders]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[GetSalesOrders]
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
CREATE PROCEDURE [dbo].[GetSalesOrders]
@territory nvarchar(200) = null,
@year int = 2008,
@pageNo int = 1,
@limit int = 200
AS
BEGIN
SET NOCOUNT ON;
DECLARE @startIndex int
SET @startIndex = ((@pageNo - 1) * @limit) + 1
DECLARE @endIndex int
SET @endIndex = @startIndex + @limit - 1
DECLARE @salesTerritory nvarchar(200) = null
IF @territory IS NOT null
SET @salesTerritory = '%' + @territory + '%'
;
WITH [CTE] AS (
SELECT ROW_NUMBER() OVER (ORDER BY [OrderDate] DESC) AS [RowNumber]
,[SalesOrderNumber]
,[OrderDate]
,[DueDate]
,[ShipDate]
,[dbo].[ufnGetSalesOrderstatusText]([Status]) AS [Status]
,[SO].[CustomerID]
,[CUST].[LastName] + ', ' + [CUST].[FirstName] + ' '
+ ISNULL([CUST].[MiddleName], '') AS [Customer]
,[ST].[Name] AS [SalesTerritory]
,[SubTotal]
,[TaxAmt]
,[Freight]
,[TotalDue]
FROM [AdventureWorks2008R2].[Sales].[SalesOrderHeader] AS [SO]
INNER JOIN [AdventureWorks2008R2].[Sales].[Customer] AS [SC]
ON [SO].[CustomerID] = [SC].[CustomerID]
LEFT JOIN [AdventureWorks2008R2].[Person].[Person] AS [CUST]
ON [SC].[PersonID] = [CUST].[BusinessEntityID]
LEFT JOIN [AdventureWorks2008R2].[Sales].[SalesTerritory] AS [ST]
ON [SO].[TerritoryID] = [ST].[TerritoryID]
WHERE DATEPART(year, [SO].[OrderDate]) = @year
AND [ST].[Name] LIKE ISNULL(@salesTerritory, [ST].[Name])
)
SELECT * FROM [CTE]
WHERE [CTE].[RowNumber] >= @startIndex
AND [CTE].[RowNumber] <= @endIndex
END
In this step, we will use SharePoint Designer to create an External Content Type from the stored procedure created in the previous step.
1. Browse to the “SharePoint – 80” site in your development environment.
2. On the Site Actions drop-down, click Edit in SharePoint Designer.
3. In the Navigation pane, click External Content Types.
4. In the New group of the ribbon, click External Content Type.
5. In the External Content Type Information group, edit each of the parameters as below and leave the remaining to their default settings.
Property
Value
Name
SalesOrders
Display Name
Namespace
http://contoso.com
Version
1.0.0.0
6. Next to External System, click Click here to discover external data sources and...
7. Click on the Add Connection button.
8. In the External Data Source Type Selection dialog, choose SQL Server in the drop down. Click OK.
9. In the SQL Server Connection dialog, edit the parameters as below and click OK.
Database Server
(local)
Database Name
AdventureWorks2008R2
AdventureWorks2008R2_SalesOrders
Note: This connection will use the service account to connect to the database. In a production configuration, you may want to use one of the impersonated identities so that database access is controlled independently from that account.
10. In the Data Source explorer, expand the AdventureWorks2008R2_SalesOrders to find the stored procedure GetSalesOrders under the Routines node.
11. Right-click on GetSalesOrders to open a pop-up menu and click New Read List Operation.
12. Click the Next > button at the bottom of the Read List dialog.
13. On the Input Parameters page, click @territory in the Data Source Elements pane.
14. In the Properties pane, edit the values as below. Then, click Click to Add against the Filter property.
Sales Territory
Default Value
<<Null>>
15. In the Filter Configuration dialog, select New Filter. Edit the properties as below leaving the rest to their default. Then click OK.
New Filter
Filter Type
Comparison
Operation
Equals
16. Back in the Input Parameters page, click @year in the Data Source Elements pane.
17. In the Properties pane, edit the values as below. Then click, Click to Add against the Filter property as shown below.
Sales Year
2008
18. In the Filter Configuration dialog, select New Filter. Edit the properties as below leaving the rest to their default. Then click OK.
19. Similarly, on the Input Parameters page, click @pageNo in the Data Source Elements pane.
20. In the Properties pane, edit the values as below. Then click, Click to Add against the Filter property as shown below.
Page No
1
21. In the Filter Configuration dialog, select New Filter. Edit the properties as below leaving the rest to their default. Then click OK.
22. Back on the Input Parameters page, click @limit in the Data Source Elements pane.
23. In the Properties pane, edit the values as below. Then click, Click to Add against the Filter property as shown below.
Limit
200
24. In the Filter Configuration dialog, select New Filter. Edit the properties as below leaving the rest to their default. Then click OK.
Limit Filter
Is Default
Checked
25. Click the Next > button at the bottom of the Read List dialog.
26. On the Return Parameters page, click SalesOrderNumber in the Data Source Elements pane.
27. In the Properties pane, check the box for Map to Identifier and Show In Picker.
28. Click the Finish button.
29. Click the Save button in the Quick Access Toolbar to commit the changes.
In this step, we will verify the various settings for the External Content Type created in the previous step.
External System
Filters on Finder Methods
Limit Filter (Type: Limit)
Page No (Type: Comparison)
Sales Territory (Type: Comparison)
Sales Year (Type: Comparison)
5. Next, to verify the External System (the Database connection) setting, select External Systems in the drop down in the ribbon.
6. Next, to verify the External System (the Database connection) setting, select External Systems in the drop down in the ribbon. The service application information page should list an entry for AdventureWorks2008R2_SalesOrders.
Access Provider
SqlServer
Authentication Mode
User’s Identity
Initial Database Name
Note: This connection will use the service account to connect to the database.
In this step we will use a Business Data List web part to display a list of sales orders.
In this example we will see how we can connect the Business Data List web part used in the previous steps to another web part. In this step we will use a Choice Filter web part, which will be used to pass the ‘Sales Territory’ filter value to the Business Data List web part. Note that, in real applications this may not be necessary as this feature is already built in to the Business Data List web part as shown in our previous step.
Filter Name
Choices
Australia
Canada
Northwest
Show (Empty) value
For more information, see the following resources:
· Business Data Connectivity services administration
· Business Connectivity Services Schema Reference
· Stereotyped Operations Supported By BDC
Nice post.