SharePoint 2010 User Profile Service: Part 2 Developing the WCF service
In this post I will start talking about using the UPS and how we can use that to connect to two sources of user profiles data. Please refer to the previous post on how to configure the UPS.
The Scenario
So lets first describe the scenario at hand. We have user profiles data residing in two sources, one is the Directory Service (AD for example), the other is any data source like SQL or Oracle DB. So we will use a custom Windows Communication Foundation Service to communicate with the custom data source.We will then use the SharePoint BCS to connect to this data source. The UPS will have to consider this new source of data while synchronizing the user profiles.
The Steps
I will list here all the steps we are going to implement along the way. Although these steps are going to be separated between several blogs.
- Configure the UPS correctly. This is already described in the previous post.
- Develop and deploy the WCF service. This will be described in this post.
- Connect to the WCF using BCS.
- Connect the UPS to the data sources.
- Add the new user profile properties and configure the link property.
- Start the user profile synchronization and create the synchronization schedule.
- Inspect the user profiles to make sure it is correct.
- (Optional) Configure search source to allow people searching and configure people search center.
In this post I will talk about the development and deployment of the WCF service. As an example I will develop a WCF service that connects to a SQL database but you can do the same to connect to any sort of data source including Oracle. Now this assumes that a table is created and populated with employee data. The table can be created using the following SQL query.
USE [EmployeeDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Employees](
[ID] [decimal](18, 0) NOT NULL,
[Salary] [decimal](18, 0) NOT NULL,
[FullName] [nvarchar](max) NOT NULL,
[Degree] [nchar](10) NOT NULL,
[MainDepartment] [nchar](50) NOT NULL,
[SubDepartment] [nchar](50) NOT NULL,
CONSTRAINT [PK_Employees] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
I will be using the ID as the linking property later between both user profiles sources.
Create WCF Project
Step 1: Left click on "File (menu item)" in "Microsoft Visual Studio (Administrator)" |
Step 2: Left click on "Project... (menu item)" in "&New" |
Step 3: Left click on "WCF Service Application (list item)" in "New Project" |
Step 4: Left click on "Name: (editable text)" in "New Project" |
Step 5: Keyboard input in "New Project" [Backspace Home ... Alt-Shift-Tab] |
Step 6: User left click on "OK (push button)" in "New Project" |
Step 7: User left click on "IService1.cs (outline item)" in "EmployeeWcfService - Microsoft Visual Studio (Administrator)" |
Step 8: User keyboard input in "EmployeeWcfService - Microsoft Visual Studio (Administrator)" [F2] |
Step 9: User keyboard input in "EmployeeWcfService - Microsoft Visual Studio (Administrator)" |
Step 10: User left click on "Yes (push button)" in "Microsoft Visual Studio" |
Step 11: User left click on "Service1.svc (outline item)" in "EmployeeWcfService - Microsoft Visual Studio (Administrator)" |
Step 12: User keyboard input in "EmployeeWcfService - Microsoft Visual Studio (Administrator)" [F2] |
Step 13: User keyboard input in "EmployeeWcfService - Microsoft Visual Studio (Administrator)" |
Step 14: User left click on "Character 26 (editable text)" in "EmployeeWcfService - Microsoft Visual Studio (Administrator)" |
Step 15: User keyboard input in "EmployeeWcfService - Microsoft Visual Studio (Administrator)" |
Step 16: User left double click on "Web.config (outline item)" in "EmployeeWcfService - Microsoft Visual Studio (Administrator)" |
Step 17: User keyboard input in "EmployeeWcfService - Microsoft Visual Studio (Administrator)" [... Ctrl-F] |
Step 18: User keyboard input in "Find and Replace" [... Enter Esc] |
Step 19: User keyboard input in "EmployeeWcfService - Microsoft Visual Studio (Administrator)" |
Step 20: User keyboard input in "Microsoft Visual Studio" [Enter] |
Step 21: User keyboard input in "EmployeeWcfService - Microsoft Visual Studio (Administrator)" |
Step 22: User keyboard input in "EmployeeWcfService - Microsoft Visual Studio (Administrator)" |
Add Database Connection
Step 1: User left click on "Server Explorer (menu item)" in "&View" |
Step 2: User left click on "Connect to Database... (push button)" in "EmployeeWcfService - Microsoft Visual Studio (Administrator)" |
Step 3: User keyboard input in "Add Connection" [...] |
Step 4: User left click on "Use SQL Server Authentication (radio button)" in "Add Connection" |
Step 5: User left click on "User name: (editable text)" in "Add Connection" |
Step 6: User keyboard input in "Add Connection" [... Tab] |
Step 7: User keyboard input in "Add Connection" [...] |
Step 8: User left click on "Save my password (check box)" in "Add Connection" |
Step 9: User left click on "Open (push button)" in "Add Connection" |
Step 10: User left click on "EmployeeDB (list item)" |
Step 11: User left click on "Test Connection (push button)" in "Add Connection" |
Step 12: User left click on "OK (push button)" in "Microsoft Visual Studio" |
Step 13: User left click on "OK (push button)" in "Add Connection" |
Create The Employee Dataset
Step 1: User right click on "EmployeeWcfService (outline item)" in "EmployeeWcfService - Microsoft Visual Studio (Administrator)" |
Step 2: User left click on "New Item... (menu item)" in "A&dd" |
Step 3: User left click on "Data (outline item)" in "Add New Item - EmployeeWcfService" |
Step 4: User left click on "DataSet (list item)" in "Add New Item - EmployeeWcfService" |
Step 5: User left click on "Name: (editable text)" in "Add New Item - EmployeeWcfService" |
Step 6: User keyboard input in "Add New Item - EmployeeWcfService" [Backspace Home ...] |
Step 7: User left click on "Add (push button)" in "Add New Item - EmployeeWcfService" |
Step 8: User left click on "sp2010.EmployeeDB.dbo (outline item)" in "EmployeeWcfService - Microsoft Visual Studio (Administrator)" |
Step 9: User left click on "Tables (outline item)" in "EmployeeWcfService - Microsoft Visual Studio (Administrator)" |
Step 10: User mouse drag start on "Employees (outline item)" in "EmployeeWcfService - Microsoft Visual Studio (Administrator)" |
Step 11: User mouse drag end in "EmployeeWcfService - Microsoft Visual Studio (Administrator)" |
Step 12: User left click on "Row 0 (row)" in "EmployeeWcfService - Microsoft Visual Studio (Administrator)" |
Step 13: User keyboard input in "EmployeeWcfService - Microsoft Visual Studio (Administrator)" [... Alt-Shift-Ctrl Alt-Shift-Tab] |
Step 14: User left click on "SELECT ID, Salary, FullName, Degree, MainDepartment, SubDepartment FROM dbo.Employees (tool tip)" |
Step 15: User left click on "CommandText (row)" in "EmployeeWcfService - Microsoft Visual Studio (Administrator)" |
Step 16: User left click on "row 0, column 7 (cell)" in "Query Builder" |
Step 17: User keyboard input in "Query Builder" [... Enter] |
Step 18: User left click on "Execute Query (push button)" in "Query Builder" |
Step 19: User keyboard input in "Query Parameters" [... Enter] |
Step 20: User left click on "OK (push button)" in "Query Builder" |
Step 21: User left click on "Yes (push button)" in "Microsoft Visual Studio" |
Step 22: User left click in "EmployeeWcfService - Microsoft Visual Studio (Administrator)" |
Step 21: User left click on "Yes (push button)" in "Microsoft Visual Studio" |
Add Service Code
In the file “IEmployeeService” add the employee data contract as follows:
[DataContract]
public class Employee
{
[DataMember]
public string ID;
[DataMember]
public decimal Salary;
[DataMember]
public string FullName;
[DataMember]
public string Degree;
[DataMember]
public string MainDept;
[DataMember]
public string SubDept;
}
And change the service contract to be as follows:
[ServiceContract]
public interface IEmployeeService
{
[OperationContract]
Employee GetEmployeeData(string EmpID);
}
In the file “EmployeeService.svc.cs” change the service implementation as follows:
public class EmployeeService : IEmployeeService
{
public Employee GetEmployeeData(string EmpID)
{
try
{
decimal EmpIdParam = decimal.Parse(EmpID);
Employee emp = new Employee();
EmployeeDataSet.EmployeesDataTable dt=new EmployeeDataSet.EmployeesDataTable();
EmployeesTableAdapter ta = new EmployeesTableAdapter();
ta.Fill(dt, EmpIdParam);
if (dt.Rows.Count != 1) return null;
emp.ID = dt.Rows[0]["ID"].ToString();
emp.Salary = decimal.Parse(dt.Rows[0]["Salary"].ToString());
emp.FullName = dt.Rows[0]["FullName"].ToString();
emp.Degree = dt.Rows[0]["Degree"].ToString();
emp.MainDept = dt.Rows[0]["MainDepartment"].ToString();
emp.SubDept = dt.Rows[0]["SubDepartment"].ToString();
return emp;
}
catch
{
}
finally
{
}
return null;
}
}
This completes the service implementation now we will deploy the service.
Deploy the WCF service to the IIS
We now deploy the developed WCF service to the IIS.
Step 1: User right click on "EmployeeWcfService (outline item)" in "EmployeeWcfService - Microsoft Visual Studio (Administrator)" |
Step 2: User left click on "Publish... (menu item)" in "Project" |
Step 3: User left click on "Publish (push button)" in "Publish Web" |
Step 4: User keyboard input in "EmployeeWcfService - Microsoft Visual Studio (Administrator)" [... Alt-Shift-Tab] |
Now this completes the WCF development and deployment and it is ready for use. In the next post I will show you how to connect to it using the SharePoint BCS.
Comments
- Anonymous
March 10, 2011
Great set of posts. I am building a sp2010 extranet and I am in an unusual situation where, for security reasons, my AD is located in a totally different forest than my SP farm. This means the UPS synch with AD directly wont work which is something of a showstopper. I like this WCF solution to add other LOB attributes to the SP user profiles - can I use WCF to get around my issue with AD as well? We also have just acquired BTS - could that also have a role presenting the WCF service or is that over the top ? Cheers John