Contents

Outlook Contacts Sync SQL Server – Iteration 3 User Stories

  1. As a user, I want my contacts synchronized with a SQL Server or SQL Azure contact table.
  2. As an Outlook user, I want to see the statistics of an Outlook / contacts table synchronization.

How to install OutlookSyncSQL Iteration 3

  • Install SqlLocaLDB.MSI 64 bit or SqlLocaLDB.MSI 32 bit.
    • For SqlLocaLDB.MSI 64 bit, download ENU\x64\SqlLocalDB.MSI on the Microsoft® SQL Server® 2012 Express page.
    • For SqlLocaLDB.MSI 32 bit, download ENU\x86\SqlLocaLDB.MSI on the Microsoft® SQL Server® 2012 Express page.
  • To connect to LocalDB, install .NET Framework 4 Update 4.0.2.
  • Install AdventureWorksLT2012_SyncSample database

How to install AdventureWorksLT2012_SyncSample database

The AdventureWorksLT2012_SyncSample database can be installed using the CREATE DATABASE statement or by attaching the database.

To install AdventureWorksLT2012_SyncSample database

  1. Download OutlookSyncSQL_Iteration_3.zip.
  2. From the License Agreement, click I Agree.
  3. From File Download, click Save and browse to a location on your desktop.
  4. Unzip OutlookSyncSQL.zip and copy \Database\AdventureWorksLT2012_SyncSample_Data.mdf to a folder.
  5. From SQL Server Management Studio, execute the following T-SQL code:

CREATE DATABASE AdventureWorksLT2012_SyncSample ON (FILENAME = '{drive}:\{file path}\AdventureWorksLT2012_SyncSample_Data.mdf’) FOR ATTACH_REBUILD_LOG;

As an alternative to step 4, you can attach the database using the SQL Server Management Studio user interface. For more detailed information, see Attach a Database (SQL Server Management Studio) .

Note: You must remove the log file from the list of files to attach. This will cause the operation to rebuild the log.

How to install AdventureWorksLT2012_localdb_SyncSample database on SQL Express LocalDB

The AdventureWorksLT2012_localdb_SyncSample database can be installed using the CREATE DATABASE statement or by attaching the database.

To install AdventureWorksLT2012_localdb_SyncSample database

1. Download OutlookSyncSQL_Iteration_3.zip.

2. From the License Agreement, click I Agree.

3. From File Download, click Save and browse to a location on your desktop.

4. Unzip OutlookSyncSQL.zip and copy \Database\AdventureWorksLT2012_localdb_SyncSample.mdf to a folder.

5. From SQL Server Management Studio, connect to (localdb)\v11.0 as the Server name, and execute the following T-SQL code:

CREATE DATABASE AdventureWorksLT2012_localdb_SyncSample ON (FILENAME = '{drive}:\{file path}\ AdventureWorksLT2012_localdb_SyncSample') FOR ATTACH_REBUILD_LOG;

As an alternative to step 4, you can attach the database using the SQL Server Management Studio user interface. For more detailed information, see Attach a Database (SQL Server Management Studio).

How to configure the sample SQL Server Synchronization

To configure the sample SQL Server Synchronization, run the SyncFrameworkUnitTest.ProvisionServerScope() test method then the SyncFrameworkUnitTest.ProvisionClientScope()test method.

How to run Iteration 3

To run iteration 3, open OutlookSyncSQL.sln in Visual Studio 2010 and run the sample. Once the solution opens Outlook, you will see a *SQL Server Samples* ribbon tab when you navigate to your Outlook Contacts folder. Click the Sync Task Pane ribbon button to toggle the Outlook Sync Sample task pane.

Note The sample source project will install the OutlookSyncSQLAddin Outlook 2010 add-in.

SyncTaskPane

Synchronization Unit Tests

The following unit tests are defined within the SyncFrameworkUnitTest class.

ProvisionServerScope()

The ProvisionServerScope() test method defines a sync scope and provisions the AdventureWorksLT2012_SyncSample sample database on a SQL Server instance with scope related artifacts. A sync scope, you define what you want to synchronize. A sync scope is a set of tables that must be synchronized as a single unit. OutlookSyncSQL Iteration 3 defines a ProductsScope containing [SalesLT].[Customer], [SalesLT].[CustomerAddress], [SalesLT].[Address].

For more information, see Walkthrough: Defining Scope and Provisioning a Server.

    [TestMethod]
    public void ProvisionServerScope()
    {
      using (SqlSyncProvider provider = new SqlSyncProvider())
      {
        provider.ScopeName = scopeName;

        //Create a new scope description and add the appropriate tables to this scope
        DbSyncScopeDescription scopeDescription = new DbSyncScopeDescription(this.scopeName);

        using (provider.Connection = new SqlConnection(@Properties.Settings.Default.ServerConnectionString))
        {
          // Provision the scope defined above
          SqlSyncScopeProvisioning sqlSyncScope = new SqlSyncScopeProvisioning((System.Data.SqlClient.SqlConnection)provider.Connection);

          // Does scope exist on the server and if not provision the scope
          if (!sqlSyncScope.ScopeExists(this.scopeName))
          {
            // Add the approrpiate tables to this scope
            scopeDescription.Tables.Add(SqlSyncDescriptionBuilder.GetDescriptionForTable("[SalesLT].[Customer]", (System.Data.SqlClient.SqlConnection)provider.Connection));
            scopeDescription.Tables.Add(SqlSyncDescriptionBuilder.GetDescriptionForTable("[SalesLT].[CustomerAddress]", (System.Data.SqlClient.SqlConnection)provider.Connection));
            scopeDescription.Tables.Add(SqlSyncDescriptionBuilder.GetDescriptionForTable("[SalesLT].[Address]", (System.Data.SqlClient.SqlConnection)provider.Connection));

            // Note: Call this after the tables have been added to the scope
            sqlSyncScope.PopulateFromScopeDescription(scopeDescription);

            // Indicate that the base table already exists and does not need to be created
            sqlSyncScope.SetCreateTableDefault(DbSyncCreationOption.Skip);

            // Provision the server sync scope
            sqlSyncScope.Apply();
          }

          Assert.IsTrue(sqlSyncScope.ScopeExists(scopeName));
        }
      }
    }

ProvisionClientScope()

The ProvisionClientScope() test method provisions AdventureWorksLT2012_localdb_SyncSample with ProductsScope artifacts from the SQL Server instance. Note that you connect to a LocalDB database using (localdb)\v11.0 as the Server Name.

For more information, see Walkthrough: Provisioning a SQL Compact Client. The OutlookSyncSQL sample was modified from this walkthrough to use SQL Express LocalDB.

    [TestMethod]
    public void ProvisionClientScope()
    {
      // Create a connection to the LocalDB database     
      using (SqlConnection clientConnection = new SqlConnection(@Properties.Settings.Default.LocalDBConnectionString))
      {
        // Create a connection to the Server database
        using (SqlConnection serverConnection = new SqlConnection(@Properties.Settings.Default.ServerConnectionString))
        {
          // Get the description of ProductsScope from the SyncDB server database
          DbSyncScopeDescription scopeDesc = SqlSyncDescriptionBuilder.GetDescriptionForScope(scopeName, serverConnection);

          // Create LocalDB provisioning object based on the ProductsScope
          SqlSyncScopeProvisioning clientProvision = new SqlSyncScopeProvisioning(clientConnection, scopeDesc);

          if (!clientProvision.ScopeExists(scopeName))
          {
            clientProvision.Apply();
          }
        }
      }
    }

Synchronize()

The Synchronize() method creates a sync orchestrator that synchronizes LocalDB AdventureWorksLT2012 and SQL Server AdventureWorksLT2012. The OutlookSyncSQL sample manually kicks off the synchronization process.

For more information, see Walkthrough: Executing Synchronization.

    [TestMethod]
    public void Synchronize()
    {
      SyncOperationStatistics syncStats = null;

      // Create a connection to the LocalDB database     
      using (SqlConnection clientConnection = new SqlConnection(@Properties.Settings.Default.LocalDBConnectionString))
      {
        // Create a connection to the Server database
        using (SqlConnection serverConnection = new SqlConnection(@Properties.Settings.Default.ServerConnectionString))
        {
          // Create the sync orhcestrator
          SyncOrchestrator syncOrchestrator = new SyncOrchestrator();

          // Set local provider of orchestrator to a LocalDB (SQL) provider associated with the 
          syncOrchestrator.LocalProvider = new SqlSyncProvider(scopeName, clientConnection);

          // Set the remote provider of orchestrator to a server sync provider associated with
          syncOrchestrator.RemoteProvider = new SqlSyncProvider(scopeName, serverConnection);

          // Set the direction of sync session to Upload and Download
          syncOrchestrator.Direction = SyncDirectionOrder.UploadAndDownload;

          // execute the synchronization process
          syncStats = syncOrchestrator.Synchronize();

          Assert.IsTrue(syncStats.SyncEndTime > syncStats.SyncStartTime);
        }
      }     
    }

Last edited Jul 19, 2012 at 7:00 AM by DerrickVMSFT, version 19

Comments

DerrickVMSFT Jul 11, 2012 at 4:18 AM 
The previous iteration has been uploaded.

DerrickVMSFT Jul 11, 2012 at 1:48 AM 
I will upload the previous iteration tomorrow. Thank you for your feedback.

Steelynx Jul 6, 2012 at 3:02 PM 
Where can I find the source code from previous iterations? I would really like to work through this sample step by step and it would be very useful to see the code as it was when the documents where written.