在 Kentico CMS 中,如何以编程方式为用户订阅时事通讯?

发布于 2024-08-02 14:41:31 字数 1089 浏览 11 评论 0原文

完成自定义付款表单后,我添加了两个用户角色、一个徽章和两个朋友。所有这些更改都运行良好并填充到用户对象和相应的数据库表中。当我使用下面的代码(从 Kentico CMS 安装复制和更改)时,我得到 IsSubscribed == true,但用户似乎从未订阅过。有什么想法吗?

int newsletterId = 3;
Subscriber sb = SubscriberProvider.GetSubscriber("cms.user", currentUser.UserID);
    if (sb == null)
     {
            // Create new subscriber
            sb = SaveSubscriber(); // CreateSubscriber(value[0], itemId);
      }
      // If subscriber exists and is not subscribed, subscribe him
      if ((sb != null) && (!SubscriberProvider.IsSubscribed(sb.SubscriberID, newsletterId)))
      {
         try
         {
             SubscriberProvider.Subscribe(sb.SubscriberID, newsletterId, DateTime.Now, false);
             lblTroubledUser.Text = "shoulda been subscribed to newsletter";
         }
         catch
         {
              lblTroubledUser.Text = "problem subscribing to newsletter";
         }
      }
      else
            lblTroubledUser.Text = (!SubscriberProvider.IsSubscribed(sb.SubscriberID, newsletterId)) ? "sb is null" : "already subscribed to newsletter!";

Upon completion of a custom payment form I add two user roles, a badge, and two friends. All of those changes work well and populate to the User object and corresponding database tables. When I use the below code (copied and altered from Kentico CMS install) I get IsSubscribed == true, but the user never appears to be subscribed. Any ideas?

int newsletterId = 3;
Subscriber sb = SubscriberProvider.GetSubscriber("cms.user", currentUser.UserID);
    if (sb == null)
     {
            // Create new subscriber
            sb = SaveSubscriber(); // CreateSubscriber(value[0], itemId);
      }
      // If subscriber exists and is not subscribed, subscribe him
      if ((sb != null) && (!SubscriberProvider.IsSubscribed(sb.SubscriberID, newsletterId)))
      {
         try
         {
             SubscriberProvider.Subscribe(sb.SubscriberID, newsletterId, DateTime.Now, false);
             lblTroubledUser.Text = "shoulda been subscribed to newsletter";
         }
         catch
         {
              lblTroubledUser.Text = "problem subscribing to newsletter";
         }
      }
      else
            lblTroubledUser.Text = (!SubscriberProvider.IsSubscribed(sb.SubscriberID, newsletterId)) ? "sb is null" : "already subscribed to newsletter!";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓦然回首 2024-08-09 14:41:31

知道了!构建订阅者后,您必须将它们添加到新闻通讯中。你必须使用:

SubscriberNewsletterInfoProvider.AddSubscriberToNewsletter(sb.SubscriberID, newsletterId, DateTime.Now);

Got it! After you build the Subscriber you have to add them to the Newsletter. You have to use:

SubscriberNewsletterInfoProvider.AddSubscriberToNewsletter(sb.SubscriberID, newsletterId, DateTime.Now);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文