Adding fields to user profiles types in code

Posted by Community Admin on 04-Aug-2018 16:15

Adding fields to user profiles types in code

All Replies

Posted by Community Admin on 27-Feb-2014 00:00

Hello,

 I am starting to write a configuration program to configure a Sitefinity solution that is about to be deployed.

 I am looking for information on how to add fields to the basic Sitefinity profile type through code. I need to add 6 new fields to the "Telerik.Sitefinity.Security.Model.SitefinityProfile" type only using code. Is there any way to accomplish this? Are there any code examples out there that manage profile type fields?

 Thanks,
Clinton

Posted by Community Admin on 03-Mar-2014 00:00
Hi Clinton,

The way you can create user profiles is described in detail in our documentation. Please refer to the link for further clarification.

Regards,
Ivan D. Dimitrov
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 03-Mar-2014 00:00

Hello Ivan,

Thanks for the link, but there is no mention of adding new fields to existing user profiles. Is there any way to do this?

Thanks,
Clinton

Posted by Community Admin on 06-Mar-2014 00:00
Hello Clinton,

We do not have any API to achieve this. You will need to create your profiles from the UI.

Regards,
Ivan D. Dimitrov
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 10-Mar-2014 00:00

Hello Ivan,

I have figured it out, seems adding fields was only possible using the Fluent API, here is the code in case others want to do something similar:

var transaction = App.Prepare().SetTransactionName("CreateAttribute");
 
transaction.WorkWith().DynamicData().Type(typeof (SitefinityProfile)).Field()
    .CreateNew("test", typeof(string)).Do(field =>
    
        field.MetaAttributes.Add(new MetaFieldAttribute(Guid.NewGuid())
        
            Name = "UserFriendlyDataType",
            Value = "ShortText"
        );
        field.MetaAttributes.Add(new MetaFieldAttribute(Guid.NewGuid())
        
            Name = "IsCommonProperty",
            Value = "true",
        );
        field.DBType = "VARCHAR";
        field.DBLength = "255";
    ).SaveChanges(true);
     
TransactionManager.CommitTransaction("CreateAttribute"); 

Regards,
Clinton

Posted by Community Admin on 15-Jan-2015 00:00

Old thread, but I wanted to add the following to the conversation.

 When doing this, if I did not map the ColumnName property, then when I tried to retrieve the field it through a database error.  My complex name (TestThisObjectId) mapped to Test_this_object_Id in the database and it had trouble resolving.

 

 

field.ColumnName = "MatchToFieldName";

Also, a code example for querying against the user profiles that worked for me.  Trying to query against GetProfiles<>() with a standard LINQ FirstOrDefault/Where caused a database exception, so hopefully this technique helps.

var profileManager = UserProfilesHelper.GetUserProfileManager<SitefinityProfile>();
var profiles = SitefinityQuery.Get<UserProfile>(typeof(SitefinityProfile), profileManager.Provider);
var apps = profiles.Where(p => p.ApplicationName == profileManager.Provider.ApplicationName);
var profile = apps.FirstOrDefault(x => x.GetValue<string>("ObjectNameHere")).Equals("TestValue"));

This thread is closed