Updating user notification preferences using UpdatePreference() method In-Process API

I'm trying to turn off all notificiatons for new users that join the community and have written code to use the In-Process API to loop round each notification setting and then make another call with the In-Process API to disable each setting in turn for that user. I'm passing null as the distribution type as I want to turn off both email and live notifications.

However whatever I try I don't seem to get an AdditionalInfo response back and I don't get any errors or warnings either. 

I've tried running this code as the new user and as the "admin" account but neither of these return a response.

Am I doing something wrong here?

        private void Events_AfterCreate(UserAfterCreateEventArgs e)
        {
            if (e.IsSystemAccount != null && e.IsSystemAccount.Value && e.Username == "Anonymous")
                return;

            UpdateUserNotificationSettings(e);
        }
        

        private void UpdateUserNotificationSettings(ReadOnlyUserEventArgsBase user)
        {
            if (user.IsSystemAccount.HasValue && user.IsSystemAccount.Value)
                return;

            if (!user.Id.HasValue)
                return;
           
            foreach (var notification in Apis.Get<Telligent.Evolution.Extensibility.Api.Version1.INotifications>().ListNotificationTypes())
            {
                AdditionalInfo additionalInfo = new AdditionalInfo();

                Apis.Get<IUsers>().RunAsUser(user.Username, () =>
                {
                    additionalInfo = Apis.Get<Telligent.Evolution.Extensibility.Api.Version1.INotifications>().UpdatePreference(notification.NotificationTypeId, null, false);
                });

                var warnings = (additionalInfo?.Warnings?.Count > 0) ? additionalInfo.Warnings[0].Message : string.Empty;
                var errors = (additionalInfo?.Errors?.Count > 0) ? additionalInfo.Errors[0].Message : string.Empty;

                _eventLogService.Log($"Notification Setting: {notification.Name} updated for user : {user.Username} - updated? : { additionalInfo!=null && !additionalInfo.HasErrors()} - warnings: {warnings} - errors: {errors} ");

            }
        }

Parents Reply
  • Ahr your right. I just logged in as one of the test users and their notifications have all been disabled as expected. I was running this code without the RunAsUser() before and when I logged in as these users their notification settings were still enabled but that may have been why. It would be good if this came back as not null so we can at least check the .HasErrors() property. Thanks for confirming I'm not going mad anyway.

Children
No Data