using System; using System.Web; using System.Web.Profile; using System.Configuration; using Microsoft.SharePoint; using Microsoft.SharePoint.Administration; using Microsoft.Office.Server; using Microsoft.Office.Server.UserProfiles;
string providerName = "CustomAspNetSqlMembershipProvider"; string userAcctAlgorithm = "{0}:{1}"; string UserName = ""; string UserType = "";
SPFarm farm = SPFarm.Local; foreach (SPService serv in farm.Services) { if (serv is SPWebService) { SPWebService webServ = (SPWebService)serv; SPWebApplication webApp = webServ.WebApplications["SharePoint - 80"]; SPSite site = webApp.Sites["http://mossdemo:80 "];
//(SP) get the context for the site we have instantiated ServerContext serverContext = ServerContext.GetContext(site);
//(SP) create a new instance of the user profile manager class using the context from above UserProfileManager upm = new UserProfileManager(serverContext);
//(SQL) get membership profiles ProfileInfoCollection profiles = ProfileManager.GetAllProfiles(ProfileAuthenticationOption.All); //(SQL) for each membership profile in SQL Server foreach (ProfileInfo info in profiles) { //(SP) build our account name (provider + : + username) //string userAccount = string.Format(userAcctAlgorithm, providerName, info.UserName); string userAccount = info.UserName UserName = info.UserName;
//(SQL) create a profile object for the user profile ProfileBase pb = ProfileBase.Create(info.UserName);
//(SP) check if user account exists and get a reference to it, if not create a new one UserProfile up;
if (upm.UserExists(userAccount)) { up = upm.GetUserProfile(userAccount); } else { up = upm.CreateUserProfile(userAccount); }
//(SQL) loop through the properties in the SQL profiles foreach (SettingsProperty sp in ProfileBase.Properties) { //(SP) check if SQL property is in SP profiles Property tempProp = upm.Properties.GetPropertyByName(sp.Name);
//(SP) if the property is null, it does not exist, so we need to create a new property if (tempProp == null) { //(SP) set property attributes Property prpty = upm.Properties.Create(false); prpty.Name = sp.Name; prpty.Type = sp.PropertyType.Name; prpty.DisplayName = sp.Name; prpty.Length = 100; prpty.PrivacyPolicy = PrivacyPolicy.OptIn; prpty.DefaultPrivacy = Privacy.Public; prpty.Description = sp.Name; prpty.IsUserEditable = true; prpty.ChoiceType = ChoiceTypes.None; prpty.IsMultivalued = false; prpty.UserOverridePrivacy = false; prpty.IsReplicable = true; prpty.IsColleagueEventLog = false; prpty.IsAlias = false; prpty.IsSearchable = true; prpty.IsUpgrade = false; prpty.IsUpgradePrivate = false; prpty.IsVisibleOnEditor = true; prpty.IsVisibleOnViewer = false; prpty.Separator = MultiValueSeparator.Comma; prpty.MaximumShown = 10;
//(SP) add the property to the user profiles store prpty.Commit(); } //(COMBINED) add the value from the SQL profile base to the SP user profile up[sp.Name].Clear(); up[sp.Name].Add(pb.GetPropertyValues(sp.Name)); up.Commit(); } |