API Questions on Kentico API.
Version 6.x > API > Exporting custom table programmatically in Kentico View modes: 
User avatar
Member
Member
nqwebdev1 - 8/28/2012 1:26:02 PM
   
Exporting custom table programmatically in Kentico
Hi there,

I'm trying to export a custom table but it doesn't work. I've read the API but this one doesn't make sense to me yet. What's a suggested fix for this?


private void exportCustomTable(string tablename)
{

DataClassInfo customTableClassInfo = DataClassInfoProvider.GetDataClass(tablename);
if (customTableClassInfo != null)
{
CustomTableItemProvider ctiProvider = new CustomTableItemProvider(CMSContext.CurrentUser);
string websitePath = Server.MapPath("~/");
string exportFileName = string.Format("table_" + tablename + "{0:yyyy-MM-dd_hh-mm}.zip", DateTime.Now);
string exportFilePath = FileHelper.GetFullPhysicalPath(exportFileName,ImportExportHelper.GetSiteUtilsFolder(), websitePath);

if (!File.Exists(exportFilePath))
{
ExportProvider.ExportObject(customTableClassInfo, exportFilePath, websitePath, CMSContext.CurrentUser);
//lblStatus.Text = "Export OK";
}
else
{
// lblStatus.Text = "Export Failed";
}
}
}

User avatar
Member
Member
nqwebdev1 - 8/28/2012 1:28:10 PM
   
RE:Exporting custom table programmatically in Kentico
By the way, I'm using Kentico v7.0RC, not v6.0 ...

User avatar
Member
Member
kentico_michal - 8/30/2012 1:52:33 AM
   
RE:Exporting custom table programmatically in Kentico
Hi,

Do you receive any error message? Could you please check if there is any related log in the Event log [Site manager -> Administration -> Event log].

Best regards,
Michal Legen

User avatar
Member
Member
nqwebdev1 - 8/30/2012 9:46:24 AM
   
RE:Exporting custom table programmatically in Kentico
No, there's no error message in either CMSDesk or Administration Event Log...

I think this is the error but I don't know the fix since it's really not clear in the API Documentation. Can you please correct it for me?


...............
DataClassInfo customTableClassInfo = DataClassInfoProvider.GetDataClass(tablename);
...............
CustomTableItemProvider ctiProvider = new CustomTableItemProvider(CMSContext.CurrentUser);
....
ExportProvider.ExportObject(customTableClassInfo, exportFilePath, websitePath, CMSContext.CurrentUser);

User avatar
Member
Member
kentico_michal - 9/7/2012 4:17:55 AM
   
RE:Exporting custom table programmatically in Kentico
Hi,

The ExportObject method exports only a custom table definition. To export the definition with custom table data, please try to use the following code:


string websitePath = Server.MapPath("~/");
DataClassInfo customTableClassInfo = DataClassInfoProvider.GetDataClass("custom table code name");

// Init the Mimetype helper (required for the export)
MimeTypeHelper.LoadMimeTypes();

// Prepare settings
SiteExportSettings settings = new SiteExportSettings(CMSContext.CurrentUser);

// Initialize web site path
if (websitePath != null)
{
settings.WebsitePath = websitePath;
}

string fullExportFilePath = FileHelper.GetFullFolderPhysicalPath(ImportExportHelper.GetSiteUtilsFolder(), websitePath) + "Export\\" + "CustomTableExport.zip";

// Initialize export file path
if (fullExportFilePath != null)
{
settings.TargetPath = Path.GetDirectoryName(fullExportFilePath);
settings.TargetFileName = Path.GetFileName(fullExportFilePath);
}

settings.SetSettings(ImportExportHelper.SETTINGS_CUSTOMTABLE_DATA, true);

// Initialize single object export
ImportExportHelper.InitSingleObjectExportSettings(settings, customTableClassInfo.Generalized);

// Export object
ExportProvider.ExportObjectsData(settings);


Best regards,
Michal Legen

User avatar
Member
Member
nqwebdev1 - 9/8/2012 10:26:42 PM
   
RE:Exporting custom table programmatically in Kentico
Hi Michal,

This works, thank you so much! However, the import is now problematic ~ is there a way so that the primary key can be overridden? I cannot afford to use UUID() as primary/foreign key as this will take a toll...

User avatar
Member
Member
nqwebdev1 - 9/9/2012 3:17:00 PM
   
RE:Exporting custom table programmatically in Kentico
Also, in addition to my reply above, if you can please add this to the import/export:

set identity_insert <the_table_name> off;

....... import/export stuff.....

set identity_insert <the_table_name> on;


User avatar
Member
Member
kentico_michal - 9/19/2012 7:39:46 AM
   
RE:Exporting custom table programmatically in Kentico
Hi,

Regrettably, the current API does not provide this funtionality. You can try to create a new custom table without the GUID column.

Best regards,
Michal Legen