Custom Profile Type

Posted by Community Admin on 04-Aug-2018 14:07

Custom Profile Type

All Replies

Posted by Community Admin on 14-Jan-2016 00:00

Hi All,     

How can I programmatically add a new custom Profile Type to Users? 

Thanks!

Posted by Community Admin on 19-Jan-2016 00:00

Hi Nathalia,

Adding a profile type programmatically is possible, but is not a simple operation, due to all the properties needed for the profile type fields. Also, it is not a common operation which is performed often. This is why you need to be careful if exposing it to frontend users, because you may end up with many profile types, and higher possibility of wrong or omitted properties.
 I am sending you one code snippet which creates a new profile type and adds two fields (short text and choice field) to it. 

Guid profileMetaTypeId = Guid.NewGuid();
            var profileTypeDescription = new MetaTypeDescription("//", Guid.NewGuid())
            
                UserFriendlyName = "Test Profile"
            ;
 
            MetaType profileMetatype = new MetaType("//", profileMetaTypeId)
            
                ClassName = "testprofile",
                BaseClassName = "Telerik.Sitefinity.Security.Model.UserProfile",
                Namespace = "Telerik.Sitefinity.Security.Model"
            ;
 
            var profileData = new UserProfileTypeViewModel(profileTypeDescription, profileMetatype);
            Guid profileTypeId = Guid.NewGuid();
            profileData.Id = profileTypeId;
            profileData.MembershipProvidersUsage = MembershipProvidersUsage.AllProviders;
            profileData.ProfileProviderName = "OpenAccessProfileProvider";
            profileData.Title = "TestProfileType";
            profileData.AppliedTo = "All user providers";
 
            UserProfilesHelper.CreateUserProfileType(profileData, string.Empty);
 
            var contentType = TypeResolutionService.ResolveType("Telerik.Sitefinity.Security.Model.testprofile");
            var cfContext = new CustomFieldsContext(contentType);
 
            var fieldsToAdd = new Dictionary<string, WcfField>();
            fieldsToAdd.Add("Street", new WcfField()
            
                Name = "Street",
                ContentType = "Short text",
                Definition = new WcfFieldDefinition()
                
                    FieldType = "Telerik.Sitefinity.Web.UI.Fields.TextField",
                    Title = "Street",
                    FieldName = "Street"
                ,
                DatabaseMapping = new WcfDatabaseMapping()
                
                    ClrType = "System.String",
                    DbLength = "255",
                    DbType = "VARCHAR",
                    Nullable = true,
                    Indexed = false
                ,
                FieldTypeKey = "ShortText",
                IsCustom = true
            );
 
            fieldsToAdd.Add("Area", new WcfField()
            
                Name = "Area",
                ContentType ="Multiple choice",
                Definition = new WcfFieldDefinition()
                
                    Choices = "[\"Text\":\"Area One\",\"Value\":\"Area One\",\"Selected\":false,\"Text\":\"Area Two\",\"Value\":\"Area Two\",\"Selected\":false,\"Text\":\"Area Three\",\"Value\":\"Area Three\",\"Selected\":false]",
                    FieldType = "Telerik.Sitefinity.Web.UI.Fields.ChoiceField",
                    Title = "Area",
                    FieldName = "Area"
                ,
                DatabaseMapping = new WcfDatabaseMapping()
                
                    ClrType = "System.String",
                    DbType = "VARCHAR",
                    Nullable = true,
                    Indexed = false
                ,
                FieldTypeKey = "MultipleChoice",
                IsCustom = true
            );
 
            if (fieldsToAdd.Count > 0)
            
                cfContext.AddOrUpdateCustomFields(fieldsToAdd, contentType.Name);
            
 
            var fieldsToRemove = new List<string>();
            // In case you want to remove a field from existing profile type.
            // fieldsToRemove.Add("Street");
            if (fieldsToRemove.Count > 0)
            
                cfContext.RemoveCustomFields(fieldsToRemove, contentType.Name);
            
            cfContext.SaveChanges();


If you need to add other type of fields, I suggest you to try to do a similar operation through the Sitefinity backend UI and inspect the applyChanges web request which is made when you save the changes of the profile type (see screenshot). There you can see sample values of the fields you want to add.

I hope these samples will guide you to the right direction. Feel free to write us again if any other issue appears.

Regards,
Ivan Eftimov
Telerik
 
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
 

Posted by Community Admin on 01-Jul-2016 00:00

This is a really useful thread, exactly what I needed. Thanks. 

I have another question: how to declare the "fieldsToAdd" in case of classification instead of multiple choice? I would like to add an already existing hierarchical classification ..

Thank you

This thread is closed