I was recently trying to debug some issues that I was having with the people picker that is shown when creating audiences and I found that I needed a way to manually trigger the distribution list import quickly but I didn’t always want to have to wait for the user profile import to finish. If you run an import using the SSP admin site you might notice that it imports all the user profile information and then it imports the distribution list information but there’s no way (at least that I could find) to do just the distribution list. So I created a new command which I called gl-runprofileimportjob which allows me to run the profile import.

Fortunately it turned out that the code to do this is really simple – you just need an instance of the UserProfileConfigManager and you call either StartImport() or StartDLImport(). To keep the command from exiting before the import is complete I check the IsImportInProgress method and loop until it is set to false:

 1public class RunProfileImportJob : SPOperation
 2{
 3    /// <summary>
 4    /// Initializes a new instance of the <see cref="RunProfileImportJob"/> class.
 5    /// </summary>
 6    public RunProfileImportJob()
 7    {
 8        SPParamCollection parameters = new SPParamCollection();
 9        parameters.Add(new SPParam("sspname", "ssp", false, null, new SPNonEmptyValidator()));
10        parameters.Add(new SPParam("distributionlistonly", "dl"));
11        parameters.Add(new SPParam("incremental", "inc"));
12 
13        StringBuilder sb = new StringBuilder();
14        sb.Append("\r\n\r\nExecutes a Profile Import.\r\n\r\nParameters:");
15        sb.Append("\r\n\t-sspname <SSP Name>");
16        sb.Append("\r\n\t[-distributionlistonly]");
17        sb.Append("\r\n\t[-incremental]");
18 
19        Init(parameters, sb.ToString());
20    }
21 
22    /// <summary>
23    /// Gets the help message.
24    /// </summary>
25    /// <param name="command">The command.</param>
26    /// <returns></returns>
27    public override string GetHelpMessage(string command)
28    {
29        return HelpMessage;
30    }
31 
32    /// <summary>
33    /// Executes the specified command.
34    /// </summary>
35    /// <param name="command">The command.</param>
36    /// <param name="keyValues">The key values.</param>
37    /// <param name="output">The output.</param>
38    /// <returns></returns>
39    public override int Execute(string command, System.Collections.Specialized.StringDictionary keyValues, out string output)
40    {
41        output = string.Empty;
42 
43        string sspName = Params["sspname"].Value;
44        bool dlOnly = Params["distributionlistonly"].UserTypedIn;
45        bool incremental = Params["incremental"].UserTypedIn;
46 
47        UserProfileConfigManager manager = new UserProfileConfigManager(ServerContext.GetContext(sspName));
48        if (!manager.IsImportInProgress())
49        {
50            if (dlOnly)
51                manager.StartDLImport();
52            else
53                manager.StartImport(incremental);
54 
55            Console.Write("Executing import...");
56            while (manager.IsImportInProgress())
57            {
58                Thread.Sleep(500);
59                Console.Write(".");
60            }
61            Console.WriteLine();
62            Console.WriteLine();
63        }
64        else
65        {
66            Console.WriteLine("Import is already running.");
67        }
68 
69        return OUTPUT_SUCCESS;
70    }
71 
72}

The syntax of the command can be seen below:

C:\>stsadm -help gl-runprofileimportjob

stsadm -o gl-runprofileimportjob


Executes a Profile Import.

Parameters:
        -sspname <SSP Name>
        [-distributionlistonly]
        [-incremental]

Here’s an example of how to run the command to import just the distribution lists:

stsadm -o gl-runprofileimportjob -sspname SSP1 -distributionlistonly

Note that the -incremental flag is only relevant when the -distributionlistonly flag is not provided.