return custom table item ID after insert

lawrence whittemore asked on December 20, 2022 18:54

how would I return the custom table itemID after the item is inserted?

Correct Answer

Brenden Kehren answered on December 21, 2022 14:39

After the newCustomTableItem.Insert() call, try this:

var newId = newCustomTableItem.ItemId;

However, I'd recommend using the GUID as that won't change between environments.

1 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on December 21, 2022 13:10

How are you inserting it?

0 votesVote for this answer Mark as a Correct answer

lawrence whittemore answered on December 21, 2022 14:34

using the API like this

// Prepares the code name (class name) of the custom table to which the data record will be added

string customTableClassName = "customtable.sampletable";

// Gets the custom table DataClassInfo customTable = DataClassInfoProvider.GetDataClassInfo(customTableClassName); if (customTable != null) { // Creates a new custom table item CustomTableItem newCustomTableItem = CustomTableItem.New(customTableClassName);

// Sets the values for the fields of the custom table (ItemText in this case)
newCustomTableItem.SetValue("ItemText", "New text");

// Save the new custom table record into the database
newCustomTableItem.Insert();

}

0 votesVote for this answer Mark as a Correct answer

lawrence whittemore answered on December 21, 2022 15:26

That was so simple I feel stupid.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on December 21, 2022 17:18

Na, it's not documented well. What Kentico does in the API is if you use the OOTB methods, it assigns the missing property values to the object so you can use them immediately. It's not just for custom table objects, but pages, custom module classes, etc. Pretty slick.

0 votesVote for this answer Mark as a Correct answer

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