D.J
D.J

Reputation: 2534

Call Profile Provider By Name in Profile config

I have a legacy system (sitecore 6.1) which is already have one profile provider in plave as default profile for admin section.

Now, i need to impelement another customised SQL profile provider (in a different table) for normal user.

But my question is How dose system know which profile provider to use in code?

Is there any thing I can do similar as :

System.Web.Security.Membership.Providers[providerString];

So that I can call customised profile provider in my code accordingly.

Or what would be the best practice in this case.

I've wasted like 1 hour try to go through sitecore docs, but not much available there.

Upvotes: 0

Views: 293

Answers (1)

divamatrix
divamatrix

Reputation: 1126

Here's some code that I recently did to set up some custom profile stuff for a client using the Email Campaign Manager. Granted this code uses some classes specific to ECM, it creates a new user, initializes a profile class and then assigns that profile to the new user. Then it sets some custom properties for the user that was just created. It shows you how to call the profile based on the user as well as assigning a profile to use for that user. This might help or maybe help someone else.

   public static void Process(List<Subscriber> userItems, Item targetAudienceDefinitionItem)
    {
        foreach (Subscriber user in userItems)
        {
            // you can also just pass it the id of the target audience as a string
            Sitecore.Modules.EmailCampaign.TargetAudienceBase target = Sitecore.Modules.EmailCampaign.TargetAudience.FromItem(targetAudienceDefinitionItem);

            string campaignname = target.ManagerRoot.Settings.CommonDomain;
            string realUsername = campaignname + "\\" + user.UserName;

            using (new SecurityDisabler())
            {
                User newUser;
                if (!Sitecore.Security.Accounts.User.Exists(realUsername))
                {
                    // create a new user and assign it to the email domain specified in the manager root item
                    newUser = Sitecore.Security.Accounts.User.Create(campaignname + "\\" + user.UserName, System.Web.Security.Membership.GeneratePassword(8,1));
                }
                else
                    // get back the existing user
                    newUser = User.FromName(realUsername, false);

                // get back the current user profile  
                UserProfile subscriber = newUser.Profile;

                // reset the profile to be the profile specified in the manager root
                subscriber.ProfileItemId = target.ManagerRoot.Settings.SubscriberProfile;
                subscriber.Save();

                // built in properties are set like this
                subscriber.Email = user.Email;

                // set custom property value 
                subscriber["Address"] = user.Address;


                // or long method 
                subscriber.SetCustomProperty("Address", user.Address);

                subscriber.Save();
                // now subscribe the user to the target audience subscriber list
                target.Subscribe(Contact.FromName(newUser.Name));

            }
        }
    }

Upvotes: 0

Related Questions