Capture the dirty items on the provider level and check there for the updated or created items and get the required information.
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"
);
}
}
}