XPages in Pictures - 07 - Session Scope Variables
Category XPages
This is the 7th post of the “XPages in Pictures” Series.
Now it’s time to address the fact that saving the contacts returns us to “xMain” instead of the company page that we were on when we created it.
In the last article, you saw that when you created a response document, you passed the current companies noteid field into the simple action as the Parent ID. If you could only store the noteid somewhere, you could then use it in the Save button of the contact page to return us to the correct company. But how do you store a variable that spans different XPages?
Scoped Variables
It turns out there’s a way to store this information in something called “Scoped Variables”. There are different types of scoped Variables. I’m going to talk about 2 that are of interest right now.
1- viewScope The variable lives for the duration of the page
2- sessionScope The variable lives for the duration of the user session.
Since you want to access a variable between pages lets focus on sessionScope. You’ll create a variable that can be accessed from anywhere. Here’s how:
Open the page “xCompany”. Go to the Events tab of the XPage and click on “afterPageLoad”.
This event will run after the page loads and will let you get the documents’ “universal id” from the backend document object and store that in a sessionScope variable. Once that happens it will be available from any page.
Rather then using a Simple Action, like you’ve done before, now you’re going to have to enter JavaScript that will run on the server. Select “Script Editor in the radio button. Now you can start to enter JavaScript in the box, or click on the “Open Script Editor” button, to bring up the editor.
Enter the following code:
//Get the document object in JavaScript
var doc:NotesDocument = companyDoc.getDocument();
//Create a variable with the Universal ID
var sID = doc.getUniversalID();
// Assign the variable sID to a Session Scope Variable called "companyCode"
sessionScope.put("companyCode",sID);
Your screen should look like this:
Now that you’re starting to enter JavaScript code, I should mention debugging. At the time of this writing, Notes DDE version 8.5, debugging tools and techniques seems to be lacking. My suggestion is to save often and test in the web browser often. (This is another way of saying that I haven’t figured out a great debugging approach yet.)
Testing…
You now have a variable that will be available for any XPage. Before putting this to a practical use, make sure you’re getting a good value. You can test this by using an Edit Box Control that’s bound not to a field on a Notes Document, but to the sessionScoped Variable itself.
From your Core Controls List, drag a Label to the page. Then drag and Edit Box next to it.
The resulting page should look like this:
Now click on the Label control and change the “Label” property to be “UNID:” Then click on the Edit box control and go to the Data tab. This is where you control the binding.
In the beginning when you created the xCompany and xContact Page, you dragged data fields to the XPage and a nice table of field names and controls was created and the binding was automatically done for you. Here is where you can control the binding of individual fields. Maybe you don’t want a table, or you want to replace an Edit Box Control with a Multiline Edit Box. You can choose between Simple, Javascript, and Advanced data binding.
In this instance, you want to bind to the scoped variable “companyCode”. This variable is created and updated with the page xCompany loads. To do this, you want to bind data using the “Advanced” option. Choose “Scoped Variable” next to “Use:” and select “Session Scope” and enter the variable name “companyCode”.
Note: This screen shot looks incorrect as I review this post. The variable name should be "companyCode". XPages is very case sensitive so be careful. I'll try and update the screenshot soon.
Finally, go back to the Edit box properties and bake the control “Read Only”. Save the page and launch the application in your browser. Click on any company. You should now see the companies’ UNID..
000000000000000000000000000000000000000000000000
Click on different companies. Notice how the UNID on the page will change. This variable is getting updated every time you open a company!
Pretty Cool huh?
This is the 7th post of the “XPages in Pictures” Series.
Now it’s time to address the fact that saving the contacts returns us to “xMain” instead of the company page that we were on when we created it.
In the last article, you saw that when you created a response document, you passed the current companies noteid field into the simple action as the Parent ID. If you could only store the noteid somewhere, you could then use it in the Save button of the contact page to return us to the correct company. But how do you store a variable that spans different XPages?
Scoped Variables
It turns out there’s a way to store this information in something called “Scoped Variables”. There are different types of scoped Variables. I’m going to talk about 2 that are of interest right now.
1- viewScope The variable lives for the duration of the page
2- sessionScope The variable lives for the duration of the user session.
Since you want to access a variable between pages lets focus on sessionScope. You’ll create a variable that can be accessed from anywhere. Here’s how:
Open the page “xCompany”. Go to the Events tab of the XPage and click on “afterPageLoad”.
This event will run after the page loads and will let you get the documents’ “universal id” from the backend document object and store that in a sessionScope variable. Once that happens it will be available from any page.
Rather then using a Simple Action, like you’ve done before, now you’re going to have to enter JavaScript that will run on the server. Select “Script Editor in the radio button. Now you can start to enter JavaScript in the box, or click on the “Open Script Editor” button, to bring up the editor.
Enter the following code:
//Get the document object in JavaScript
var doc:NotesDocument = companyDoc.getDocument();
//Create a variable with the Universal ID
var sID = doc.getUniversalID();
// Assign the variable sID to a Session Scope Variable called "companyCode"
sessionScope.put("companyCode",sID);
Your screen should look like this:
Now that you’re starting to enter JavaScript code, I should mention debugging. At the time of this writing, Notes DDE version 8.5, debugging tools and techniques seems to be lacking. My suggestion is to save often and test in the web browser often. (This is another way of saying that I haven’t figured out a great debugging approach yet.)
Testing…
You now have a variable that will be available for any XPage. Before putting this to a practical use, make sure you’re getting a good value. You can test this by using an Edit Box Control that’s bound not to a field on a Notes Document, but to the sessionScoped Variable itself.
From your Core Controls List, drag a Label to the page. Then drag and Edit Box next to it.
The resulting page should look like this:
Now click on the Label control and change the “Label” property to be “UNID:” Then click on the Edit box control and go to the Data tab. This is where you control the binding.
In the beginning when you created the xCompany and xContact Page, you dragged data fields to the XPage and a nice table of field names and controls was created and the binding was automatically done for you. Here is where you can control the binding of individual fields. Maybe you don’t want a table, or you want to replace an Edit Box Control with a Multiline Edit Box. You can choose between Simple, Javascript, and Advanced data binding.
In this instance, you want to bind to the scoped variable “companyCode”. This variable is created and updated with the page xCompany loads. To do this, you want to bind data using the “Advanced” option. Choose “Scoped Variable” next to “Use:” and select “Session Scope” and enter the variable name “companyCode”.
Note: This screen shot looks incorrect as I review this post. The variable name should be "companyCode". XPages is very case sensitive so be careful. I'll try and update the screenshot soon.
Finally, go back to the Edit box properties and bake the control “Read Only”. Save the page and launch the application in your browser. Click on any company. You should now see the companies’ UNID..
000000000000000000000000000000000000000000000000
Click on different companies. Notice how the UNID on the page will change. This variable is getting updated every time you open a company!
Pretty Cool huh?
- 


Comments
Posted by Stephan H. Wissel At 02:18:05 AM On 05/19/2009 | - Website - |
Thanks for the comments! These scoped variables seem really powerful!
Declan - That's an awesome tip you posted about having a custom control to display the values of the variables!!!
Posted by David Leedy At 08:17:50 PM On 05/19/2009 | - Website - |
{ Link }
Posted by Declan Lynch At 11:01:36 AM On 05/19/2009 | - Website - |
Anyone else had any success with anything other than strings?
Posted by Jerry Shelley At 11:35:12 AM On 06/01/2009 | - Website - |
From my understanding you definitely can not store a NotesDocument Object in a scoped variable. It would be too big and impossible because of the stateless nature of the web. So you want the UNID in the scoped variable and you can use that to retrieve the Notes Document Object when you need it.
Posted by David Leedy At 09:15:53 PM On 06/05/2009 | - Website - |
Posted by Edwin At 05:01:28 PM On 07/19/2009 | - Website - |
Posted by Patrick Kwinten At 09:51:25 AM On 06/01/2010 | - Website - |