Portal Engine Questions on portal engine and web parts.
Version 6.x > Portal Engine > Subscribing to newsletter throw API, not showing under PROFILE View modes: 
User avatar
Member
Member
eagleag - 9/3/2012 5:55:51 AM
   
Subscribing to newsletter throw API, not showing under PROFILE
Hi,
I'm using API to allow users to signup for newsletter on registration form.

When I check under CMSDesk -> tools -> newsletter- > Subscribers I see the new added subscriber.
But, if I go to the new userrs PROFILE page, using My account (webpart) and enabled Display Newsletters subscriptions: id do not see the newsletter that the user has subscribed for.
I do see this:

Your e-mail address myemail@yahoo.com is currently subscribed to receive the following newsletters:
No newsletters selected


This is the code im using for subscribing users to newsletter:

private void SubscribeNewsletter(string newsletterName, Subscriber sb)
{
// bool toReturn = false;
int siteId = CMSContext.CurrentSiteID;

// Check if sunscriber info object exists
if ((sb == null) || string.IsNullOrEmpty(newsletterName))
{
return;
}

// Get nesletter info
Newsletter news = NewsletterProvider.GetNewsletter(newsletterName, siteId);
if (news != null)
{
try
{
// Check if subscriber is not allready subscribed
if (!SubscriberProvider.IsSubscribed(sb.SubscriberGUID, news.NewsletterGUID, siteId))
{
// toReturn = true;

// Subscribe to the site
SubscriberProvider.Subscribe(sb.SubscriberID, news.NewsletterID, DateTime.Now, true);// this.SendConfirmationEmail);

//// Info message
//if (!chooseMode)
//{
// lblInfo.Visible = true;
// lblInfo.Text = GetString("NewsletterSubscription.Subscribed");
//}

// Track successful subscription conversion
if (this.TrackConversionName != string.Empty)
{
string siteName = CMSContext.CurrentSiteName;

if (AnalyticsHelper.AnalyticsEnabled(siteName) && AnalyticsHelper.TrackConversionsEnabled(siteName) && !AnalyticsHelper.IsIPExcluded(siteName, HTTPHelper.UserHostAddress))
{
// Log conversion
HitLogProvider.LogConversions(siteName, CMSContext.PreferredCultureCode, TrackConversionName, 0, ConversionValue);
}
}

// Log newsletter subscription activity if double opt-in is not required
if (!news.NewsletterEnableOptIn)
{
if ((CMSContext.ViewMode == ViewModeEnum.LiveSite) && news.NewsletterLogActivity && ActivitySettingsHelper.ActivitiesEnabledAndModuleLoaded(siteId) &&
ActivitySettingsHelper.ActivitiesEnabledForThisUser(CMSContext.CurrentUser) &&
ActivitySettingsHelper.NewsletterSubscribeEnabled(siteId))
{
int contactId = ModuleCommands.OnlineMarketingGetCurrentContactID();
ModuleCommands.OnlineMarketingUpdateContactFromExternalData(sb, contactId);
ModuleCommands.OnlineMarketingCreateRelation(sb.SubscriberID, MembershipType.NEWSLETTER_SUBSCRIBER, contactId);

if (ActivitySettingsHelper.ActivitiesEnabledForThisUser(CMSContext.CurrentUser))
{
var data = new ActivityData()
{
ContactID = contactId,
SiteID = sb.SubscriberSiteID,
Type = PredefinedActivityType.NEWSLETTER_SUBSCRIBING,
TitleData = news.NewsletterName,
ItemID = news.NewsletterID,
URL = URLHelper.CurrentRelativePath,
Campaign = CMSContext.Campaign
};
ActivityLogProvider.LogActivity(data);
}
}
}
}
}
catch (Exception ex)
{
lblError.Visible = true;
lblError.Text = ex.Message;
}
}
else
{
lblError.Visible = true;
lblError.Text = GetString("NewsletterSubscription.NewsletterDoesNotExist");
}

//return toReturn;
}




/// <summary>
/// Save subscriber.
/// </summary>
/// <returns>Subscriver info object</returns>
private Subscriber SaveSubscriber(UserInfo uip)
{
UserInfo ui = uip;
// Check if a subscriber exists first
Subscriber sb = null;

sb = SubscriberProvider.GetSubscriber(ui.Email, CMSContext.CurrentSiteID);


if (sb == null)
{
// Create subscriber
if (sb == null)
{
sb = new Subscriber();
}


// Get user info and copy first name, last name or full name to new subscriber
// UserInfo ui = UserInfoProvider.GetUserInfo(CMSContext.CurrentUser.UserID); // ag
if (ui != null)
{
if (!DataHelper.IsEmpty(ui.FirstName) && !DataHelper.IsEmpty(ui.LastName))
{
sb.SubscriberFirstName = ui.FirstName;
sb.SubscriberLastName = ui.LastName;
sb.SubscriberEmail = ui.Email;
}
else
{
sb.SubscriberFirstName = ui.FullName;
sb.SubscriberEmail = ui.Email;
}
// Full name consists of "user " and user full name
sb.SubscriberFullName = "User '" + ui.FullName + "'";
}
else
{
return null;
}

sb.SubscriberType = "cms.user";
sb.SubscriberRelatedID = CMSContext.CurrentUser.UserID;




// Set site ID
sb.SubscriberSiteID = CMSContext.CurrentSiteID;

// Check subscriber limits
if (!SubscriberProvider.LicenseVersionCheck(URLHelper.GetCurrentDomain(), FeatureEnum.Subscribers, VersionActionEnum.Insert))
{
lblError.Visible = true;
lblError.Text = GetString("LicenseVersionCheck.Subscribers");
return null;
}

// Save subscriber info
SubscriberProvider.SetSubscriber(sb);
}



return sb;
}


Any ideas why under PROFILE -> SUBSCRIPTIONS new newsletter is showing?


User avatar
Member
Member
eagleag - 9/4/2012 2:05:13 AM
   
RE:Subscribing to newsletter throw API, not showing under PROFILE
I found a fix, I've added the following code:


// Set site ID
sb.SubscriberSiteID = CMSContext.CurrentSiteID;
// added
sb.SubscriberType = SiteObjectType.USER;
sb.SubscriberRelatedID = ui.UserID;
// end added