CustomTableItem.New(string className, CustomTableItemProvider provider) from outside CMS

Simon Lukell asked on July 22, 2014 09:09

Hi,

I have a Core project, which contains a static class 'CustomTableHelper' that inserts/gets records from a Custom Table called 'Feedback'. The class responsible for the insert/get is the following:

using CMS.CMSHelper;
using CMS.SettingsProvider;
using CMS.SiteProvider;
using System;
using System.Data;

public static class CustomTableHelper
{
    public static DataSet GetData(TableType Table, string where, string orderBy)
    {
            string tableClassName = GetTableClassName(Table);

            DataClassInfo customTableClassInfo = DataClassInfoProvider.GetDataClass(tableClassName);

            if (customTableClassInfo == null)
                throw new Exception("Given custom table does not exist.");

            CustomTableItemProvider ctiProvider = new CustomTableItemProvider();

            DataSet dsItems = ctiProvider.GetItems(customTableClassInfo.ClassName, where, orderBy);

            return dsItems;
        }

    public static void CreateFeedbackHistoryItem(Feedback feedback)
    {
            CustomTableItemProvider customTableProvider = new CustomTableItemProvider(CMSContext.CurrentUser);

            string tableClassName = GetTableClassName(TableType.FeedbackHistory);

            DataClassInfo customTableClassInfo = DataClassInfoProvider.GetDataClass(tableClassName);

            if (customTableClassInfo == null)
                throw new Exception("Given custom table does not exist.");

            CustomTableItemProvider ctiProvider = new CustomTableItemProvider();

            CustomTableItem item = CustomTableItem.New(tableClassName, ctiProvider);
            item.SetValue("Name", feedback.Name);
            item.SetValue("Email", feedback.Email);
            item.SetValue("Phone", feedback.Contact);
            item.SetValue("Message", feedback.Message);
            item.Insert();
        }
}

I also have 2 sites, Kentico CMS Site (clean from any other code but Kentico), and my public Site (which consumes documents from CMS Kentico).

The insert/get works fine whenever I call the methods in Core from inside CMS Kentico Site.

However if I try to insert a new record on my Custom Table using 'CreateFeedbackHistoryItem' method from my public site (which also uses Core project), it throws and error on the item.Insert() line (it recognize the table though). (Note: If I run the 'GetData' method from my public site works fine and retrieves the rows).

It seems the problem is the Insert() method requiring some data internally.

Could you please provide a solution for inserting records in a Custom Table using the API from outside Kentico CMS?

Recent Answers


Brenden Kehren answered on July 22, 2014 13:21

In your project calling the Kentico code you will need to reference several Kentico CMS dlls. OR you can simply make a reference in your external project to the Kentico website and you should have access to those namespaces you're looking for.

0 votesVote for this answer Mark as a Correct answer

Yehuda Lando answered on July 23, 2014 10:16

What error are you getting?

0 votesVote for this answer Mark as a Correct answer

Simon Lukell answered on July 31, 2014 06:33

I'm still working on this. I added all the .dll but it's still not working.

The exception is 'Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack', but it's not representative since means that unmanaged code is throwing an exception, and the .NET debugger can't show you the usual useful details. I'm working with ServiceStack on my public site.

On the back the exception also says 'ThreadAbortException', but probably it has to do with what I mentioned above.

Hope you can help me!

0 votesVote for this answer Mark as a Correct answer

Simon Lukell answered on September 3, 2014 07:45

This issue is fixed now. The cause was that the CurrentSiteID was not initialized in the Context. So as soon as I create the Context, in the next line I set the CurrentSiteID:

CMSContext.Init(); CMSContext.CurrentSiteID = 1;

So I have 2 questions: 1 - Is there an elegant way to do it? Something like this?: //Not allowed Site is a readonly property CMSContext.CurrentSite.Site = SiteInfoProvider.GetSiteInfo("MyNewSite");

2 - What was the main cause The anonymous user or the Context being created on my external website (not being created by CMS)?

0 votesVote for this answer Mark as a Correct answer

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