How to save additional Profile data through create new blog page?

Jun 26, 2014 at 9:30 AM
Edited Jun 26, 2014 at 9:58 AM
Hi All,

I am trying to get information from our users:

FirstName and LastName during sign up and want to save that info directly into their profile instead of them having to go to their profile and adding it in as another step.

Want one sign-up step that saves their First and Last name into their profile.

Also notice when a user creates a new sub blog it does not create a "profile" for them until they go to My Profile and press save.

How can I programmatically with C# trigger the Save process?

I tried the following:

AuthorProfile.Load(BlogOwner).LastName = "LastNameTest";
AuthorProfile.Load(BlogOwner).Save();

But, since the user did not press save in their profile, there is no "AuthorProfile" to load and I get an error doing this.

When I go to My Profile and don't change anything and press save it says "Updated Information" and then it creates the profile.

Need way to trigger that with C# :) and not JavaScript.


Wanted to load the user profile after they had created their new Sub blog to display as part of their blog.

Like their First and Last Name, and Avatar image.


Currently since when they create a new sub blog no profile is created and get object not set error when trying to pull their data from their profile.


Thanks,

Brian Davis
Coordinator
Jun 26, 2014 at 2:59 PM
I don't have a code to look at right now, but I would imagine you can always check if loaded profile is null and create new profile if it does not exist. So "get" would be a trigger.
Jun 26, 2014 at 10:23 PM
Hi Rtur,

Currently I am checking to see if there is a profile or not to prevent nulls like this:

if (AuthorProfile.GetProfile(UserName) != null)
{
Do some things and this works in preventing nulls
}

But, what do I do when it is null and I need to create their profile?

When using AuthorProfile.GetProfile(UserName) it does not create their profile, but throws a null error.
Coordinator
Jun 27, 2014 at 5:30 PM
In the users repository there is "UpdateUserProfile" method that creates new profile if not exists, you can look at it for example.
Jul 1, 2014 at 7:41 AM
Hi Rtur,

For the last few days I been trying to get it to work but no luck, so need some additional help :)

Here is the code that I have:

protected void CreateUserButton_Click(object sender, EventArgs e)
    {
        //Change BlogName to be UserName
        string blogName = UserName.Text.Trim().ToLower();

        string msg = CreateNewBlog();

        if (string.IsNullOrEmpty(msg))
            {
                this.Response.Redirect(Utils.ApplicationRelativeWebRoot + blogName);
            }
        else
            {
                this.Master.SetStatus("warning", msg);
            }

        CreateNewProfile();

    }



void CreateNewProfile()
    {

        try
        {

            var pf = new AuthorProfile(UserName.Text);


            pf.DisplayName = UserName.Text;
            pf.FirstName = FirstName.Text;
            pf.MiddleName = "";
            pf.LastName = LastName.Text;
            pf.EmailAddress = Email.Text; // user.Profile.EmailAddress;

            DateTime date;

            date = DateTime.Now;

            pf.Birthday = date;

            pf.PhotoUrl = "";
            pf.Private = false;

            pf.Save();

        }


        catch (Exception ex)
            {
                Utils.Log("Error editing profile", ex);

            }

    }


When this runs it does nothing for the Profile, it does not create a New profile and also no errors are being thrown either.

Have it connected to the logger to log exceptions but its not recording anything.

Seems like it is not running at all.

I bet I have some code messed up or something.

Need help :)


Thanks,


Brian Davis
Jul 1, 2014 at 8:16 AM
Hi Rtur,


I also tried this code:


static Profile CreateNewProfile(string id)
    {
        Utils.Log("Starting to Create New Profile");

        if (!Utils.StringIsNullOrWhitespace(id))
        {
            Utils.Log("Creating New Profile For {0} ", id);

            var pf = AuthorProfile.GetProfile(id);
            if (pf == null)
                {

                    Utils.Log("Profile is Empty, Creating new one");

                    pf = new AuthorProfile(id);
                    pf.Birthday = DateTime.Parse("01/01/1900");
                    pf.DisplayName = id;
                    pf.EmailAddress = Utils.GetUserEmail(id);
                    pf.FirstName = id;
                    pf.LastName = "";
                    pf.Private = false;  //Kdavis07  Change this to False
                    pf.Save();

                    Utils.Log("Saved Author Profile");
                }

            return new Profile 
                { 
                    AboutMe = string.IsNullOrEmpty(pf.AboutMe) ? "" : pf.AboutMe,
                    Birthday = pf.Birthday.ToShortDateString(),
                    CityTown = string.IsNullOrEmpty(pf.CityTown) ? "" : pf.CityTown,
                    Country = string.IsNullOrEmpty(pf.Country) ? "" : pf.Country,
                    DisplayName = pf.DisplayName,
                    EmailAddress = pf.EmailAddress,
                    PhoneFax = string.IsNullOrEmpty(pf.PhoneFax) ? "" : pf.PhoneFax,
                    FirstName = string.IsNullOrEmpty(pf.FirstName) ? "" : pf.FirstName,
                    Private = pf.Private,
                    LastName = string.IsNullOrEmpty(pf.LastName) ? "" : pf.LastName,
                    MiddleName = string.IsNullOrEmpty(pf.MiddleName) ? "" : pf.MiddleName,
                    PhoneMobile = string.IsNullOrEmpty(pf.PhoneMobile) ? "" : pf.PhoneMobile,
                    PhoneMain = string.IsNullOrEmpty(pf.PhoneMain) ? "" : pf.PhoneMain,
                    PhotoUrl = string.IsNullOrEmpty(pf.PhotoUrl) ? "" : pf.PhotoUrl.Replace("\"", ""),
                    RegionState = string.IsNullOrEmpty(pf.RegionState) ? "" : pf.RegionState
                };


        }

        Utils.Log("Returning a New Profile"); 
        return null;
    }


This time logger is recording:

Date: 7/1/2014 3:12:12 AM
Starting to Create New Profile
---------------------------------------
Date: 7/1/2014 3:12:12 AM
Creating New Profile For 311
---------------------------------------
Date: 7/1/2014 3:12:12 AM
Profile is Empty, Creating new one
---------------------------------------
Date: 7/1/2014 3:12:12 AM
Saved Author Profile



But, it is not throwing any errors.

Still not creating a new profile.
Jul 1, 2014 at 9:56 AM
Found out it is saving in the root profile folder and not the subBlog/Profiles folder.


How to get it to save to subBlog/Profiles instead of SiteRoot/Profiles/ ?
Jul 1, 2014 at 12:27 PM
I am now using this:

var pf = AuthorProfile.GetProfile(id);
            if (pf == null)
                {
                    var checkProfile = AuthorProfile.GetProfile(id);
                    Utils.Log("Profile is Empty, Creating new one");



                    pf = new AuthorProfile(id);
                    pf.Birthday = DateTime.Parse("01/01/1900");
                    pf.DisplayName = id;
                    pf.EmailAddress = Utils.GetUserEmail(id);
                    pf.FirstName = FirstName.Text;
                    pf.LastName =  LastName.Text;
                    pf.Private = false;
                    //pf.Save();


                    BlogService.InsertProfile(pf);  <--- Seems not to work
                    BlogService.FillProfiles();        <---- Seems not to work

Currently with this code I get an Object not set error.

Was hoping that would fill the profiles within the /subBlog but it does not.


This page is using the create-blog.aspx.cs page if that helps.



:)
Coordinator
Jul 1, 2014 at 4:03 PM
Your issue is likely because you trying to create profile for sub blog. The "save" or "insertprofile" expect you running in the same instance. Not sure why you not getting any errors, probably they suppressed in provider. But changing data on another blog would be major security flaw, when we create new blog we basically physically copy files from "_new" modifying file content itself to match new blog/user.