Freigeben über


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.

  1. Configure the UPS correctly. This is already described in the previous post.
  2. Develop and deploy the WCF service. This will be described in this post.
  3. Connect to the WCF using BCS.
  4. Connect the UPS to the data sources.
  5. Add the new user profile properties and configure the link property.
  6. Start the user profile synchronization and create the synchronization schedule.
  7. Inspect the user profiles to make sure it  is correct.
  8. (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)"

clip_image001[4]

 

Step 2: Left click on "Project... (menu item)" in "&New"

clip_image002[4]

 

Step 3: Left click on "WCF Service Application (list item)" in "New Project"

clip_image003[4]

 

Step 4: Left click on "Name: (editable text)" in "New Project"

clip_image004[4]

 

Step 5: Keyboard input in "New Project" [Backspace Home ... Alt-Shift-Tab]

clip_image005[4]

 

Step 6: User left click on "OK (push button)" in "New Project"

clip_image006

 
 

Step 7: User left click on "IService1.cs (outline item)" in "EmployeeWcfService - Microsoft Visual Studio (Administrator)"

clip_image007

 
 

Step 8: User keyboard input in "EmployeeWcfService - Microsoft Visual Studio (Administrator)" [F2]

clip_image008

 
 

Step 9: User keyboard input in "EmployeeWcfService - Microsoft Visual Studio (Administrator)"

clip_image009

 
 

Step 10: User left click on "Yes (push button)" in "Microsoft Visual Studio"

clip_image010

 
 

Step 11: User left click on "Service1.svc (outline item)" in "EmployeeWcfService - Microsoft Visual Studio (Administrator)"

clip_image011

 
 

Step 12: User keyboard input in "EmployeeWcfService - Microsoft Visual Studio (Administrator)" [F2]

clip_image012

 
 

Step 13: User keyboard input in "EmployeeWcfService - Microsoft Visual Studio (Administrator)"

clip_image013

 
 

Step 14: User left click on "Character 26 (editable text)" in "EmployeeWcfService - Microsoft Visual Studio (Administrator)"

clip_image014

 
 

Step 15: User keyboard input in "EmployeeWcfService - Microsoft Visual Studio (Administrator)"

clip_image015

 
 

Step 16: User left double click on "Web.config (outline item)" in "EmployeeWcfService - Microsoft Visual Studio (Administrator)"

clip_image016

 
 

Step 17: User keyboard input in "EmployeeWcfService - Microsoft Visual Studio (Administrator)" [... Ctrl-F]

clip_image017

 
 

Step 18: User keyboard input in "Find and Replace" [... Enter Esc]

clip_image018

 
 

Step 19: User keyboard input in "EmployeeWcfService - Microsoft Visual Studio (Administrator)"

clip_image019

 
 

Step 20: User keyboard input in "Microsoft Visual Studio" [Enter]

clip_image020

 
 

Step 21: User keyboard input in "EmployeeWcfService - Microsoft Visual Studio (Administrator)"

clip_image021

 
 

Step 22: User keyboard input in "EmployeeWcfService - Microsoft Visual Studio (Administrator)"

clip_image022

 

Add Database Connection

Step 1: User left click on "Server Explorer (menu item)" in "&View"

clip_image001[6]

 
 

Step 2: User left click on "Connect to Database... (push button)" in "EmployeeWcfService - Microsoft Visual Studio (Administrator)"

clip_image002[6]

 
 

Step 3: User keyboard input in "Add Connection" [...]

clip_image003[6]

 
 

Step 4: User left click on "Use SQL Server Authentication (radio button)" in "Add Connection"

clip_image004[6]

 
 

Step 5: User left click on "User name: (editable text)" in "Add Connection"

clip_image005[6]

 
 

Step 6: User keyboard input in "Add Connection" [... Tab]

clip_image006[4]

 
 

Step 7: User keyboard input in "Add Connection" [...]

clip_image007[4]

 
 

Step 8: User left click on "Save my password (check box)" in "Add Connection"

clip_image008[4]

 
 

Step 9: User left click on "Open (push button)" in "Add Connection"

clip_image009[4]

 
 

Step 10: User left click on "EmployeeDB (list item)"

clip_image010[4]

 
 

Step 11: User left click on "Test Connection (push button)" in "Add Connection"

clip_image011[4]

 
 

Step 12: User left click on "OK (push button)" in "Microsoft Visual Studio"

clip_image012[4]

 
 

Step 13: User left click on "OK (push button)" in "Add Connection"

clip_image013[4]

 
 

Create The Employee Dataset

Step 1: User right click on "EmployeeWcfService (outline item)" in "EmployeeWcfService - Microsoft Visual Studio (Administrator)"

clip_image001[8]

 
 

Step 2: User left click on "New Item... (menu item)" in "A&dd"

clip_image002[8]

 
 

Step 3: User left click on "Data (outline item)" in "Add New Item - EmployeeWcfService"

clip_image003[8]

 
 

Step 4: User left click on "DataSet (list item)" in "Add New Item - EmployeeWcfService"

clip_image004[8]

 
 

Step 5: User left click on "Name: (editable text)" in "Add New Item - EmployeeWcfService"

clip_image005[8]

 
 

Step 6: User keyboard input in "Add New Item - EmployeeWcfService" [Backspace Home ...]

clip_image006[6]

 
 

Step 7: User left click on "Add (push button)" in "Add New Item - EmployeeWcfService"

clip_image007[6]

 
 

Step 8: User left click on "sp2010.EmployeeDB.dbo (outline item)" in "EmployeeWcfService - Microsoft Visual Studio (Administrator)"

clip_image008[6]

 
 

Step 9: User left click on "Tables (outline item)" in "EmployeeWcfService - Microsoft Visual Studio (Administrator)"

clip_image009[6]

 
 

Step 10: User mouse drag start on "Employees (outline item)" in "EmployeeWcfService - Microsoft Visual Studio (Administrator)"

clip_image010[6]

 
 

Step 11: User mouse drag end in "EmployeeWcfService - Microsoft Visual Studio (Administrator)"

clip_image011[6]

 
 

Step 12: User left click on "Row 0 (row)" in "EmployeeWcfService - Microsoft Visual Studio (Administrator)"

clip_image012[6]

 
 

Step 13: User keyboard input in "EmployeeWcfService - Microsoft Visual Studio (Administrator)" [... Alt-Shift-Ctrl Alt-Shift-Tab]

clip_image013[6]

 

Step 14: User left click on "SELECT ID, Salary, FullName, Degree, MainDepartment, SubDepartment FROM dbo.Employees (tool tip)"

clip_image014[4]

 
 

Step 15: User left click on "CommandText (row)" in "EmployeeWcfService - Microsoft Visual Studio (Administrator)"

clip_image015[4]

 
 

Step 16: User left click on "row 0, column 7 (cell)" in "Query Builder"

clip_image016[4]

 
 

Step 17: User keyboard input in "Query Builder" [... Enter]

clip_image017[4]

 
 

Step 18: User left click on "Execute Query (push button)" in "Query Builder"

clip_image018[4]

 
 

Step 19: User keyboard input in "Query Parameters" [... Enter]

clip_image019[4]

 
 

Step 20: User left click on "OK (push button)" in "Query Builder"

clip_image020[4]

 
 

Step 21: User left click on "Yes (push button)" in "Microsoft Visual Studio"

clip_image021[4]

 
 

Step 22: User left click in "EmployeeWcfService - Microsoft Visual Studio (Administrator)"

clip_image022[4]

 
 

Step 21: User left click on "Yes (push button)" in "Microsoft Visual Studio"

clip_image023

 

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)"

clip_image001[10]

 
 

Step 2: User left click on "Publish... (menu item)" in "Project"

clip_image002[10]

 
 

Step 3: User left click on "Publish (push button)" in "Publish Web"

clip_image003[10]

 
 

Step 4: User keyboard input in "EmployeeWcfService - Microsoft Visual Studio (Administrator)" [... Alt-Shift-Tab]

clip_image004[10]

 

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