Xpages:getDocumentByKey in viewentry


Am doing some Search by key that works fine , now am looking to export to excel , found this code (Nathan code, thanks) and my attempt to make it work with Key is not working

I have added getFirst after getDocumentByKey but no luck , am getting a blank screen , anyone see whats wrong with this code ?

<!–?xml version=”1.0″ encoding=”UTF-8″?>
<xp:view xmlns:xp=”http://www.ibm.com/xsp/core&#8221; rendered=”false”>
<xp:this.afterRenderResponse>
var writer = facesContext.getResponseWriter();
var response = exCon.getResponse();
var viewNav:NotesViewNavigator = database.getView(‘TreeContent’).createViewNav();
var searchKey = context.getUrlParameter(“searchVariable”);
var viewEnt:NotesViewEntry = viewNav.getDocumentByKey(SearchKey,true);
var output:string = “”;
while (viewEnt != null) {
output += “<tr>”;
for (var x in viewEnt.getColumnValues()) {
output += “<td>” + x + “</td>”
}
output += “</tr>”;
viewEnt = viewNav.getNext(viewEnt);
}
facesContext.getExternalContext().getResponse().setContentType(“application/vnd.ms-excel”);
facesContext.getExternalContext().getResponse().setHeader(“Content-disposition”, “attachment; filename=projects.xls”);
facesContext.getExternalContext().getResponse().setHeader(“Cache-Control”, “no-cache”);
facesContext.getResponseWriter().write(“<table><thead><tr><td><b>Project</b></td><td><b>Estimate ( Hours )</b></td><td><b>Developer</b></td><td><b>Estimate ( Budget )</b></td></tr></thead>”+output+”</table>”);
facesContext.getResponseWriter().endDocument();}]]>
<!–xp:view>

Advertisements

9 thoughts on “Xpages:getDocumentByKey in viewentry

  1. I’m guessing I’m the Nathan you’re referring to. But I must admit that I don’t recall writing or posting this code anywhere. If it was me, then I must have been drunk or something, because .getDocumentByKey() isn’t a method of ViewNavigator, and thus the code as written would never ever work. In fact, it should be throwing a stack trace.

    It’s not entirely clear what the intent is from the code. One would normally get a ViewEntryCollection from the search key, and then iterate over that. Since this code creates a ViewNavigator, though, it might have been intended to create it using the search key as a category.

  2. Well true you are the nathan, False you wrote whole code , i did add to it and from the looks of it i maybe the one that have been drinking 😦 I will post my finding when done .

  3. Ah I see. Hope I didn’t insult you! 🙂

    You should just be able to do a .getallentriesbykey to get what you want.

  4. Nhaaa takes more then that 🙂 – That is what i don´t understand
    I have tried this : var viewEnt:NotesViewEntry = viewNav.GetAllDocumentsByKey(SearchKey,true);
    var viewEnt:notesViewEntry = viewNav.getFirstDocument();

    but no dice , i will send you an email with link to this page so you can see this in action

  5. Could it be as simple as case sensitivity? You have
    var searchKey…. but then
    viewNav.getDocumentByKey(SearchKey,true);

    which makes the getDocumentByKey look for a different (probably null) value than that in searchKey.

  6. Mick, Maybe but would this
    var searchKey = context.getUrlParameter(“searchVariable”);
    give me the Value “U27Q274hGm” of the searchVariable ? Like

    .xsp?searchVariable=U27Q274hGm Your Thoughts !

  7. Palmi,

    I’ve modified my example from XPagesCheatSheet.com to filter by category for state. Now I’ve hardcoded the state in, but it should be easy enough to get the filterkey from a URL or scoped var.

    Is this what you’re looking for?

    // Here are the Database and View Names
    var dbName = “FakeNames.nsf”;
    var viewName = “byNameSmall”;
    var filter = “PA”;

    // I assume that the database we’re looking up is in the SAME PATH as the current db
    var path = database.getFilePath().split(database.getFileName())[0];
    // print(path);

    var fullDB = path + dbName
    var lookupDB:NotesDatabase = session.getDatabase(database.getServer() , fullDB);
    var myView:NotesView = lookupDB.getView(viewName);

    var exCon = facesContext.getExternalContext();
    var writer = facesContext.getResponseWriter();
    var response = exCon.getResponse();
    var viewNav:NotesViewNavigator = myView.createViewNav();
    var viewEC:NotesViewEntryCollection = myView.getAllEntriesByKey(filter)
    var viewEnt:NotesViewEntry = viewEC.getFirstEntry()

    response.setContentType(“application/vnd.ms-excel”);
    response.setHeader(“Cache-Control”, “no-cache”);
    writer.write(“”);
    writer.write(“”);
    writer.write(“Column1Header“);
    writer.write(“Column2Header“);
    writer.write(“Column3Header“);
    writer.write(“”);

    while (viewEnt != null) {
    writer.write(“”);
    writer.write(“” + viewEnt.getColumnValues()[0] + “”);
    writer.write(“” + viewEnt.getColumnValues()[1] + “”);
    writer.write(“” + viewEnt.getColumnValues()[2] + “”);
    writer.write(“”);

    viewEnt = viewNav.getNext(viewEnt);
    }

    writer.write(“”);
    writer.endDocument()

  8. I would recommend to put the code into the beforeRenderResponse object instead of afterRenderResponse and add a “facesContext.responseComplete()” as demonstrated here:

    http://www.wissel.net/blog/d6plinks/SHWL-8248MT

    In addition, adding some error handling with try/catch and writing the error to the console using _dump would help.
    The parameter rendered=”false” causes the XPages web server to not output ANY content to give the developer full control.
    Unfortunately, this also includes the Xpages error page. That’s why you see the blank screen.

  9. Is there any way to programmatically get the column headers? Or do I have to manually add them to the code? That means I’d have to create an “export” xPage for every view the user wants to export.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s