Sitefinity-发布动态内容项目时修订历史记录为空

时间:2018-08-17 03:32:10

标签: sitefinity

我尝试使用以下代码发布动态内容项。它可以工作,但是我认为它将在该项目的修订历史记录中创建一个新记录,但是没有:

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);
}

我也使用了另一种方法,但是效果不佳:

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();
}

我试图移动versionManager.CreateVersion(productItem,true);也可以移至代码中的其他位置,例如:发布项目后或方法结束时,但在修订历史记录中未创建任何内容。

有人可以帮忙吗?预先感谢!

1 个答案:

答案 0 :(得分:0)

我认为修订历史是针对一个“项目”的。但是您要在此处创建具有以下内容的新项目:

DynamicContent productItem = dynamicModuleManager.CreateDataItem(productType);

我建议您以某种方式查找现有项目,进行编辑并提交/发布,以将新记录添加到该现有项目的修订历史记录中。

我会尝试这样的事情:

    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;
        }
    }