Skip to content

Instantly share code, notes, and snippets.

@VesselinVassilev
Last active August 1, 2018 02:41
Show Gist options
  • Select an option

  • Save VesselinVassilev/a19946fb4cbc1ad4b958c706dbe7a3db to your computer and use it in GitHub Desktop.

Select an option

Save VesselinVassilev/a19946fb4cbc1ad4b958c706dbe7a3db to your computer and use it in GitHub Desktop.
This creates and updates a dynamic item in all defined languages and creates orphaned records in sf_url_data preventing opening of the child items in the backend
var languages = SystemManager.CurrentContext.AppSettings.DefinedFrontendLanguages.ToList();
for (int j = 0; j < languages.Count; j++)
{
var lang = languages[j];
try
{
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture = lang;
var transactionName = "importCountryData{0}{1}".Arrange(lang.Name, j);
var versionManager = VersionManager.GetManager(null, transactionName);
var manager = DynamicModuleManager.GetManager(string.Empty, transactionName);
var dcCountry = manager.GetMasterDataItemsForType(ScootConstants.Country_DynamicType)
.Where(p => p.GetValue<string>(ScootConstants.Country_CountryCode) == "RO")
.FirstOrDefault();
if (dcCountry == null)
{
dcCountry = manager.CreateDataItemOfType(ScootConstants.Country_DynamicType);
dcCountry.SetString(ScootConstants.Title, "Romania " + lang.Name, lang);
dcCountry.SetString(ScootConstants.UrlName, "romania", lang);
dcCountry.SetWorkflowStatus(manager.Provider.ApplicationName, "Draft", lang);
versionManager.CreateVersion(dcCountry, false);
manager.Lifecycle.Publish(dcCountry);
dcCountry.SetWorkflowStatus(manager.Provider.ApplicationName, "Published", lang);
versionManager.CreateVersion(dcCountry, true);
}
else
{
var checkOutCountryItem = manager.Lifecycle.CheckOut(dcCountry, lang) as DynamicContent;
checkOutCountryItem.SetString(ScootConstants.Title, "Romania " + lang.Name, lang);
var checkInCountryItem = manager.Lifecycle.CheckIn(checkOutCountryItem);
versionManager.CreateVersion(checkInCountryItem, false);
manager.Lifecycle.Publish(checkInCountryItem);
versionManager.CreateVersion(checkInCountryItem, true);
}
TransactionManager.CommitTransaction(transactionName);
}
catch (Exception ex)
{
Log.Write(ex);
}
}
...
public static DynamicContent CreateDataItemOfType(this DynamicModuleManager manager, string type)
{
Type resolvedType = GetDynamicTypeFromFullName(type);
manager.Provider.SuppressSecurityChecks = true;
var item = manager.CreateDataItem(resolvedType);
item.SetValue(ScootConstants.Owner, SecurityManager.GetCurrentUserId());
item.SetValue(ScootConstants.PublicationDate, DateTime.UtcNow);
// set the UrlName to be Guid, can be overriden at caller level
item.SetString(ScootConstants.UrlName, item.Id.ToString());
return item;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment