Contents
Outlook Contacts Sync SQL Server – Iteration 3 User Stories
- As a user, I want my contacts synchronized with a SQL Server or SQL Azure contact table.
- As an Outlook user, I want to see the statistics of an Outlook / contacts table synchronization.
- 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
- Download
OutlookSyncSQL_Iteration_2.zip .
- From the License Agreement, click I Agree.
- From File Download, click Save and browse to a location on your desktop.
- Unzip OutlookSyncSQL.zip and copy \Database\AdventureWorksLT2012_SyncSample_Data.mdf to a folder.
- From SQL Server Management Studio, execute the following 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 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.
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.

Synchronization Unit Tests
The following unit tests are defined within the SyncFrameworkUnitTest class.
[TestClass]
public class SyncFrameworkUnitTest
{
private string scopeName = "ContactsScope";
/// <summary>
/// Configure server scope
/// </summary>
[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));
}
}
}
/// <summary>
/// Configure client scope
/// </summary>
[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 CE provisioning object based on the ProductsScope
SqlSyncScopeProvisioning clientProvision = new SqlSyncScopeProvisioning(clientConnection, scopeDesc);
if (!clientProvision.ScopeExists(scopeName))
{
clientProvision.Apply();
}
}
}
}
[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);
}
}
}