Pages

Showing posts with label Server Side Javascript. Show all posts
Showing posts with label Server Side Javascript. Show all posts

Thursday, November 28, 2013

Roles in XPages - Part ||

I have tried obtaining user roles using different methods before. I think the following way is pretty simple to obtian an array or user roles and a comma separated string of user roles

var userRolesArray=database.getACL().getRoles().toArray()

var userRolesString="";

for (itr=0;itr
{ userRolesString+=userRolesArray[itr]+",";
}

userRolesString;

My previous post on this topic can be navigated to by clicking on the following link

Thursday, November 7, 2013

Get label of link in XPages

In order to get value from a component like Edit box control etc in an XPage I use

getComponent("ControlName").getValue()

When it comes to a link it will give you the URL Value that the XPage link control has been mapped. In order to obtain the Label or the Name of the link (and not url) you got to use the getText() method.

Following is a brief illustration of what I am talking about


Following is the result obtained on the browser


Get class name of Components in XPage

Its very easy to obtain the class name of components in Xpages. The only issue is that, its difficult if you dont know how :P

Well for starters (like me) drag and drop a control on to an XPage. Give it a name say "MyControl"

Now add a label to the XPage where you performed the above mentioned difficult steps and use the following code to compute its label

getComponent("MyControl").getClass().getName()

In my case I tried it with a link object and got the following stuffs on my screen




If you feel curious about understanding (most likely about simply knowing) controls better you can refer to the control documentation. Following post speaks about its where abouts.

http://xcellerant.net/2013/09/17/tip-finding-all-properties-and-methods-available-for-xpages-components/

Thursday, October 31, 2013

JSON Issues in XPages - Unknown literal of class com.ibm.jscript.types.FBSUndefined

This post does not speak about any fixes for this error message, however, contains a tip that will help avoid the issue.

I encountered this error when I tried to apply toJson method on a json construct.

I had the following json construct. Let me say this as "x"


{"menuItems": [
{
"serialNumber": 1,
"Label": "Label_1",
"Link":"Link_1",
"subMenuItems": [
{
"serialNumber": 1,
"Label": "Label_1",
"Link":"Link_1",
"subMenuItems": [
,
]
},

]
},
]}

When i attempted some thing like toJson(x), I got the FBSUndefined error. How ever, when I attempted, fromJson(x) I got sort of I got the following error

Error when parsing JSON string
Encountered " "object "" at line 1, column 2. Was expecting one of: "false" ... "null" ... "true" ...blah blah blah

When I just put my json construct on to a field's default value and tried to parse the field's value using the following code
x=getComponent("toJsonTest").getValue();

y=fromJson(x);

getComponent("result").setValue(y.menuItems.length)//-> result was 2.0 (i guess)
it worked like charm. Voila

I understand that this must be sort of, a beginner level stuff for Json in XPages.
If you would like to be saddened further by coding a lot to obtain perfection, may be you can explore "com.ibm.commons.util.io.json" and use them in your SSJS :P

Issues working with XPage Tabbed Panel Controls - Aligning Tab Headers with Margin

This was something that I was thinking to do for a long time but never had to do it before. Thanks to my most demanding customer who literally takes a scale and measures micrometer differences on the screen, I am now finding time for working with this finer details on the screen.

The goal is to get the tab headers of a Tabbed Control in XPages to align itself with the margin. I know we always play around with options available in the control's property tab to do it. Atleast I always do it that way :P. I did play around with margins and found some frustrating issues that resulted in some sort of "so-close" answers all the time.

Thanks to chrome and firefox and their developer modes, I was able to do something that pleases me.

Jumping straight to the point, my XPage Tabbed Control looked some what like one in the following screen shot



Hmm looking at this part of screen alone make me realize why my customer was like "WHAT THE HELL!!! WHY IS THE PASSENGER DATA OUT OF LINE!!!". That was scary :P

At last (so at least now) I was able to modify that to look like the following

  Looks pretty nice tight. My customer is really something. Wonder about the eagle eyes my customer has got :)

The trick that helped me do this is very simple. Just add the following to your style sheet

ul.xspTabbedPanelTabs {
left: 0px;
}

and by the way if you are interested, its 10px by default. Thank you wasting some of your time reading my story and not scolding me :)

Monday, May 27, 2013

Trigger Select All Checkbox Click in XPage view panels in XPages


This post shows the method through which the select all button that you would enable on a Xpage view panel can be programatically clicked.

I found need for a scenario in which I would require the document ids of all documents that are being displayed on a Xpage view in a browser. Eventually the simplest userfrinedly/user interactive mechanism that I was able to think about was the native select all feature available with the XPage view controls in Lotus Notes.

It took me quite a while to understand the usage though, It was quite tricky and tough but not any more :)
Following is the code fragment that allowed me to do this silly stuff

var viewPanelContainer=document.getElementById("ViewPanelContainer");

//-- Following line of code has been commented and the forth comming lines, second and third respectively
//have been introduced to bypass a IE bug related to the HTMLNode.getElementsByClassName command in javascript
//var selectAllCheckBox=viewPanelContainer.getElementsByClassName("xspCheckBoxViewColumnHeader")[0];
var selectAllCheckBoxes=getElementsByClassName(viewPanelContainer, "xspCheckBoxViewColumnHeader");
var selectAllCheckBox=selectAllCheckBoxes[0];
//-- End of block highlighting the work around for an IE bug


selectAllCheckBox.click(); //dispatchEvent("click");

Here I am using a function named as getElementsByClassName. The function is available in the blog, you can simply search for the same using the name itself or use any other method that you have. This is essential for the code to work in the tremendously awesome Internet explorer crap.

For all other poor browsers out there, they are in capable of making my life difficult and hence a simple object.getElementsByClassName will work on them

Hope this helps :)

Wednesday, March 23, 2011

Crash a Domino Server - an other SSJS did it

In addtion to what I have found previously - http://ozinisle.blogspot.com/2011/03/crash-domino-server-oops-ssjs-did-it.html, I happened to stumble upon an other code fragment that did it. I mean crashing my domino server.

This time its a more serious one to be aware of. Because the following line of code works perfectly until it breaks. So be care full.

viewHandle.getColumnValues(colNumber)

where, viewHandle is a NotesView object.

Hope this helps :)

Wednesday, March 16, 2011

@DbColumn - An other way in SSJS

In addition to my recent adventure on crashing a domino server, the following work around helped me fix the issue and it seems to be an elagant way to get the column values from a view column

var viewHandle:NotesView=database.getView(viewName)
return viewHandle.getColumnValues(colNumber-1)

Crash a Domino Server - oops SSJS did it

In an attempt to simulate @DbColumn using SSJS, i used a few lines of code which included the following,

doc.getColumnValues();


To my astonishment, I found that my domino server had crashed. Yes the server it self had crashed every time it read this code fragment.

I have coded a few lines in the past that crashed Lotus notes client/designer, but never had the opportunity to crash a server. And now it took me XPages to do it :)

Looking for a work around :(

Wednesday, March 9, 2011

Triggering Client script from server side : Xpages

Triggering client script from server side on XPages saves developers in many scenarios.
I missed them so badly until I came to know about the "xp:onComplete" tag.

Associate this property to the event handler of any of the controls with partial refresh enabled and voila you are ready to go.

The following is a simple example on getting the property to work in xpages.


Ensure that the control does not have full update enabled cos, this method would work only when partial refresh is enabled

Wednesday, February 9, 2011

agent.runWithDocumentContext(NotesDocument) not working in XPages

If you attempt to execute a Lotus Script agent from the server side script in the XPages and get the following error,

There are few issues that you may have to look into,

1. Sign the agent, XPage and the associated elements. Better sign the database with the server id.
2. Ensure that you have sufficient access to execute agents
3. Ensure the "Run as Web User" and "Allow Restricted Operation" are selected on the agent
4. Ensure that the type of the agent is "Agent List Selection" with target "None"

Hope this helps :)

Tuesday, January 11, 2011

Triggering lotus script agents and obtaining their response on the XPage using Server side javascript - An XMLHTTPRequest equivalent

Triggering agents from server side javascript in XPages is easy. How ever obtaining the response from the agents once the lotus script agent completes execution and displaying the same on the xpage was always a challenge to me. The following is a way helped me achieve it,

/***************************************************/
importPackage(java.net);
importPackage(java.io);

//create a url object by passing the lotusscript agent's url and ensure that you have some print statements in your agent
var agent:URL = new URL("Agent URL");
//create a connection with the agent's url provided
var agentConnection:URLConnection = agent.openConnection();
//open a buffered reader that gets the stream of response from the agent connection        
var inputReader:BufferedReader = new BufferedReader(new InputStreamReader(agentConnection.getInputStream()));
//read the output line by line and store them in a string
var res:String="";
while ((inputLine = inputReader.readLine()) != null) {
     res+=inputLine;

//update the result to a output field on the XPage to view the same visually
getComponent("output").setValue(res);

/***************************************************/


This attempt was successful and I was able to recieve responses on the XPage but is a Synchronous way. Hence you may have to wait until the agent gets executed and the response is obtained.

Am yet to find the same to do in a Asynchronous way.

Hope this helps :)

Importing Java Packages in server-side-script in XPages

In an attempt to use methods in java directly in XPages, I used to try the traditional import methods which would say,

import java.io.*;

 to import the IO.* package in Java and that will always return an error. I was wondering how to do these stuffs untill I stumbled upon a http://www.stevecastledine.com/sc.nsf/dx/using-standard-java-packages-in-your-xpage-code"> post in Steve Castledine's Blog

The proper way to do that in XPages is as follows,

importPackage(java.io)


so in general I assume that to be importPackage(java package name without .*)


Hope this helps :)

Monday, January 10, 2011

getWebDBName in XPages (@WebDBName in XPages)

The following is a function that helps u obtain the web db name of a database in xpages (@WebDBName)


function getWebDBName() {
var arg=@Trim(arguments[0]);
var webDBName="";
if (typeof arg!="undefined"){
webDBName=@ReplaceSubstring(arg,"\\","/");
return webDBName;
}
var dbPath=@DbName()[1];
webDBName=@ReplaceSubstring(dbPath,"\\","/");
return webDBName;
}


The following are the ways in which u can use the above function

getWebDBName("a\\b/c\\d.nsf");      //will return a/b/c/d.nsf
getWebDBName(database);             //will return @WebDBName;
getWebDBName();                          //will return @WebDBName;
getWebDBName(db:NotesDatabase);  // will return @WebDBName of the db object being passed

Hope this helps :)

Wednesday, January 5, 2011

Get Current URL in XPage

In traditional Web Application it is kind of simple to get the current url using location.href.

In case of XPages the following is the method to get the current url in XPages

context.getUrl().toString();

Hope this helps :)