A
A
Anton Starostin2014-10-28 15:17:37
SharePoint
Anton Starostin, 2014-10-28 15:17:37

How to initialize javascript object in ascx form sharepoint web part?

Hello.
There was a need to make a non-standard solution, namely for SharePoint 2013, you need to get data from the list and build graphics based on this data. I decided to build graphs using jQuery libraries, but for this, the data must first be somehow transferred to ascx from the backend.
The essence of the question is that in the backend code I can get the elements of the list, after that I must initialize the jQuery plugin with this data and then everything will work.
I found two possible solutions - the first is to use the REST data access model and receive data directly in ASCX without resorting to the backend.
The second way is to build a string with a script in the backend, then pass this string to the form before rendering.
Is it possible to somehow place the placeholder in ascx, so that later I can simply pass the line from the backend there?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anton Starostin, 2014-11-01
@Lixoradka

I finally found a solution to the problem.
My problem was that I did not understand the concept of working with web parts, and now it dawned on me that this is a regular asp.net and you need to work with it like with an asp.net site.
Thus, if you need, for example, to initialize an array of any elements in javascript on an ascx page, you can use a code like this:

<script>
var test = '<%= BackendVariable%>';
</script>

In this case, if this variable was initialized in the PageLoad method, it will be substituted in place with brackets.
In another case, if you need to draw non-standard markup, let's say in a cycle, you can use the following constructions:
<% foreach (var item in collection)
{ %>
    <p> <%= item %> </p>
<% } %>

Then, when building the page, special markup will be created according to your cycle.
And now the most interesting! Sharepoint 2013 has the ability to use the REST API to access lists. It turned out to be not as difficult as I thought. If you choose this approach, then you can practically not use .NET for processing requests, but simply use frameworks a la Angular/ExtJS/Knockout.
Perhaps there are minor errors in the code - I wrote it right here.

S
Silvery, 2014-10-30
@Silvery

Not sure what will work in 2013, but in Sharepoint 2010 this kind of thing is done via __doPostBack(). Try googling, it might help.

V
Viktor Buzin, 2015-09-19
@Buzzz

In the 2010 version, only such complex machinations were needed, now you can do everything in JavaScript, without Layouts pages, look towards the SharePoint API, how to get data from the list you can look here: blog.techbuzzz.me/2015/06/sharepoint-csr- callout.html
If there is a lot of data (~5000 elements) that needs to be aggregated on charts, then you can use the Search Service, in particular its API, because crawling search results are cached and you can take a large scope of data from the search.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question