Xpage Copy Document

I have an xpage that contains some buttons (expand/collapse/delete/etc) and a view control.

I would like to do several things:

(listed in order of priority)

  1. I would like to add a button to the xpage that will create a copy of the document selected in the view.

Alternatively this button could go on the document itself.

  1. Set the field “approved” on the new document to be “NO”.

  2. Open the newly created document in edit mode.

Thanks in advance for any help.

Subject: How to copy document

I’m assuming you’re using a view panel. In which case, as this post on the Domino Designer wiki shows, you can use getSelectedIds() http://www-10.lotus.com/ldd/ddwiki.nsf/dx/2008-11-11033022WEBBZ4.htm.

That will give you an array to process. You’ll want to ensure they only select one entry.

Alternatively, you can set the var property for the view panel/repeat control/data table (that will give you a handle on the document in the row you’re dealing with), add a button to a column, and use varName.getDocument().getUniversalID() to get the relevant ID.

Use server side javascript to get the back-end document, create a copy, modify the field value and save.

You can then do context.redirectToPage(url). Construct your url with pageName.xsp?action=editDocument?documentId=UNID. You’ve just saved the new document, so you can easily get its’ unid. Make sure the XPage you’re using isn’t set to ignore request parameters, otherwise the parameters to force edit mode and define the document ID will be ignored.

Subject: Xpage Copy Document

the server side functions of actually copying the document are what I am most not sure about how to do.

I already have this piece where I can set the field published to now on the selected document, but I want to copy the document first.

var viewPanel=getComponent(“viewPanel1”); //get the componet of viewPanel

var docIDArray=viewPanel.getSelectedIds(); //get the array of document ids

for(i=0;i < docIDArray.length;i++){

var docId=docIDArray[i];

var doc=database.getDocumentByID(docId);

doc.replaceItemValue(“published”,“No”);

doc.save(true);

}

Subject: Copy doc

For SSJS you have all the same functions as LotusScript (except UI script like NotesUIWorkspace, NotesUIView etc), if you’re familiar with LotusScript. If not, the good news is it’s very well documented in the Help files.

Adding “:” after the variable declaration with a variable type defines it as that variable type, so

var doc:NotesDocument=database.getDocumentByID(docId)

will define doc as a NotesDocument, allowing you to get all the methods and properties through content assist(so when you enter “.” or press Ctrl+Space)

So all you need to change is

var doc=database.getDocumentByID(docId);

var newDoc:NotesDocument=doc.CopyToDatabase(doc)

newDoc.replaceItemValue(“published”,“No”);

newDoc.save(true);

Then to open the document, this should work:

context.redirectToPage(myPage+“?action=editDocument?documentId=”+newDoc.UniversalID

Subject: Resolved

Code as follows:

var viewPanel=getComponent(“viewPanel1”); //get the componet of viewPanel

var docIDArray=viewPanel.getSelectedIds(); //get the array of document ids

for(i=0;i < docIDArray.length;i++){

var docId=docIDArray[i];

var doc=database.getDocumentByID(docId);

var db=session.getCurrentDatabase();

var newDoc:NotesDocument=doc.copyToDatabase(db);

newDoc.replaceItemValue(“approved”,“No”);

var id=newDoc.getUniversalID();

newDoc.save(true);

}

context.redirectToPage(“content.xsp?documentId=”+id+“&action=editDocument”)

Subject: copyToDatabase

I keep getting an error on the copyToDoc portion of the code… this is what I have. I have not included the redirect portion yet.

var viewPanel=getComponent(“viewPanel1”); //get the componet of viewPanel

var docIDArray=viewPanel.getSelectedIds(); //get the array of document ids

for(i=0;i < docIDArray.length;i++){

var docId=docIDArray[i];

var doc=database.getDocumentByID(docId);

var newDoc:NotesDocument=doc.copyToDatabase(doc);

newDoc.replaceItemValue(“published”,“No”);

newDoc.save(true);}

Thanks again for all the help!!