Skip to content

Instantly share code, notes, and snippets.

@hawjeh

hawjeh/Index.cshtml

Last active Feb 26, 2021
Embed
What would you like to do?
Sitefinity Field Migration Tool
@if (ViewBag.IsUserInRole)
{
<section class="container">
<div class="row" style="margin: 30px">
<div>
<h3>Migration Service</h3>
</div>
<br>
<div class="col-12 form-group">
<label>Service:</label>
<input name="methodName" type="text" id="methodName" placeholder="Migration Method Name">
</div>
<br>
<button type="button" id="w-btn-start" onclick="Run();">Run</button>
</div>
<div class="row" style="margin: 30px">
<div>
<p id="messageResult"></p>
</div>
</div>
</section>
<script>
function Run() {
$('#w-btn-start').attr('disabled', true);
$.post('@Url.Action("Process")', {
methodName: $('input[name=methodName]').val()
}).done(function (data) {
$('#w-btn-start').attr('disabled', false);
$('#messageResult').html(data.message);
}).fail(function (xhr, param) {
console.log(xhr);
console.log(param);
});
}
</script>
}
[ControllerToolboxItem(Name = "MigrationController", Title = "Migration Service", SectionName = "Custom Widgets")]
public class MigrationController : Controller
{
private static string ADMIN_ROLE = "Administrators";
public ActionResult Index()
{
var identity = ClaimsManager.GetCurrentIdentity();
var roleManagerApp = RoleManager.GetManager("AppRoles");
ViewBag.IsUserInRole = roleManagerApp.IsUserInRole(identity.UserId, ADMIN_ROLE);
return View();
}
[HttpPost]
public JsonResult Process(string methodName)
{
var message = string.Empty;
try
{
var service = new MigrationService();
var method = service.GetType().GetMethod(methodName);
message = (string)method.Invoke(service, null);
Log.Write(message, ConfigurationPolicy.Trace);
}
catch (Exception ex)
{
message = methodName + " is an inappropriate method name.";
Log.Write(ex.Message, ConfigurationPolicy.ErrorLog);
Log.Write(message, ConfigurationPolicy.ErrorLog);
}
return Json(new { success = true, message = message });
}
}
public class MigrationService
{
public static string RunEventMigration()
{
var message = "<Migration Message Header> <br/>";
var counter = 0;
try
{
var dynamicModule = new DynamicModuleManager(string.Empty, "<MigrationName>");
var eventType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.<Module>.<ContentType>");
var eventsDynamicContents = dynamicModule
.GetDataItems(eventType)
.Where(p => p.Status != ContentLifecycleStatus.Deleted); // Get all except the deleted one
var tempEvent = eventsDynamicContents.FirstOrDefault();
if (tempEvent == null || !tempEvent.DoesFieldExist("PostalCodes"))
return "Please setup PostalCodes-ShortText at Event module builder.";
dynamicModule.Provider.SuppressSecurityChecks = true;
foreach (var item in eventsDynamicContents)
{
// Only update when PostalCodes has never insert value
var postalCodes = item.GetValue("PostalCodes")?.ToString();
var postalCodeOld = item.GetValue("PostalCode")?.ToString();
if (string.IsNullOrEmpty(postalCodes) && !string.IsNullOrEmpty(postalCodeOld))
{
var title = item.GetValue("Title")?.ToString();
item.SetValue("PostalCodes", postalCodeOld);
TransactionManager.CommitTransaction(dynamicModule.TransactionName);
message += title + " Migrated.<br/>";
counter++;
}
}
dynamicModule.Provider.SuppressSecurityChecks = false;
}
catch (Exception ex)
{
message += ex.Message;
Log.Write(ex, ConfigurationPolicy.ErrorLog);
}
message += "<br/>Total PostalCodes migrate: " + counter.ToString();
return message;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment