Develop a Business Data Connectivity Model for SPS 2010 Using VS 2010

Introduction

The main objective of Business Data Connectivity in SharePoint 2010 is to develop a solution to integrate external data and services. SharePoint 2010 leverages external content type; this specific type is introduced in SharePoint 2010. External lists can be created in SharePoint's web user interface and the list downloaded offline into an application, such as Outlook, as a set of contacts. Any updates to contacts in Outlook will cause the data in the external system to update as well.

Components of BCS

  • BDC Metadata Store: This provides storage for a collection of external content types where each describe how to connect to the external store.
  • BDC Server Runtime: The BDC Server Runtime is responsible for back-end store connectivity to the external content type.
  • Security: Responsible for integrating Business Connectivity Service with Secure Store Service (SSS).
  • Solution Packaging: Solutions can be packaged as Visual Studio Tools for Office (VSTO) and delivered to a rich client.
  • UI: Display external data through a Web Part UI.
  • BDC Client Runtime: BDC Client Runtime enables offline operations, interacting with the external data cache.
  • Design Tools: Designer tools provide functionality for creating BCS solutions.

External Content Type

External content type is the mapping between SharePoint and the external system describing both the entities and behaviors of the data source. It is defined using metadata in an XML grammar, which is deployed to the BDC Metadata Store.

Creating an Entity with Visual Studio 2010

  • Select File -> New -> Project...
  • Choose Business Data Connectivity Model under SharePoint templates
  • Give a name to the Project. The new project wizard will ask you to choose a SharePoint site to use for debugging. Finally, Visual Studio 2010 stubs out code for an entity, an entity service, the model definition and a diagram file used to support the Visual Studio design surface.

Creating an Entity with Visual Studio 2010
Creating an Entity with Visual Studio 2010

The entity class describes the schema for the entity and is modeled as a typical class with public properties. The service file provides the implementation for CRUD (Create, Read, Update, and Delete) methods.

Modeling the Entity

Let's start by making Entity class with the code shown below.

namespace BDCDemo
{    
public partial class User {        
public int userID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }        
public string TwitScreenName { get; set; }             
public string TwitStatus { get; set; }
    }
}

Implementing the Methods

The scenario requires data to be aggregated from an online source such as Twitter, and a separate store such as a SQL Server database. TwitterHelper helps in obtaining data from Twitter and feeding it into User entity. The GetChannelWithCredentials method calls Twitter using a specific set of credentials. The GetUserssFromTwitter method returns a list of users and their current status and stores the data in a list of User objects. The GetUserFromTwitterByScreenName method retrieves a single user from Twitter based on the user's screen name and returns that data in a single User object.

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Channels;
using System.Xml.Linq;
namespace BDCDemo
{
    [ServiceContract]
public interface ITwitterFriends
{
        [OperationContract]
        [WebGet(UriTemplate="/statuses/friends.xml")]
        Message GetAllFriends();
        [OperationContract]
        [WebGet(UriTemplate = "/users/show.xml?screen_name={screenName}")]
        Message GetFriendByID(string screenName);
  }
public class TwitterHelper
{
public static List<User> GetUsersFromTwitter()
{
var cf = GetChannelWithCredentials();
using(cf)
{
ITwitterFriends proxy = cf.CreateChannel();
Message m = proxy.GetAllFriends();
List<User> users = (from user in 
XDocument.Load(m.GetReaderAtBodyContents()).Root.Elements("user")
select new User
{
            TwitterScreenName = user.Element("screen_name").Value, 
            TwitterStatus = user.Element("status").Element("text").Value
}).ToList();
return users;
}
}
public static User GetUserFromTwitterByScreenName(string screenName)
{
var cf = GetChannelWithCredentials();
using (cf)
{
            ITwitterFriends proxy = cf.CreateChannel();
        Message m = proxy.GetFriendByID(screenName);
        XElement user = XDocument.Load(m.GetReaderAtBodyContents()).Root;
        User c = new User
        {
                        TwitterScreenName = user.Element("screen_name").Value,                    
                 TwitterStatus = user.Element("status").Element("text").Value
        };
            return c;
}
}
private static WebChannelFactory<ITwitterFriends> GetChannelWithCredentials()
{
WebHttpBinding binding 
= new WebHttpBinding(WebHttpSecurityMode.TransportCredentialOnly);
binding.MaxReceivedMessageSize = 999999;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
binding.Security.Transport.Realm = "Twitter API";
Uri location = new Uri("http://www.twitter.com");
WebChannelFactory<ITwitterFriends> cf 
= new WebChannelFactory<ITwitterFriends>(binding, location);
cf.Credentials.UserName.UserName = "your_twitter_screenname_here";
cf.Credentials.UserName.Password = "your_password_here";
return cf;
        }
    }
}

Our database table is quite simple; here is its schema:

CREATE TABLE [dbo].[User]
(
 [UserID] [int] IDENTITY (1,1) NOT NULL, 

 [FirstName] [nvarchar](50) NOT NULL,

 [LastName] [nvarchar](50) NOT NULL,

 [TwitterScreenName] [nvarchar](100) NOT NULL,

 )

GetUser, uses LINQ to SQL to query the database for a single customer record and then obtains the current status for that user from Twitter.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BDCDemo
{
public partial class UserService
    {
private const string CONNECTION_STRING = @"Data Source=sqlserver2010demo\sqlserver;Initial Catalog=UserCRM;Integrated Security=True";
public static Customer GetUser(int userID)
        {            
using (UserCRMDataContext db = new UserCRMDataContext(CONNECTION_STRING))
{
                User user = (from c in db.Users
where c.UserID == userID
select new User
                    {
                        userID = c.UserID,
                         FirstName = c.FirstName,
                         LastName = c.LastName,
                         TwitterScreenName = c.TwitterScreenName
                     }).FirstOrDefault();
 
User friend 
= TwitterHelper.GetUserFromTwitterByScreenName(user.TwitterScreenName);
user.TwitterStatus = friend.TwitterStatus;                
return user;
};
}
public static IEnumerable<User> GetUserList()
{
List<User> friends = TwitterHelper.GetUsersFromTwitter();
List<User> users = new List<User>();
using (UserCRMDataContext db = new UserCRMDataContext(CONNECTION_STRING))
{
foreach (User friend in friends)
{
var cust = 
db.Users.FirstOrDefault(c => c.TwitterScreenName == friend.TwitterScreenName);
if (null != cust)
{
users.Add(new User
{
UserID = cust.UserID,
FirstName = cust.FirstName,
LastName = cust.LastName,
TwitterScreenName = cust.TwitterScreenName,
TwitterStatus = friend.TwitterStatus
});
}
}
return users;
};              
}
}
}

A sample body for the Updater method might look like the following code.

public static void Update(User user)
{
using (UserCRMDataContext db = new UserCRMDataContext(CONNECTION_STRING))
{
var u = db.Users.Single(c => c.UserID == user.UserID);
 db.SubmitChanges();
    };
}

Conclusion

External connectivity in SharePoint 2010 has an excellent feature to take lists offline and sync data. This list allows you to represent the data in an external content type such as – Generic List, Appointment, Contact, Task, Post. This in turn influences how the data are displayed and synchronized. There are many customizations, such as selecting the Contact type and taking the list offline into Outlook as a list of contacts, and if data looks like a calendar item, it can be modeled as an appointment in Outlook. These integrations provide the ability to update and synchronize external data back to the data source.

TAGS:

Microsoft, SharePoint 2010, backup, display, SharePoint Designer
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.