Three weeks ago I was standing against task “How to delete synchronization source for ActiveSync in Windows Mobile device”. By synchronization source is meant Microsoft Exchange or Windows PC. I had no clue where to start. Before I started digging with API, I’ve tested configuration XML.

Just query the Sync configuration manager.

<wap-provisioningdoc>
    <characteristic-query type="Sync" />
</wap-provisioningdoc>

I’ve received quite long result with interesting information where I found something like this

<characteristic type="Sources">
    <characteristic type="{3CBBAEC4-C001-4355-C0AD-4426439580F9}">
        <parm name="Name" value="Microsoft Exchange"/>
        <parm name="Server" value="emea.mail.microsoft.com"/>
        <parm name="StoreType" value="3"/>
        <characteristic type="Engines">
            <characteristic type="{22C7DA12-F3FD-4875-8344-7786454F6534}">
                <characteristic type="CarrierConnectorList"/>
                .
                .
                .
                .
                .

BINGO!!! It’s easy. If you want to query your synchronization source, do it by this easy XML.

<wap-provisioningdoc>
    <characteristic type="Sync">
        <characteristic-query type="Sources" />
    </characteristic>
</wap-provisioningdoc>

Every synchronization source is given the GUID as the identifier. So if you want to delete this source, the XML is obvious.

<wap-provisioningdoc>
    <characteristic type="Sync">
        <characteristic type="Sources">
            <nocharacteristic type="{YOUR-SOURCE-GUID-HERE-1234567890}" />
        </characteristic>
    </characteristic>
</wap-provisioningdoc>

List sources and delete them in C#

When I realize all of this, it was quite easy to wrap it all to the C# class for Compact Framework 2.0. Complete listing follows.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.WindowsMobile.Configuration;
using System.Xml;
using System.Xml.XPath;

namespace OfficeClean
{
    public class SyncSource
    {
        /// <summary>
        /// Name of the Source
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// GUID of the source
        /// </summary>

        public string GUID { get; set; }

        /// <summary>
        /// Delete the selection source
        /// </summary>
        public void Delete()
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(string.Format(@"<wap-provisioningdoc>
                                        <characteristic type=""Sync"">
                                            <characteristic type=""Sources"">

                                                <nocharacteristic type=""{0}"" />
                                            </characteristic>
                                        </characteristic>
                                    </wap-provisioningdoc>", this.GUID));

            ConfigurationManager.ProcessConfiguration(doc, false);         
        }

        /// <summary>
        /// Returns list of all Synchronization Sources
        /// </summary>

        /// <returns>List of sources</returns>
        public static List<SyncSource> GetSources()
        {
            StringBuilder apps = new StringBuilder();

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(@"<wap-provisioningdoc>
                            <characteristic type=""Sync"">
                                <characteristic-query type=""Sources"" />

                            </characteristic>
                        </wap-provisioningdoc>");

            XmlDocument ret = ConfigurationManager.ProcessConfiguration(doc, false);            
            XmlNode sourceNode = ret.SelectSingleNode("//characteristic/characteristic[@type='Sources']");

            List<SyncSource> retList = new List<SyncSource>();            
            foreach (XmlNode node in sourceNode.ChildNodes)
            {
                SyncSource item = new SyncSource();
                item.GUID = node.Attributes["type"].Value;
                XmlNode bnode = node.SelectSingleNode("parm[@name='Name']");
                item.Name = bnode.Attributes["value"].Value;

                retList.Add(item);
            }

            return retList;
        }

        public override string ToString()
        {
            return this.Name;
        }
    }
}

And how to use it?

foreach (SyncSource item in SyncSource.GetSources())
{
    Debug.WriteLine(item.Name);    
    Debug.WriteLine(item.GUID);
    //item.Delete();
}