Reputation: 11
I tried to use the following code to publish a dynamic content item. It works, but I thought it would create a new record in revision history of that item, but it doesn't:
public void PublishProduct()
{
// Set the provider name for the DynamicModuleManager here. All available providers are listed in
// Administration -> Settings -> Advanced -> DynamicModules -> Providers
var providerName = String.Empty;
// Set a transaction name and get the version manager
var transactionName = "someTransactionName";
var versionManager = VersionManager.GetManager(null, transactionName);
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(providerName, transactionName);
Type productType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Products.Product");
DynamicContent productItem = dynamicModuleManager.CreateDataItem(productType);
// This is how values for the properties are set
productItem.SetValue("Title", "Some Title");
productItem.SetValue("Description", "Some Description");
productItem.SetString("UrlName", "SomeUrlName");
productItem.SetValue("Owner", SecurityManager.GetCurrentUserId());
productItem.SetValue("PublicationDate", DateTime.UtcNow);
productItem.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName, "Draft");
// Create a version and commit the transaction in order changes to be persisted to data store
versionManager.CreateVersion(productItem, false);
// We can now call the following to publish the item
ILifecycleDataItem publishedproductItem = dynamicModuleManager.Lifecycle.Publish(productItem);
//You need to set appropriate workflow status
productItem.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName, "Published");
// Create a version and commit the transaction in order changes to be persisted to data store
versionManager.CreateVersion(productItem, true);
// Commit the transaction in order for the items to be actually persisted to data store
TransactionManager.CommitTransaction(transactionName);
}
I used another method as well but doesn't work as well:
public void PublishProduct()
{
// Set the provider name for the DynamicModuleManager here. All available providers are listed in
// Administration -> Settings -> Advanced -> DynamicModules -> Providers
var providerName = String.Empty;
var versionManager = VersionManager.GetManager();
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(providerName, transactionName);
Type productType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Products.Product");
DynamicContent productItem = dynamicModuleManager.CreateDataItem(productType);
// This is how values for the properties are set
productItem.SetValue("Title", "Some Title");
productItem.SetValue("Description", "Some Description");
productItem.SetString("UrlName", "SomeUrlName");
productItem.SetValue("Owner", SecurityManager.GetCurrentUserId());
productItem.SetValue("PublicationDate", DateTime.UtcNow);
productItem.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName, "Draft");
// Create a version and commit the transaction in order changes to be persisted to data store
versionManager.CreateVersion(productItem, true);
versionManager.SaveChanges();
productItem.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName, "Draft");
dynamicModuleManager.SaveChanges();
dynamicModuleManager.Lifecycle.Publish(productItem);
productItem.ApprovalWorkflowState.SetString("Published", true);
dynamicModuleManager.SaveChanges();
}
I tried to move versionManager.CreateVersion(productItem, true); to some another places in the code as well, for eg: after publishing the item, or the end of the method, but nothing created in revision history.
Could anyone help please? Thanks in advance!
Upvotes: 1
Views: 334
Reputation: 1
I think revision history is for one "Item". But you are creating here a new item with:
DynamicContent productItem = dynamicModuleManager.CreateDataItem(productType);
I suggest you somehow find your existing item, edit, and commit/publish to add new record to revision history of that EXISTING item.
I would try something like this:
public void AddOrUpdateItem(string someIdentifier)
{
var providerName = string.Empty;
string txName = Guid.NewGuid().ToString();
try
{
var versionManager = VersionManager.GetManager(null, txName);
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(providerName, txName);
DynamicContent someItem = GetItemBySomeIdentifier(someIdentifier);
bool createNew = someItem == null;
if (createNew)
{
someItem = dynamicModuleManager.CreateDataItem(_bannerMetadataType);
someItem.SetValue("SomeIdColumn", someIdentifier);
someItem.SetValue("Owner", SecurityManager.GetCurrentUserId());
someItem.SetString("UrlName", someIdentifier);
}
someItem.SetValue("SomeFiled", "xyz");
someItem.SetValue("PublicationDate", DateTime.Now);
someItem.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName, "Published",
CultureInfo.CreateSpecificCulture(SystemManager.CurrentContext.CurrentSite.DefaultCulture));
versionManager.CreateVersion(someItem, true);
TransactionManager.CommitTransaction(txName);
}
catch (Exception)
{
TransactionManager.RollbackTransaction(txName);
throw;
}
}
Upvotes: 0