+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Adding fields to user profiles types in code

Adding fields to user profiles types in code

6 posts, 1 answered
  1. Clinton
    Clinton avatar
    14 posts
    Registered:
    27 Feb 2014
    27 Feb 2014
    Link to this post

    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

  2. Ivan D. Dimitrov
    Ivan D. Dimitrov avatar
    310 posts
    Registered:
    26 Mar 2015
    03 Mar 2014
    Link to this post
    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
  3. Clinton
    Clinton avatar
    14 posts
    Registered:
    27 Feb 2014
    03 Mar 2014 in reply to Ivan D. Dimitrov
    Link to this post

    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

  4. Ivan D. Dimitrov
    Ivan D. Dimitrov avatar
    310 posts
    Registered:
    26 Mar 2015
    06 Mar 2014
    Link to this post
    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
  5. Clinton
    Clinton avatar
    14 posts
    Registered:
    27 Feb 2014
    10 Mar 2014 in reply to Ivan D. Dimitrov
    Link to this post

    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

    Answered
  6. Xian
    Xian avatar
    18 posts
    Registered:
    03 Oct 2014
    15 Jan 2015 in reply to Clinton
    Link to this post

    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"));

6 posts, 1 answered