1

I have stepped through and debugged my code and the id that is being passed to the GetItem() method is the ID of the training plan I am looking to get and the custom table class name is correct (It is getting the correct custom table. However the returned item is the first item in the custom table (I've looked in the database) not the item with the ID that was entered.

int id = parameter.ID;

CustomTableItemProvider provider = new CustomTableItemProvider(CMSContext.CurrentUser);

entity = provider.GetItem(id, TrainingPlanConstants.TrainingPlanTableName);

This unrelated custom table item is returned no matter what integer value is input. I have done a work around by using the GetItems() method - but this is not ideal as I know which CustomTableItem I want and this workaround is an unnecessary loop.

(Version 7)

6
  • Can you check how does the query look like when you call GetItem()? devnet.kentico.com/docs/7_0/devguide/… What happens when you call the overload with Guid?
    – rocky
    Oct 15, 2015 at 21:13
  • @Mutton92 Honestly dude, save yourself some time and trouble and just setup LINQ to SQL or EF classes for your CRUD operations in Kentico, especially for custom tables. It's way more flexible than Kentico's API, they're super easy to setup, and you can make your own queries instead of trying to figure out what's going on in a black box.
    – Jerreck
    Oct 16, 2015 at 0:03
  • @Jerreck I have a dll for all of the operations for getting the items from custom tables (that is what I am updating). It was made by my line manager years ago and updated for new versions of Kentico. Apparently he has known about the issue with the GetItem() function for a while and has just been using the GetItems() method in the DLL i just thought I would fix our DLL so it actually functions because another developer has used it throughout a site not knowing it doesnt function correctly! I have fixed it with LINQ in the DLL but Kentico should know that the method isn't working as expected!
    – Mutton92
    Oct 16, 2015 at 8:25
  • So if this was a known issue by you or your co-worker then why not specify that in your original post or even better yet, contact Kentico directly with this? Did you do any research in the hot fixes to see if there was a fix put in place? Because there were several bugs for Custom Tables in v7. Oct 16, 2015 at 11:59
  • I only knew the internal method written by us didn't work and I just found out that its the Kentico method that is causing it to not work. I just asked Kentico the exact same question and they pointed me to this thread. I did research the hotfixes and there is no mention of this issue. Also Kentico Q&A page tells you to ask questions on stack overflow - " Both Stack Overflow and the Devnet's Q&As are intended for Kentico community."
    – Mutton92
    Oct 16, 2015 at 13:15

3 Answers 3

1

Strange because what you do seems correct, maybe the ID is not the ItemID in your custom table. You can debug it by looking into the database and run an query with this ID or check with the method GetItems() to see what the ItemID is.

Here is some more examples for Kentico 7 : http://devnet.kentico.com/docs/7_0/devguide/index.html?custom_tables_api_examples_managing_custom_table_data.htm

The workaround can be the GetItems methods as also is done in one the examples in the documentations. And if you use this parameters it is not so heavy for the performance :

string where = "YourIDColumn = " + id;
int topN = 1;
string columns = "ItemID";

DataSet dataSet = customTableProvider.GetItems(customTableClassName, where, null, topN, columns);

But I agree that it seems strange if you have the GetItem method and you have the ID.

Good luck with debugging and finding your solution.

1
  • All of the examples that are on the link you sent either use the GetItem() method or don't get the CustomTableItem type which is what is returned by method i am trying to replace. To avoid making changes all over the place I am trying to get the CustomTableItem but I can't seem to manage to convert from DataTable / DataRow to CustomTableItem. Anyone have any ideas?
    – Mutton92
    Oct 15, 2015 at 14:15
1

Your code is correct.

Two things to verify (assuming your ID is a valid ItemID in the custom table you are looking in):

  1. Ensure the CMSContext.CurrentUser has permissions to read the custom table being queried.
  2. Ensure you are passing in the correct custom table code name. Seems trivial but verify it again.
3
  • I have checked in debug mode and the ID that is being used in the method is the ItemID of the custom table item I am trying to recieve
    – Mutton92
    Oct 15, 2015 at 12:31
  • What about the two other items I suggested to verify? What is the code name you're passing to the CustomTableItemProvider and does the user have access to that custom table? Oct 15, 2015 at 15:04
  • The custom table being referenced is correct. The item it is giving me is another member of the same table just not the correct one. It isn't returning null so the table class name must be correct it is also a constant that is being used in a lot of other places that are functioning fine.
    – Mutton92
    Oct 15, 2015 at 15:43
0

Try this:

int id = parameter.ID;
string where = "YourIdColumn=" + id;
string columns = "TheColumnYouWant"

CustomTableItem provider CustomTableItemProvider.GetItems(customTableClassName,where,null,columns);

var theTrainingPlan = provider["TheColumnNameOfTheValueWant"];

When you create a CustomTableItem it basically becomes a property map where you can access values directly by the key (or column) name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.