Add data to custom table with API

Stefan Lorenz asked on June 1, 2014 05:43

Hi,

I created a custom table and tried to add items from code within a scheduled task. Getting the DataClassInfo works, but unfortunately I'm getting the error [AbstractProvider.GetProvider]: The object type 'customtableitem.mytables.myfiles' is missing the provider type configuration. in the line

CustomTableItem newCustomTableItem = CustomTableItem.New("mytables.myfiles");

I'm a bit lost here. What am I doing wrong here?

Correct Answer

Brenden Kehren answered on June 2, 2014 06:31

You're right Stefan, I missed that crucial part of the code. This is what I use for v8, it does a lookup for the columns and matches them and sets the values assuming the column names are the same from the source datasource to the target or Kentico custom table.

/// <summary>
/// Creates an item for a custom table and dynamically adds the columns data based on the classinfo and row column comparison
/// </summary>
/// <param name="Dci"></param>
/// <param name="Row"></param>
/// <returns></returns>
public static bool CreateCustomTableItem(DataClassInfo Dci, DataRow Row)
{
    bool okInsert = false;

    if (Dci == null)
    {
        throw new Exception("Data class info cannot be null.");
    }
    else
    {
        // Creates new Custom table item provider
        CustomTableItemProvider customTableProvider = new CustomTableItemProvider(Dci.ClassName);

        // Creates new custom table item object
        CustomTableItem newCustomTableItem = CustomTableItem.New(Dci.ClassName, Row);

        List<string> classProperties = new List<string>();
        // get the class definition
        var doc = XDocument.Parse(Dci.ClassFormDefinition);
        // get a list of all the tables properties by column value
        List<string> elements = doc.Descendants("field").Select(el => el.Attribute("column").Value).ToList();

        // got the class properties now compare
        foreach (string s in elements)
        {
            // see if the class info contains the column in the data row passed in this method
            if (Row.Table.Columns.Contains(s))
            {
                // contains the column so set a value
                newCustomTableItem.SetValue(s, Row[s]);
                okInsert = true;
            }
        }

        if (okInsert)
        {
            // Inserts the custom table item into database
            newCustomTableItem.Insert();
        }
    }
    return okInsert;
}
2 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on June 1, 2014 13:54

If you're using version 7 you need to pass the provider as well.

        // Creates new Custom table item provider
        CustomTableItemProvider customTableProvider = new CustomTableItemProvider(UserInfoProvider.GetUserInfo("Administrator"));

        if (Dci == null)
        {
            throw new Exception("Data class info cannot be null.");
        }
        else
        {
            // Creates new custom table item object
            CustomTableItem newCustomTableItem = CustomTableItem.New("class.name", customTableProvider);
            // do some work here
        }

In v8 you simply use:

CustomTableItem newCustomTableItem = CustomTableItem.New("class.name", dataRow);
newCustomTableItem.Insert();
0 votesVote for this answer Mark as a Correct answer

Stefan Lorenz answered on June 2, 2014 05:11

Hi Brenden,

I'm using v8. I got the feeling that I'm missing something crucial. You provide a dataRow to CustomTableItem.New(), so I wonder how the system maps the row to the custom table fields. I thought that it should be also possible to call CustomTableItem.New("class.name") without a DataRow, add data manually with newCustomTableItem.SetValue() and then call newCustomTableItem.Insert() to save it. Shouldn't this be possible as well?

If not, I ask myself if I need to create a "mapping class" and/or have the DataRow contain identical field names as my custom table has?

Thanks for your help Stefan

0 votesVote for this answer Mark as a Correct answer

Stefan Lorenz answered on June 3, 2014 02:36

Thanks Brenden, it's working now!

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.