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

Forums / Developing with Sitefinity / Nested Dynamic Module Help

Nested Dynamic Module Help

1 posts, 0 answered
  1. Brian
    Brian avatar
    26 posts
    Registered:
    12 Jun 2015
    19 Oct
    Link to this post

    Hi Everyone. 

    I am kind of at my wits end at the moment and need some advice.  We are maintaining a legacy site that someone developed in 7.3.  One functionality that does not quite work right is the ability to send communications to the users.  They call them alerts and notifications.  They are both nearly the same except for the way that they are displayed to the users.  This could be from a handful to tens of thousands (possibly over 120k).  So I am only showing code for alerts.

    The way they did it (note the code below), is that they create an alert.  Then then loop through the user list passed in and create an AlertUser for each one and attach that to the parent.  Once the list gets long enough, it never finishes.  There is a global error handler that is supposed to log any errors and nothing is logged.

    So I have two problems to solve.  The first and foremost is to find a way to make this work.  The second is to give the user some kind of progress indicator (we have been trying the RadProgressArea, but it disappears way before the process stops - we have a ticket opened on that).  The latter could be eliminated if the former is quick enough.
    Any suggestions would be appreciated.

    protected static readonly Type AlertType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.MyAlert.Alert");
    protected static readonly Type AlertUserType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.MyAlert.Alertuser");
     
    public void CreateAlert(string text
        , DateTime startTime
        , DateTime expirationTime
        , Dictionary<Guid, string> userList
        , RadProgressContext progressContext
        , HttpResponse response)
    {
        var processed = new List<Guid>();
        // Set the provider name for the DynamicModuleManager here. All available providers are listed in
        // Administration -> Settings -> Advanced -> DynamicModules -> Providers
        var providerName = string.Empty;
        var dynamicModuleManager = DynamicModuleManager.GetManager(providerName);
        dynamicModuleManager.Provider.SuppressSecurityChecks = true;
        try
        {
            var alertItem = dynamicModuleManager.CreateDataItem(AlertType);
            // This is how values for the properties are set
            alertItem.SetValue("AlertText", text);
            alertItem.SetValue("AlertExpiration", expirationTime);
            alertItem.SetValue("AlertStartTime", startTime);
            alertItem.SetString("UrlName", alertItem.Id.ToString());
            alertItem.SetValue("Owner", SecurityManager.GetCurrentUserId());
            alertItem.SetValue("PublicationDate", DateTime.UtcNow);
            alertItem.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName, "Draft");
            foreach (var user in userList)
            {
                // Make sure we are going to be running this twice on anyone.
                if (processed.Contains(user.Key)) continue;
                processed.Add(user.Key);
                // Was the cancel button clicked or was the browser closed?  If so, cancel the operation
                // This is how we add child items
                var alertuserItem = CreateAlertUser(alertItem.Id, user.Key, user.Value);
                if (alertuserItem != null) alertItem.AddChildItem(alertuserItem);
            }
            // You need to call SaveChanges() in order for the items to be actually persisted to data store
            dynamicModuleManager.SaveChanges();
            // We can now call the following to publish the item
            var publishedAlertItem = dynamicModuleManager.Lifecycle.Publish(alertItem);
            //You need to set appropriate workflow status
            alertItem.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName, "Published");
            // You need to call SaveChanges() in order for the items to be actually persisted to data store
            dynamicModuleManager.SaveChanges();
        }
        finally
        {
            dynamicModuleManager.Provider.SuppressSecurityChecks = false;
        }
    }
     
    public DynamicContent CreateAlertUser(Guid parentId, Guid userId, string userName)
    {
        var providerName = string.Empty;
        var dynamicModuleManager = DynamicModuleManager.GetManager(providerName);
        try
        {
            // Set the provider name for the DynamicModuleManager here. All available providers are listed in
            // Administration -> Settings -> Advanced -> DynamicModules -> Providers
            dynamicModuleManager.Provider.SuppressSecurityChecks = true;
            var alertuserItem = dynamicModuleManager.CreateDataItem(AlertUserType);
            // This is how values for the properties are set
            alertuserItem.SetValue("UserName", userName);
            alertuserItem.SetValue("AlertId", parentId);
            alertuserItem.SetValue("UserId", userId);
            alertuserItem.SetString("UrlName", alertuserItem.Id.ToString());
            alertuserItem.SetValue("Owner", SecurityManager.GetCurrentUserId());
            alertuserItem.SetValue("PublicationDate", DateTime.UtcNow);
            alertuserItem.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName, "Draft");
            // Set item parent
            alertuserItem.SetParent(parentId, AlertType.FullName);
            // You need to call SaveChanges() in order for the items to be actually persisted to data store
            dynamicModuleManager.SaveChanges();
            // We can now call the following to publish the item
            var publishedAlertuserItem = dynamicModuleManager.Lifecycle.Publish(alertuserItem);
            //You need to set appropriate workflow status
            alertuserItem.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName, "Published");
            // You need to call SaveChanges() in order for the items to be actually persisted to data store
            dynamicModuleManager.SaveChanges();
            return alertuserItem;
        }
        finally
        {
            dynamicModuleManager.Provider.SuppressSecurityChecks = false;
        }
    }

1 posts, 0 answered