Does SharePoint support session variables that can be accessed by the client side? Using client side jQuery or JS or even placing code on an ASPX acsset attached to a webpart to set variable on the page in a web part that can be read by the jQ, JS, etc.
1 Answer
Define JavaScript variable like this:
<script type="text/javascript">
var myVariable = '<%= this.Session["VariableName"] %>';
</script>
In this example your variable will have string type. If it is e.g. integer, you don't need to use quotes ''. The idea is the same as described in this article: Pass locale of current web site (SPWeb
) in SharePoint
into JavaScript.
Note that you can't use this technique in separate .js
files. You can only use it if JavaScript is embedded to the page as shown above. However it is not big limitation. You may define variable as shown above and pass it to the function which is defined in separate .js
file.
You can also look at this post.
- You may use
HiddenField
control to "send" value from server to client, where you can read it from JavaScript
-
Beware that by default Session variables (as in ASP.NET Session) are stored in memory so you will have separate variable instances on each SharePoint server. You need to separately configure it in web.config in order to persist Session variables in, e.g., DB to have farm wide session variables. May 31, 2016 at 12:06
-
-
If you are storing critical and sensitive data in session, do not use this method and it will be visible if you use developer tools in browser.– FLICKERJul 26, 2018 at 17:18
-