Inserting row into custom module dynamically in code

Jessica Knaak asked on August 15, 2018 07:16

I built a custom module that handles some data I am retrieving from a 3rd party through an API. I am now building a custom task that will be run nightly through Kentico's scheduled tasks and am looking to rewrite all the data in the custom module. I'm not sure if I am just missing it in the documentation, but is there a way to insert data into a custom module through c# code?

Correct Answer

Dragoljub Ilic answered on August 15, 2018 17:45

Hi Jessica,

If I understand you correctly, you can accomplish that with code above. Here are some example:

var customObject = new NameOfTheObjectInfo(){
    param1 = "",
    param2 = ""
    //...
};

When you map all fields from API to your custom module class object, just call info provider: NameOfTheObjectInfoProvider.SetNameOfTheObjectInfo(customObject)

This will add new row for object into custom module table. Hope this will help.

Best regards, Dragoljub

2 votesVote for this answer Unmark Correct answer

Recent Answers


Arun Kumar answered on August 15, 2018 07:51 (last edited on August 15, 2018 07:51)

Hi,

Check this post to insert new record in your custom table which is generated from your custom module classes. You can get the class info through Kentico API and then insert new record easily in C# code.

0 votesVote for this answer Mark as a Correct answer

Dragoljub Ilic answered on August 15, 2018 14:13

Hi Jessica,

If you created a custom class into your custom module, you can easily generate ObjectInfo and ObjectInfoProvider for that class which will allow you to manipulate with data from C# code.

Here are some steps how to generate classes for kentico:

  1. Go to custom module
  2. Select 'Classes' from tab on the left
  3. Click to edit specific class that you want
  4. Go on 'Code' page from tab on the left
  5. There you can customize your code and chose where to save it

If you save your code outside the CMS project (another assembly), you may use this attribute for newly generated clases: [assembly: AssemblyDiscoverable]

When you do all this, you can get/update objects with:

  • NameOfTheObjectInfoProvider.GetNameOfTheObjectInfo(id)
  • NameOfTheObjectInfoProvider.GetNameOfTheObjects() - to retrive all
  • NameOfTheObjectInfoProvider.SetNameOfTheObjectInfo(infoObj)
  • NameOfTheObjectProvider.DeleteNameOfTheObjectInfo(infoObj)
  • ...

Best regards, Dragoljub

0 votesVote for this answer Mark as a Correct answer

Jessica Knaak answered on August 15, 2018 17:20

Thank you Dragoljub Ilic,

This is how I built things and do have the code saved and have customized it. I just couldn't find anywhere how to add/insert a row into the data. Do you have any insights on that particular area?

Thanks, Jessica

0 votesVote for this answer Mark as a Correct answer

Jessica Knaak answered on August 15, 2018 18:09

Dragoljub,

I think that is exactly what I'm looking for. I didn't think about Set. I will let you know if it does what I need.

Thank you, Jessica

1 votesVote for this answer Mark as a Correct answer

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