Creating Custom Profile Properties through Code (C#)

I recently needed to add several custom profile properties through a web part for tracking a users’ security preferences for a particular web application.

The following is the method used to create four new properties in the SSP:

private void VerifyCreateBaseProfileProperties()
        {
            SPSite site = SPContext.GetContext(HttpContext.Current).Site;
            Guid siteId = site.ID;
            HttpContext savedContext = HttpContext.Current;

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite elevatedSite = new SPSite(siteId))
                {
                    try
                    {
                        HttpContext.Current = null;

                        elevatedSite.AllowUnsafeUpdates = true;
                        elevatedSite.RootWeb.AllowUnsafeUpdates = true;

                        ServerContext serverContext = ServerContext.GetContext(elevatedSite);

                        UserProfileManager profileManager = new UserProfileManager(serverContext);
                        UserProfileConfigManager profileConfigManager = new UserProfileConfigManager(serverContext);
                        PropertyCollection propertyCollection = profileConfigManager.GetProperties();

                        bool question1Exists = false;
                        bool question2Exists = false;
                        bool question3Exists = false;
                        bool termsOfUseExists = false;

                        foreach (Property property in profileManager.Properties)
                        {
                            switch (property.Name)
                            {
                                case "SecurityQuestion1":
                                    question1Exists = true;
                                    break;
                                case "SecurityQuestion2":
                                    question2Exists = true;
                                    break;
                                case "SecurityQuestion3":
                                    question3Exists = true;
                                    break;
                                case "TermsOfUseAccepted":
                                    termsOfUseExists = true;
                                    break;
                            }
                        }

                        if (!question1Exists)
                        {
                            Property question1 = propertyCollection.Create(false);
                            question1.Name = "SecurityQuestion1";
                            question1.DisplayName = "Security Question 1";
                            question1.Type = "string";
                            question1.PrivacyPolicy = PrivacyPolicy.OptIn;
                            question1.DefaultPrivacy = Privacy.Private;
                            question1.Description = "Security Question 1";
                            question1.IsSearchable = false;
                            question1.IsVisibleOnEditor = false;
                            question1.IsAlias = false;
                            question1.Length = 100;
                            question1.IsUserEditable = true;
                            propertyCollection.Add(question1);
                        }
                        if (!question2Exists)
                        {
                            Property question2 = propertyCollection.Create(false);
                            question2.Name = "SecurityQuestion2";
                            question2.DisplayName = "Security Question 2";
                            question2.Type = "string";
                            question2.PrivacyPolicy = PrivacyPolicy.OptIn;
                            question2.DefaultPrivacy = Privacy.Private;
                            question2.Description = "Security Question 2";
                            question2.IsSearchable = false;
                            question2.IsVisibleOnEditor = false;
                            question2.IsAlias = false;
                            question2.Length = 100;
                            question2.IsUserEditable = true;
                            propertyCollection.Add(question2);
                        }
                        if (!question3Exists)
                        {
                            Property question3 = propertyCollection.Create(false);
                            question3.Name = "SecurityQuestion3";
                            question3.DisplayName = "Security Question 3";
                            question3.Type = "string";
                            question3.PrivacyPolicy = PrivacyPolicy.OptIn;
                            question3.DefaultPrivacy = Privacy.Private;
                            question3.Description = "Security Question 3";
                            question3.IsSearchable = false;
                            question3.IsVisibleOnEditor = false;
                            question3.IsAlias = false;
                            question3.Length = 100;
                            question3.IsUserEditable = true;
                            propertyCollection.Add(question3);
                        }
                        if (!termsOfUseExists)
                        {
                            Property termsofUsequestion = propertyCollection.Create(false);
                            termsofUsequestion.Name = "TermsOfUseAccepted";
                            termsofUsequestion.DisplayName = "Terms Of Use Accepted";
                            termsofUsequestion.Type = "string";
                            termsofUsequestion.PrivacyPolicy = PrivacyPolicy.OptIn;
                            termsofUsequestion.DefaultPrivacy = Privacy.Private;
                            termsofUsequestion.Description = "Terms Of Use Accepted";
                            termsofUsequestion.IsSearchable = false;
                            termsofUsequestion.IsVisibleOnEditor = false;
                            termsofUsequestion.IsAlias = false;
                            termsofUsequestion.Length = 10;
                            termsofUsequestion.IsUserEditable = true;
                            propertyCollection.Add(termsofUsequestion);
                        }
                    }
                    catch (Exception exc)
                    {
                    }
                    finally
                    {
                        elevatedSite.Dispose();
                    }
                }
            });

            site.Dispose();
            HttpContext.Current = savedContext;
        }

Note that the current context (HttpContext.Current) needs to be saved off, set to null, then reinstated upon the completion of the method.

Bookmark and Share
Leave a comment

4 Comments.

  1. Really nice article about user propfile properties.

    Is there any way to create propert of custom data type? Insted of using predifined datatypes like string, html etc can we create custom data type and use it as a data type for user profile property?

    Regards,
    Rahul

  2. Thanks for this; I was off site and didn’t have access to my code and this was a quick fix.

  3. Great article! Thanks for this article, it took me a while to find it!

    One thing, the using statement ensures that dispose is called even if an exception occurs.

  4. Thanks for this, was very helpful. Although im not able to find a way to configure the property import mapping. Any help with that?
    Thanks!

Leave a Reply


[ Ctrl + Enter ]

Performance Optimization WordPress Plugins by W3 EDGE