How do i connect to that

32
How Do I Connect to That? Becky Bertram SharePoint MVP, Microsoft Certified Trainer MCSD, MCTS, MSPD, MSITP www.beckybertram.com Twitter: @beckybertram

Transcript of How do i connect to that

Page 1: How do i connect to that

How Do I Connect to That?Becky Bertram

SharePoint MVP, Microsoft Certified TrainerMCSD, MCTS, MSPD, MSITP

www.beckybertram.comTwitter: @beckybertram

Page 2: How do i connect to that

Display information from external systems in UI, such as web parts, application pages, etc.

Allow users to interact with external data in SharePoint list format

Crawl back-end data sources so they show up in search results

Import external data into user profiles

Some Reasons for Connecting to External Data

Page 3: How do i connect to that

ADO.NET LINQ to SQL ADO.NET Entity

Model SharePoint BCS

Options for Data Connection from SharePoint

Page 4: How do i connect to that

Use ADO.NET object model to connect to back-end data sources: Connection, Command, Parameter objects ExecuteScalar(), DataReader.Read() methods DataTable, DataSet, DataAdapter objects

ADO.NET

Page 5: How do i connect to that

SqlConnection conn = new SqlConnection()conn.ConnectionString = “DataSource=SERVER;Initial Catalog=OrderMgmt;Integrated Security=SSPI”;SqlCommand command = new SqlCommand(“sp_GetOrders”, conn);conn.Open();SqlDataReader reader = command.ExecuteReader();DataTable dt = new DataTable();dt.Load(reader);reader.Close();

Sample Code

Page 6: How do i connect to that

A way of using strongly typed classes to query back-end data sources using a T-SQL-like syntax

Examples of LINQ: LINQ to SQL LINQ to XML LINQ to SharePoint

What is LINQ?

Page 7: How do i connect to that

Strongly typed classes generated by the SqlMetal.exe tool

Visual Studio allows you to add LINQ to SQL project types to your project

VS gives you a visual interface that dynamically generates classes behind the scenes for you

DataContext class provides entry point for code

What is LINQ to SQL?

Page 8: How do i connect to that

CustomersDataContext dc = new CustomersDataContext(“DataSource=SERVER;Initial Catalog=OrderMgmt;Integrated Security=SSPI”);IQueryable<Customer> customers = from Customer c

in dc.Customerswhere c.Name == "Hot Dog Heaven"select c;

Customer customer = customers.First<Customer>();int customerId = customer.ID;

LINQ to SQL Sample

Page 9: How do i connect to that

An entity is a generic term for something that has properties and instructions for performing actions A database table has columns (properties) and uses

SQL statements or stored procedures to carry out CRUD operations

An Excel spreadsheet has rows and columns and uses functions to carry out operations

An object model has classes with properties and methods to carry out operations

What is the ADO.NET Entity Model?

Page 10: How do i connect to that

SQL Diagram vs Entity Model

The ADO.NET Entity model allows you to create an object model that mirrors your application logic and not necessarily your data source

Page 11: How do i connect to that

VS 2010 gives you a nice interface for creating Entity Models

VS gives you two automatic options: Create an entity model by hand, then have VS

create a SQL script that will generate a database that mirrors your entity model

Connect to a database and have an entity model created automatically that reflects the DB design

You also can create an entity model and manually wire entities up to stored procedures (thus creating a more de-coupled model)

Entity Models and Visual Studio

Page 12: How do i connect to that

It is possible to use LINQ with an Entity Model, i.e. the LINQ statements returns Entity Model objects

The Entity Model itself isn’t usually tied to LINQ to SQL

LINQ and Entity Models

Page 13: How do i connect to that

All three of these models require connection strings to connect to the backend database

In typical ASP.NET applications, connection strings are stored in the web.config file so they can be swapped out easily in each new environment (Dev, QA, Prod, etc.)

SharePoint WFE can be load balanced, so it’s not recommended to modify the web.config by hand

SharePoint object model provides a way of programmatically modifying the web.config file on multiple WFEs.

Managing Application Configuration

Page 14: How do i connect to that

SPConfigModification configMod = new SPWebConfigModification(); configMod.Name = “add”; configMod.Owner = “sp_serviceAccount”; configMod.Sequence = 1; configMod.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode; configMod.Value = “DataSource=SERVER;Initial Catalog=OrderMgmt;Integrated Security=SSPI”; webApp.WebConfigModifications.Add(configMod); webApp.WebService.ApplyWebConfigModifications();

SPWebConfigModification

Page 15: How do i connect to that

SPWebConfigModification solution requires writing your connection string in code; defeats the purpose of using the web.config

Other storage option: store the connection string in SharePoint itself Store in the property bag of a SPWebApplication, SPSite, or SPWeb

object Plus: easily accessible via code; can be different for each environment Minus: No user interface out of the box; UI for updating those values must

be written Store in a SharePoint list

Plus: UI already built; security, change management, etc. there already Minus: Not so good performance; list couple be deleted more easily than

accidentally deleting a property in the property bag, potentially SharePoint Patterns and Practices:

Variation of property bag Allows you to set config values higher up in the hierarchy and then query

them within a lower context; no hardcoded to setting values specifically in an object such as a web application, site collection, etc.

Environment-specific Connection Strings

Page 16: How do i connect to that

Best practice is to connect to SQL using a trusted connection (as opposed to using SQL authentication)

This means users will be connecting to the back-end database using the SharePoint web application’s application pool process identity. You must grant that account the proper permissions in the external database.

Connection Context

Page 17: How do i connect to that

A means of connecting SharePoint 2010 and Office applications to external data sources.

The term BCS refers to the connectivity components, tools, and user interface components such as web parts, that combine to make integration with external systems possible.

Business Data Connectivity (BDC) is refers to just the connectivity runtime. (Business Data Catalog is the SharePoint 2007 term for the BCS.)

Unlike SP2007 that only allowed Read functionality, BCS allows standard CRUD operations on the external system.

Can be used to search external data sources Can be used as a data source for the User Profile Service

What are Business Connectivity Services (BCS)?

Page 18: How do i connect to that

Authentication Type

Kerberos or NTLM

Description

Passthrough Kerberos Uses credentials of current userPassthrough NTLM Uses credentials of app pool account or anonymous

user accountRevertToSelf Uses credentials of the app poolWindowsCredentials

BCS uses SSS credentials as Windows credentials. Must pass in Target Application name.

RdbCredentials Passes in username and password to a DB, appended to the connection string (such as when using SQL Authentication). Must pass in Target Application name

Credentials Used to access web services that don’t support Windows authentication, but use Basic or Digest authentication. Must pass in Target Application name

Application-Level Authentication

Passes credentials along with parameters when carrying out a Read operation on external data source.

BCS Authentication Choices

Page 19: How do i connect to that

Provides the ability to map credentials of SharePoint users or groups to user accounts used for accessing external systems.

Can specify if each user gets mapped to an individual account, or if all the users in a given group map to a single account.

Each SSS entry has an “Application Name”. It’s possible to create BCS connections that use the credentials stored with a particular Application Name. This means you could create the same Application Name in your different environments, but use different credentials. Your BCS model wouldn’t need to change.

Secure Store Service

Page 20: How do i connect to that

Also known as an Entity in Visual Studio, to be consistent with the BCS object model.

Similar to an object with properties when referring to object models, or tables with columns when referring to databases.

ECT could be something like “Customer”, “Product”, etc. ECT is like a traditional content type in that it includes a

collection of metadata to describe an item. Storage mechanism for ECT is totally different than a traditional SharePoint content type.

ECTs can have Associations, which is the equivalent of foreign keys.

External Content Types (ECT)

Page 21: How do i connect to that

Contains: ECT definitions ECT associations ECT security permissions

Created using: SPD Visual Studio 3rd Party Tools XML Editor Programmatically using BCS Administration object

model

Metadata Model

Page 22: How do i connect to that

<Entities> <Entity Namespace="http://sandbox.sp2010.com/sites/bdc" Version="1.0.0.0" EstimatedInstanceCount="10000" Name="Product" DefaultDisplayName="Product"> <Properties> <Property Name="Title" Type="System.String">Name</Property> <Property Name="ExcludeFromOfflineClientForList" Type="System.Boolean">true</Property> </Properties> <Identifiers> <Identifier TypeName="System.Int32" Name="ProductID" /> </Identifiers> <Methods> <Method IsStatic="false" Name="DeleteProduct"> <Properties> <Property Name="RdbCommandType" Type="System.Data.CommandType, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">StoredProcedure</Property> <Property Name="RdbCommandText" Type="System.String">[dbo].[DeleteProduct]</Property> <Property Name="BackEndObjectType" Type="System.String">SqlServerRoutine</Property> <Property Name="BackEndObject" Type="System.String">DeleteProduct</Property> <Property Name="Schema" Type="System.String">dbo</Property> </Properties>

Sample Entity from BDC Model

Page 23: How do i connect to that

<Parameters> <Parameter Direction="In" Name="@ProductID"> <TypeDescriptor TypeName="System.Nullable`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]" IdentifierName="ProductID" Name="@ProductID" /> </Parameter></Parameters><MethodInstances> <MethodInstance Type="Deleter" Name="DeleteProduct" DefaultDisplayName="Delete Product"> </MethodInstance></MethodInstances>

</Method> </Entity></Entities>

Sample Entity Cont’d

Page 24: How do i connect to that

Resource files can be used to upload unique language values, security settings, and connection values

You can export a BCS metadata model to XML, import that model to a different model, then import an additional resource file in each new environment. That resource file could contain server-specific connection info.

BCS Resource Files

Page 25: How do i connect to that

<?xml version="1.0" encoding="utf-8" standalone="yes"?><Model xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.microsoft.com/windows/2007/BusinessDataCatalog BDCMetadata.xsd" Name="BCS Product" xmlns="http://schemas.microsoft.com/windows/2007/BusinessDataCatalog/Resources"> <AccessControlList> <AccessControlEntry Principal="sp2010\administrator"> <Right BdcRight="Edit" /> <Right BdcRight="Execute" /> <Right BdcRight="SetPermissions" /> <Right BdcRight="SelectableInClients" /> </AccessControlEntry> </AccessControlList> <LobSystems> <LobSystem Name="BCS Test"> <LobSystemInstances> <LobSystemInstance Name="BCS Test"> <Properties> <Property Name="AuthenticationMode" Type="System.String">WindowsCredentials</Property> <Property Name="DatabaseAccessProvider" Type="System.String">SqlServer</Property> <Property Name="RdbConnection Data Source" Type="System.String">WIN-28QOC9KSJHL</Property> <Property Name="RdbConnection Initial Catalog" Type="System.String">BCSTest</Property> <Property Name="RdbConnection Integrated Security" Type="System.String">SSPI</Property> <Property Name="RdbConnection Pooling" Type="System.String">True</Property> <Property Name="SsoProviderImplementation" Type="System.String">Microsoft.Office.SecureStoreService.Server.SecureStoreProvider, Microsoft.Office.SecureStoreService, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Property> <Property Name="SsoApplicationId" Type="System.String">BDC</Property> <Property Name="ShowInSearchUI" Type="System.String"></Property> </Properties> </LobSystemInstance> </LobSystemInstances> </LobSystem> </LobSystems></Model>

Sample Resource File

Page 26: How do i connect to that

A SharePoint list that was created using the definition defined in an ECT.

Not every ECT needs to have an External List. An EL is simply an embodiment of the ECT in SharePoint, but it’s not necessary to have an EL to access an ECT. It’s also possible to use the BDC object model to access External Data directly, without having an EL.

EL forms can either use a standard List View Web Part or they can be generated using InfoPath.

Once data is in an EL, programmers can program against list items using the standard SharePoint object model

External List

Page 27: How do i connect to that

Web Service/WCF Service SQL Server .NET Connectivity Component Custom

Connectors

Page 28: How do i connect to that

You can use SharePoint Designer’s tools to create your BDC model

You can auto-generate SQL queries or specify stored procedures that need to be executed

SQL Connector

Page 29: How do i connect to that

Your BCS model can connect to .NET objects in an assembly, and those objects can handle doing the CRUD operations

Visual Studio allows you to create a BCS model using a project item template

Your BCS model is tied to .NET classes in VS. You can write custom code in the methods you define (typically CRUD methods) that access your ADO.NET entities or your LINQ to SQL DataContext objects.

You could create a web service that exposes your entity model or LINQ to SQL classes and connect your BCS model to it

.NET Connectors

Page 30: How do i connect to that

ADO.NET object model connecting directly to DB ADO.NET Data classes that use LINQ to retrieve

content from DB ADO.NET Entity Model that’s configured to

connect to a back-end DB BCS to SQL BCS to Web Service/WCF BCS to .NET classes which use an Entity Model

you’ve created to return values BCS to .NET classes that use LINQ to SQL to return

values

Summary: Connection Options

Page 31: How do i connect to that

ADO.NET, Entity Model, and LINQ to SQL: Connects to back-end database using SharePoint Web

Application IIS application pool identity Store connection string in web.config using

SPWebConfigModification or within SharePoint itself (using property bag, with or without the aid of the Patterns and Practices code library; or in a SharePoint list)

BCS: Store credentials right in the model or store credentials

using the Secure Store Service Change configuration in each environment by uploading

Resource files associated with the BCS model

Configuration Options

Page 32: How do i connect to that

ADO.NET Entity Framework on MSDN:http://msdn.microsoft.com/en-us/library/bb399572.aspx

Wrox Professional Business Connectivity Services in SharePoint 2010

Secure Store Service on MSDN:http://msdn.microsoft.com/en-us/library/ee557754.aspx

LINQ to SQL on MSDN:http://msdn.microsoft.com/en-us/library/bb386976.aspx

Managing Application Configuration on MSDN:http://msdn.microsoft.com/en-us/library/ee413935.aspx

BCS Meta Man by Lightning Tools: http://lightningtools.com/bcs/bcs-meta-man.aspx SharePoint Property Bag Settings 2010:

http://pbs2010.codeplex.com

Resources