Answer the question
In order to leave comments, you need to log in
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
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>
<% foreach (var item in collection)
{ %>
<p> <%= item %> </p>
<% } %>
Not sure what will work in 2013, but in Sharepoint 2010 this kind of thing is done via __doPostBack(). Try googling, it might help.
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 questionAsk a Question
731 491 924 answers to any question