Can I inject a JS Object on the server-side?

Yang Wen asked on June 9, 2016 20:06

I'm developing a single page app hosted by Kentico. It persists data to a custom table.

I would like to inject a JSON representation of the a custom table into the page markup, to alleviate the need for additional XHR calls to get the custom table data.

I've done this in the past by writing repeater transformations to wrap column values within a JS array like ""

Is there a server-side api that will automatically output custom table into JSON format?

Correct Answer

Dawid Jachnik answered on June 10, 2016 08:29

Hello,

You can easly achive this with Newstonsoft

    Response.ContentType = "application/json";
    Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(customTableItemsDS, Newtonsoft.Json.Formatting.None));
0 votesVote for this answer Unmark Correct answer

Recent Answers


Joshua Adams answered on June 9, 2016 21:01

I believe that the ScriptHelper class should do what you are asking. Maybe ScriptHelper.RegisterClientScriptBlock or something along those lines. Then inside of that, build your string of the json data.

0 votesVote for this answer Mark as a Correct answer

Yang Wen answered on July 19, 2016 22:01

It looks like you are advocating the use of a custom table data source web part to output the object? In that scenario, where would you define the c# code? I'm currently using a runat=server script tag in my page's ASCX layout to do the serialization of the in-line dummy object literal

What if the source object is a query result of a custom page type? I have bunch of objects of a custom page type where I would like to output a specific view of these pages as a JSON string.

I dont' believe there is a data source web part where I can setup the data query.

0 votesVote for this answer Mark as a Correct answer

Yang Wen answered on July 19, 2016 22:06 (last edited on July 19, 2016 22:16)

Doh! There is a Pages DS webpart

0 votesVote for this answer Mark as a Correct answer

Yang Wen answered on July 19, 2016 23:10

Where are you declaring the .net code? I have entered as inline code in my ascx layout, and dragged a Pages data source webpart onto the page. However I'm getting this error upon preview of page.

The name 'ResourceNavigatorDS' does not exist in the current context

I assume it has to do with Kentico's processing pipeline?

0 votesVote for this answer Mark as a Correct answer

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