This command replaces the gl-setmysitesnamingformat command as it includes the same functionality along with additional capabilities.

I had originally created the gl-setmysitesnamingformat command to address a need to set the naming format of personal sites and I originally didn’t think I’d have to worry about the other settings on the My Sites Settings page but turns out that I did need to set one other field. So rather than create another command for just one field I decided to create a command that would allow me to set any value on that particular page.

I was originally thinking I would exclude the naming format as I already had a command for that but then decided that I’d just include it so that this command became a one-stop-shop and the other could be deprecated (I’ll leave it in but there’s not much need for it now that this command exists).

Like the original command I had to use reflection to set the internal properties (I just don’t understand why Microsoft chose not to make the UserProfileApplication object public). Beyond using the internal UserProfileApplication class the command also uses an internal method called CommitPersonalSiteSettings which is part of the UserProfileManager class (I had to use reflection to call this method but at least I’m not accessing the database directly).

I’m hoping that Microsoft will either make these methods and objects public or will create the ability to set these values via the existing properties. The core code is shown below (if using reflection scares you I’d suggest clicking away):

  1public class MySiteSettings : SPOperation
  2{
  3    /// <summary>
  4    /// Initializes a new instance of the <see cref="MySiteSettings"/> class.
  5    /// </summary>
  6    public MySiteSettings()
  7    {
  8        SPParamCollection parameters = new SPParamCollection();
  9        parameters.Add(new SPParam("sspname", "ssp", true, null, new SPNonEmptyValidator(), "Please specify the SSP name."));
 10        parameters.Add(new SPParam("nameformat", "f", false, null, new SPRegexValidator("(?i:^Username_CollisionError$|^Username_CollisionDomain$|^Domain_Username$)")));
 11        parameters.Add(new SPParam("location", "l", false, null, new SPNonEmptyValidator(), "Please specify the location at which to create personal sites."));
 12        parameters.Add(new SPParam("chooselanguage", "cl", false, null, new SPEnableDisableValidator(), "Please choose whether users can choose the language of their personal site."));
 13        parameters.Add(new SPParam("readers", "r", false, null, new SPNonEmptyValidator(), "Please enter the comma separated list of site group readers."));
 14        parameters.Add(new SPParam("searchcenter", "search", false, null, new SPNonEmptyValidator(), "Please enter the preferred search center URL."));
 15        parameters.Add(new SPParam("remotemysites", "remote", false, null, new SPEnableDisableValidator(), "Please specify whether you wish to enable or disable my sites to support global deployments."));
 16        parameters.Add(new SPParam("provider", "p", false, null, new SPUrlValidator(), "Please specify the personal site provider."));
 17        
 18        StringBuilder sb = new StringBuilder();
 19        sb.Append("\r\n\r\nSets the my site settings for the My Sites web application.\r\n\r\nParameters:");
 20        sb.Append("\r\n\t-sspname <SSP name>");
 21        sb.Append("\r\n\t[-nameformat <Username_CollisionError | Username_CollisionDomain | Domain_Username>]");
 22        sb.Append("\r\n\t[-location <personal site location>]");
 23        sb.Append("\r\n\t[-chooselanguage <enable | disable>]");
 24        sb.Append("\r\n\t[-readers <comma separated list of site group readers>]");
 25        sb.Append("\r\n\t[-searchcenter <preferred search center URL>]");
 26        sb.Append("\r\n\t[-remotemysites <enable | disable>]");
 27        sb.Append("\r\n\t[-provider <personal site provider URL (ex: \"http://mysites\")>]");
 28        
 29        Init(parameters, sb.ToString());
 30    }
 31
 32    #region ISPStsadmCommand Members
 33
 34    /// <summary>
 35    /// Gets the help message.
 36    /// </summary>
 37    /// <param name="command">The command.</param>
 38    /// <returns></returns>
 39    public override string GetHelpMessage(string command)
 40    {
 41        return HelpMessage;
 42    }
 43
 44    /// <summary>
 45    /// Runs the specified command.
 46    /// </summary>
 47    /// <param name="command">The command.</param>
 48    /// <param name="keyValues">The key values.</param>
 49    /// <param name="output">The output.</param>
 50    /// <returns></returns>
 51    public override int Run(string command, StringDictionary keyValues, out string output)
 52    {
 53        output = string.Empty;
 54
 55        InitParameters(keyValues);
 56
 57        string sspname = Params["sspname"].Value;
 58
 59        ServerContext current = ServerContext.GetContext(sspname);
 60        UserProfileManager upm = new UserProfileManager(current);
 61        
 62        SiteNameFormat nameFormat = upm.PersonalSiteFormat;
 63        if (Params["nameformat"].UserTypedIn)
 64            nameFormat = (SiteNameFormat)Enum.Parse(typeof(SiteNameFormat), Params["nameformat"].Value, true);
 65
 66        string location = upm.PersonalSiteInclusion;
 67        if (Params["location"].UserTypedIn)
 68            location = Params["location"].Value;
 69
 70        bool chooseLanguage = upm.IsPersonalSiteMultipleLanguage;
 71        if (Params["chooselanguage"].UserTypedIn)
 72            chooseLanguage = (Params["chooselanguage"].Value == "enable" ? true : false);
 73
 74        string readers = upm.PersonalSiteReaders;
 75        if (Params["readers"].UserTypedIn)
 76            readers = Params["readers"].Value;
 77
 78        CommitPersonalSiteSettings(upm, chooseLanguage, location, nameFormat, readers);
 79
 80        bool modified = false;
 81
 82        // UserProfileApplication userProfileApplication = current.UserProfileApplication;
 83        System.Reflection.PropertyInfo userProfileApplicationProp = current.GetType().GetProperty("UserProfileApplication",
 84                                                    BindingFlags.NonPublic |
 85                                                    BindingFlags.Instance |
 86                                                    BindingFlags.InvokeMethod |
 87                                                    BindingFlags.GetProperty);
 88        object userProfileApplication = userProfileApplicationProp.GetValue(current, null);
 89        
 90        // Set the search center url
 91        if (Params["searchcenter"].UserTypedIn)
 92        {
 93            // userProfileApplication.SearchCenterUrl = Params["searchcenter"].Value;
 94            System.Reflection.PropertyInfo searchCenterProp = userProfileApplication.GetType().GetProperty("SearchCenterUrl",
 95                                                                                        BindingFlags.NonPublic |
 96                                                                                        BindingFlags.Public |
 97                                                                                        BindingFlags.Instance |
 98                                                                                        BindingFlags.SetProperty |
 99                                                                                        BindingFlags.FlattenHierarchy);
100            searchCenterProp.SetValue(userProfileApplication, Params["searchcenter"].Value, null);
101            modified = true;
102        }
103        if (Params["remotemysites"].UserTypedIn)
104        {
105            //userProfileApplication.EnablePersonalFeaturesForMultipleDeployments = Params["remotemysites"].Value;
106            System.Reflection.PropertyInfo remoteProp = userProfileApplication.GetType().GetProperty("EnablePersonalFeaturesForMultipleDeployments",
107                                                                                        BindingFlags.NonPublic |
108                                                                                        BindingFlags.Public |
109                                                                                        BindingFlags.Instance |
110                                                                                        BindingFlags.SetProperty |
111                                                                                        BindingFlags.FlattenHierarchy);
112            bool remoteMySites = (Params["remotemysites"].Value == "enable" ? true : false);
113            remoteProp.SetValue(userProfileApplication, remoteMySites, null);
114            modified = true;
115        }
116
117        if (Params["provider"].UserTypedIn)
118        {
119            Uri uri = new Uri(Params["provider"].Value);
120            //userProfileApplication.MySitePortalUrl = uri.AbsoluteUri;
121            System.Reflection.PropertyInfo searchCenterProp = userProfileApplication.GetType().GetProperty("MySitePortalUrl",
122                                                                                BindingFlags.NonPublic |
123                                                                                BindingFlags.Public |
124                                                                                BindingFlags.Instance |
125                                                                                BindingFlags.SetProperty |
126                                                                                BindingFlags.FlattenHierarchy);
127            searchCenterProp.SetValue(userProfileApplication, uri.AbsoluteUri, null);
128            modified = true;
129        }
130
131        if (modified)
132        {
133            MethodInfo update =
134                userProfileApplication.GetType().GetMethod("Update",
135                                                    BindingFlags.NonPublic |
136                                                    BindingFlags.Public |
137                                                    BindingFlags.Instance |
138                                                    BindingFlags.InvokeMethod |
139                                                    BindingFlags.FlattenHierarchy,
140                                                    null,
141                                                    new Type[] { }, null);
142            // userProfileApplication.Update(true);
143            update.Invoke(userProfileApplication, null);
144
145            Console.WriteLine("The settings updated may require an iisreset before the changes are visible.");
146        }
147
148        return 1;
149    }
150
151
152    #endregion
153
154    /// <summary>
155    /// Commits the personal site settings.
156    /// </summary>
157    /// <param name="upm">The user profile manager.</param>
158    /// <param name="chooseLanguage">if set to <c>true</c> the user may choose a language for their personal site.</param>
159    /// <param name="location">The personal site location.</param>
160    /// <param name="nameFormat">The site name format.</param>
161    /// <param name="readers">The readers site groups (comma separated).</param>
162    internal static void CommitPersonalSiteSettings(UserProfileManager upm, bool chooseLanguage, string location, SiteNameFormat nameFormat, string readers)
163    {
164        MethodInfo commitPersonalSiteSettings =
165            upm.GetType().GetMethod("CommitPersonalSiteSettings",
166                                    BindingFlags.NonPublic | BindingFlags.Public |
167                                    BindingFlags.Instance | BindingFlags.InvokeMethod);
168
169        commitPersonalSiteSettings.Invoke(upm,
170                                            new object[]
171                                                {
172                                                    location, nameFormat, readers, chooseLanguage
173                                                });
174    }
175}