Feedback
Did this article resolve your question/issue?

   

Article

Related data cannot be retrieved in the DynamicContentUpdated event

« Go Back

Information

 
TitleRelated data cannot be retrieved in the DynamicContentUpdated event
URL NameRelated-data-cannot-be-retrieved-in-the-DynamicContentUpdated-event
Article Number000188037
EnvironmentProduct: Sitefinity
Version: 7.x, 8.0
OS: Windows 7 64bit
Database: Microsoft SQL Server 2012 SP2
Question/Problem Description
Sitefinity EventHub allows you to capture the UpdatedEvent of a dynamic item. A bug prevents this in versions before 8.0.5702.0.
Steps to Reproduce
Clarifying Information
Error Message
Defect Numberhttp://feedback.telerik.com/Project/153/Feedback/Details/128509-related-data-cannot-be-retrieved-from-item-in-the-dynamiccontentupdated-event
Enhancement Number
Cause
Due to the bug, the data cannot be retrieved when the item is updated.
Resolution

Capture the dirty items on the provider level and check there for the updated or created items and get the required information.

Subscribe to the Executing and Executed events of the provider:

void Bootstrapper_Initialized(object sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)
       {
           if (e.CommandName == "Bootstrapped")
           {
                
               var provider = DynamicModuleManager.GetManager().Provider;
               provider.Executing += Provider_Executing;
               provider.Executed += provider_Executed;
           }
       }

 

In executing capture the dirty items and persist their ids:

 

private void Provider_Executing(object sender, Telerik.Sitefinity.Data.ExecutingEventArgs e)
       {
           if (!(e.CommandName == "CommitTransaction" || e.CommandName == "FlushTransaction"))
               return;
   
           var provider = sender as DynamicModuleDataProvider;
           if (provider == null)
           {
               return;
           }
   
           //Create separate containers for information coming from updated and craeted items
   
           HashSet<Guid> updatedItemsUrls = provider.GetExecutionStateData("DeletedItemUrls") as HashSet<Guid>;
           HashSet<Guid> createdItemsIds = new HashSet<Guid>();
   
           //Added a container for the item types to allow easy retrieval of the item and for it to work for all dynamic types
           HashSet<string> updatedItemType = new HashSet<string>();
           HashSet<string> cretedItemType = new HashSet<string>();
   
           if (updatedItemsUrls == null)
               updatedItemsUrls = new HashSet<Guid>();
   
           var dirtyItems = provider.GetDirtyItems();
   
           for (int i = 0; i < dirtyItems.Count; i++)
           {
               var item = dirtyItems[i] as DynamicContent;
               if (item == null)
                   continue;
   
               var itemStatus = provider.GetDirtyItemStatus(item);
               if (itemStatus == Telerik.Sitefinity.Security.SecurityConstants.TransactionActionType.Updated)
               {
                   updatedItemsUrls.Add(item.Id);
                   updatedItemType.Add(item.GetType().ToString());
               }
               else if (itemStatus == Telerik.Sitefinity.Security.SecurityConstants.TransactionActionType.New)
               {
                   createdItemsIds.Add(item.Id);
                   cretedItemType.Add(item.GetType().ToString());
               }
           }
   
           if (updatedItemsUrls.Count > 0)
           {
               provider.SetExecutionStateData("UpdatedItemId", updatedItemsUrls);
               provider.SetExecutionStateData("UpdatedItemType", updatedItemType);
               provider.SetExecutionStateData("CreatedItemIds", createdItemsIds);
               provider.SetExecutionStateData("CreatedItemType", cretedItemType);
           }
       }
in the executed event, we can get the ids and based on them get the RelatedData as needed
private void provider_Executed(object sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)
        {
            if (e.CommandName != "CommitTransaction")
                return;
   
            var provider = sender as DynamicModuleDataProvider;
            if (provider == null)
            {
                return;
            }
   
            //Get the required information
            var updatedItem = provider.GetExecutionStateData("UpdatedItemId") as HashSet<Guid>;
            var updatedItemType = provider.GetExecutionStateData("UpdatedItemType") as HashSet<string>;
            var createdItem = provider.GetExecutionStateData("CreatedItemIds") as  HashSet<Guid>;
            var createdItemType = provider.GetExecutionStateData("CreatedItemType") as HashSet<string>;
   
            //I introduced the same logic separately depending on if the items is updated or created for the first time
            if (updatedItem != null)
            {
                foreach (var id1 in updatedItem)
                {
                    var id = id1;
                    var providerName = String.Empty;
   
                    DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(providerName);
                       
                    Type questionType = TypeResolutionService.ResolveType(updatedItemType.FirstOrDefault());
                    var item = dynamicModuleManager.GetDataItem(questionType, id);
                    if (item.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live)
                    {
                        //You can add different logic for the different dynamic types that you have.
                        if (item.GetType().ToString() == "Telerik.Sitefinity.DynamicTypes.Model.SleepyAdvisor.Question")
                        {
                            var relatedData = item.GetRelatedItems<DynamicContent>("Answers");
                        }
                    }
                }
            
Workaround
Notes
Last Modified Date7/20/2016 1:59 PM
Attachment 
Files
Disclaimer The origins of the information on this site may be internal or external to Progress Software Corporation (“Progress”). Progress Software Corporation makes all reasonable efforts to verify this information. However, the information provided is for your information only. Progress Software Corporation makes no explicit or implied claims to the validity of this information.

Any sample code provided on this site is not supported under any Progress support program or service. The sample code is provided on an "AS IS" basis. Progress makes no warranties, express or implied, and disclaims all implied warranties including, without limitation, the implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample code is borne by the user. In no event shall Progress, its employees, or anyone else involved in the creation, production, or delivery of the code be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample code, even if Progress has been advised of the possibility of such damages.