Skip to main content link. Accesskey S
  • Help
  • HCL Logo
  • HCL Notes and Domino Application Development wiki
  • THIS WIKI IS READ-ONLY. Individual names altered for privacy purposes.
  • HCL Forums and Blogs
  • Home
  • Product Documentation
  • Community Articles
  • Learning Center
  • API Documentation
Search
Community Articles > Developing Applications > Developing XPage Applications > Developing XPage Applications for Notes Client > NotesDocumentCollection sample JavaScript code for XPages
  • Share Show Menu▼
  • Subscribe Show Menu▼

Recent articles by this author

NotesName sample JavaScript code for XPages

Here is sample JavaScript code for the class NotesName. Eventually this sample code will find its way into the documentation. Corrections and comments are appreciated.

NotesMIMEEntity sample JavaScript code for XPages

Here is sample JavaScript code for the class NotesMIMEEntity. Eventually this sample code will find its way into the documentation. Corrections and comments are appreciated.

NotesViewEntryCollection sample JavaScript code for XPages

Here is sample JavaScript code for the class NotesViewEntryCollection. Eventually this sample code will find its way into the documentation. Corrections and comments are appreciated.

NotesViewNavigator sample JavaScript code for XPages

Here is sample JavaScript code for the class NotesViewNavigator. Eventually this sample code will find its way into the documentation. Corrections and comments are appreciated.

NotesViewEntry sample JavaScript code for XPages

Here is sample JavaScript code for the class NotesViewEntry. Eventually this sample code will find its way into the documentation. Corrections and comments are appreciated.
Community articleNotesDocumentCollection sample JavaScript code for XPages
Added by ~Elizabeth Umkroskiettu | Edited by ~Elizabeth Umkroskiettu on April 15, 2011 | Version 2
  • Actions Show Menu▼
expanded Abstract
collapsed Abstract
No abstract provided.
Tags: Here is sample JavaScript code for the class NotesDocumentCollection. Eventually this sample code will find its way into the documentation. Corrections and comments are appreciated.
ShowTable of Contents
HideTable of Contents
    • 0.1 Count
    • 0.2 IsSorted
    • 0.3 Parent
    • 0.4 Query
    • 0.5 UntilTime
    • 0.6 addDocument
    • 0.7 cloneCollection
    • 0.8 contains
    • 0.9 deleteDocument
    • 0.10 FTSearch
    • 0.11 getDocument
    • 0.12 getFirstDocument getNextDocument
    • 0.13 getLastDocument getPrevDocument
    • 0.14 getNthDocument
    • 0.15 markAllRead
    • 0.16 markAllUnread
    • 0.17 putAllInFolder
    • 0.18 removeAll
    • 0.19 removeAllFromFolder
    • 0.20 stampAll

Count


This computed field displays the number of documents in the current database.

return database.getAllDocuments().getCount().toFixed()


IsSorted


A button processing a document collection assigns the NotesDocumentCollection object to a global variable. This computed field queries the global variable and displays whether the collection is sorted.

var dc:NotesDocumentCollection = requestScope.dc;
if (dc != null) {
	return dc.isSorted() ? "Sorted collection" : "Unsorted collection";
}


Parent


A button processing a document collection assigns the NotesDocumentCollection object to a global variable. This computed field queries the global variable and displays the title of the parent database.

var dc:NotesDocumentCollection = sessionScope.dc;
if (dc != null) {
	return dc.getParent().getTitle();
}


Query


A button processing a document collection assigns the NotesDocumentCollection object to a global variable. This computed field queries the global variable and displays its query if it is sorted.

var dc:NotesDocumentCollection = requestScope.dc;
if (dc != null) {
	return dc.isSorted() ? "Indexed on " + dc.getQuery() : "Unsorted collection";
}


UntilTime


This button gets all documents in the current database modified since the last time the button was clicked.

var profile:NotesDocument = database.getProfileDocument("UntilTimeProfile", null);
if (profile.hasItem("untilTime")) {
	var dt:NotesDateTime = profile.getItemValueDateTimeArray("untilTime")[0];
	var dc:NotesDocumentCollection = database.getModifiedDocuments(dt);
	requestScope.status = "Getting documents since " + dt.getLocalTime();
} else {
	var dc:NotesDocumentCollection = database.getModifiedDocuments(null);
	requestScope.status = "Getting documents since beginning";
}
profile.replaceItemValue("untilTime", dc.getUntilTime());
var doc:NotesDocument = dc.getFirstDocument();
while (doc != null) {
	requestScope.status += "\n" + doc.getItemValueString("subject");
	var tmpdoc = dc.getNextDocument();
	doc.recycle();
	doc = tmpdoc;
}


addDocument


This button merges two collections based on two searches.

var dc:NotesDocumentCollection = database.getAllDocuments();
var dc2:NotesDocumentCollection = dc.cloneCollection();
var query:string = requestScope.query;
var query2:string = requestScope.query2;
if (!query.isEmpty()) {
	query = "\"" + query + "\"";
	database.updateFTIndex(true);
	dc.FTSearch(query);
	if (!query2.isEmpty()) {
		query2 = "\"" + query2 + "\"";
		dc2.FTSearch(query2);
		var doc2:NotesDocument = dc2.getFirstDocument();
		while (doc2 != null) {
			// Add document from second query to first if not already there
			if (dc.getDocument(doc2) == null) {
				dc.addDocument(doc2);
			}
			var tmpdoc2 = dc2.getNextDocument();
			doc2.recycle();
			doc2 = tmpdoc2;
		}
	}
	requestScope.status = "Query results:";
	var doc:NotesDocument = dc.getFirstDocument();
	while (doc != null) {
		requestScope.status += "\n" + doc.getItemValueString("subject");
		var tmpdoc = dc.getNextDocument();
		doc.recycle();
		doc = tmpdoc;
	}
} else {
	requestScope.status = "No query";
}


cloneCollection


This button gets a collection of all documents in the current database and clones it. The first collection is then reduced to one document by a search. That document is used in the cloned collection to get the previous and next documents.

var dc:NotesDocumentCollection = database.getAllDocuments();
var clone:NotesDocumentCollection = dc.cloneCollection();
var query:string = requestScope.query;
if (!query.isEmpty()) {
	query = "\"" + query + "\"";
	database.updateFTIndex(true);
	dc.FTSearch(query, 1);
	var doc:NotesDocument = dc.getFirstDocument();
	if (doc != null) {
		requestScope.status = "";
		var doc2:NotesDocument = clone.getDocument(doc);
		var doc1:NotesDocument = clone.getPrevDocument();
		var doc3:NotesDocument = clone.getNextDocument();
		if (doc1 != null) {
			requestScope.status += 
				"\nPrevious: " + doc1.getItemValueString("subject");
		}
		if (doc2 != null) {
			requestScope.status += 
				"\n" + doc2.getItemValueString("subject");
		}
		if (doc3 != null) {
			requestScope.status += 
				"\nNext: " + doc3.getItemValueString("subject");
		}
	} else {
		requestScope.status = "No hit"
	}
} else {
	requestScope.status = "No query"
}


contains


This button determines if the current document is in the collection resulting from a search.

try {

var noteid:string = currentDocument.getDocument().getNoteID();
var dc:NotesDocumentCollection = database.getAllDocuments();
var query:string = requestScope.query;
if (!query.isEmpty()) {
	query = "\"" + query + "\"";
	database.updateFTIndex(true);
	dc.FTSearch(query);
	if (dc.contains(noteid)) {
		requestScope.status = "Current document contains query";
	} else {
		requestScope.status = "Current document does not contain query";
	}
} else {
	requestScope.status = "No query";
}

} catch(e) {
	requestScope.status = e.message;
}


deleteDocument


This button intersects two collections based on two searches.

var dc:NotesDocumentCollection = database.getAllDocuments();
var dc2:NotesDocumentCollection = dc.cloneCollection();
var query:string = requestScope.query;
var query2:string = requestScope.query2;
if (!query.isEmpty()) {
	query = "\"" + query + "\"";
	database.updateFTIndex(true);
	dc.FTSearch(query);
	if (!query2.isEmpty()) {
		query2 = "\"" + query2 + "\"";
		dc2.FTSearch(query2);
		var doc2:NotesDocument = dc2.getFirstDocument();
		while (doc2 != null) {
			// Delete document from second query if it is not in first
			var tmpdoc2 = dc2.getNextDocument();
			if (dc.getDocument(doc2) == null) {
				dc2.deleteDocument(doc2);
			}
			doc2.recycle();
			doc2 = tmpdoc2;
		}
	}
	requestScope.status = "Query results:";
	var doc:NotesDocument = dc2.getFirstDocument();
	while (doc != null) {
		requestScope.status += "\n" + doc.getItemValueString("subject");
		var tmpdoc = dc2.getNextDocument();
		doc.recycle();
		doc = tmpdoc;
	}
} else {
	requestScope.status = "No query";
}


FTSearch


This button gets all the documents that match a user-specified query.

var dc:NotesDocumentCollection = database.getAllDocuments();
var query:string = requestScope.query;
if (!query.isEmpty()) {
	query = "\"" + query + "\"";
	database.updateFTIndex(true);
	dc.FTSearch(query, 16);
	if (dc.getCount() > 0) {
		requestScope.status = dc.getCount().toFixed() + " hit(s):"
		var doc:NotesDocument = dc.getFirstDocument();
		while (doc != null) {
			requestScope.status += "\n" + doc.getItemValueString("subject");
			var tmpdoc = dc.getNextDocument();
			doc.recycle();
			doc = tmpdoc;
		}
	} else {
		requestScope.status = "No hits"
	}
} else {
	requestScope.status = "No query"
}


getDocument


This button gets a collection of all documents in the current database and clones it. The first collection is then reduced to one document by a search. That document is used in the cloned collection to get the previous and next documents.

var dc:NotesDocumentCollection = database.getAllDocuments();
var clone:NotesDocumentCollection = dc.cloneCollection();
var query:string = requestScope.query;
if (!query.isEmpty()) {
	query = "\"" + query + "\"";
	database.updateFTIndex(true);
	dc.FTSearch(query, 1);
	var doc:NotesDocument = dc.getFirstDocument();
	if (doc != null) {
		requestScope.status = "";
		var doc2:NotesDocument = clone.getDocument(doc);
		var doc1:NotesDocument = clone.getPrevDocument();
		var doc3:NotesDocument = clone.getNextDocument();
		if (doc1 != null) {
			requestScope.status += 
				"\nPrevious: " + doc1.getItemValueString("subject");
		}
		if (doc2 != null) {
			requestScope.status += 
				"\n" + doc2.getItemValueString("subject");
		}
		if (doc3 != null) {
			requestScope.status += 
				"\nNext: " + doc3.getItemValueString("subject");
		}
	} else {
		requestScope.status = "No hit"
	}
} else {
	requestScope.status = "No query"
}


getFirstDocument getNextDocument


This button gets all the documents in the current database.

var dc:NotesDocumentCollection = database.getAllDocuments();
var doc:NotesDocument = dc.getFirstDocument();
while (doc != null) {
	requestScope.status += "\n" + doc.getItemValueString("subject");
	var tmpdoc:NotesDocument = dc.getNextDocument();
	doc.recycle();
	doc = tmpdoc;
}


This button gets the second document in the current database.

var dc:NotesDocumentCollection = database.getAllDocuments();
if (dc.getCount() > 1) {
	var doc:NotesDocument = dc.getNextDocument(dc.getFirstDocument());
	requestScope.status = doc.getItemValueString("subject");
	doc.recycle();
}


getLastDocument getPrevDocument


This button gets all the documents in the current database in reverse order.

var dc:NotesDocumentCollection = database.getAllDocuments();
var doc:NotesDocument = dc.getLastDocument();
while (doc != null) {
	requestScope.status += "\n" + doc.getItemValueString("subject");
	var tmpdoc:NotesDocument = dc.getPrevDocument();
	doc.recycle();
	doc = tmpdoc;
}


This button gets the next to the last document in the current database.

var dc:NotesDocumentCollection = database.getAllDocuments();
if (dc.getCount() > 1) {
	var doc:NotesDocument = dc.getPrevDocument(dc.getLastDocument());
	requestScope.status = doc.getItemValueString("subject");
	doc.recycle();
}


getNthDocument


This button gets the next document in the current database modulo the total document count. A global variable specifies the current position.

var dc:NotesDocumentCollection = database.getAllDocuments();
var n:int = sessionScope.documentNumber;
if (dc.getCount() > 0) {
	if (n == null || n == dc.getCount()) {
		n = 1;
	} else {
		n++;
	}
	var doc:NotesDocument = dc.getNthDocument(n);
	requestScope.status = doc.getItemValueString("subject");
	doc.recycle();
	sessionScope.documentNumber = n;
}


markAllRead


This button marks as read documents that match a search query in the current database.

var dc:NotesDocumentCollection = database.getAllDocuments();
var query:string = requestScope.query;
if (!query.isEmpty()) {
	query = "\"" + query + "\"";
	database.updateFTIndex(true);
	dc.FTSearch(query);
	dc.markAllRead();
} else {
	requestScope.status = "No query";
}


markAllUnread


This button marks as unread documents that match a search query in the current database.

var dc:NotesDocumentCollection = database.getAllDocuments();
var query:string = requestScope.query;
if (!query.isEmpty()) {
	query = "\"" + query + "\"";
	database.updateFTIndex(true);
	dc.FTSearch(query);
	dc.markAllUnread();
} else {
	requestScope.status = "No query";
}


putAllInFolder


This button adds documents that match a search query to a folder in the current database.

var dc:NotesDocumentCollection = database.getAllDocuments();
var query:string = requestScope.query;
if (!query.isEmpty()) {
	query = "\"" + query + "\"";
	database.updateFTIndex(true);
	dc.FTSearch(query);
	dc.putAllInFolder("Search Results", true);
} else {
	requestScope.status = "No query";
}


removeAll


The following button creates a document collection, adds documents conditionally, then removes those documents.

var dc:NotesDocumentCollection = database.createDocumentCollection();
var view:NotesView = database.getView("main");
var doc:NotesDocument = view.getFirstDocument();
while (doc != null) {
	if (doc.getItemValueString("subject").endsWithIgnoreCase("obsolete")) {
		dc.addDocument(doc);
	}
	doc = view.getNextDocument(doc);
}
requestScope.status = "Number of documents removed: " + dc.getCount();
dc.removeAll(true);


removeAllFromFolder


This button removes documents that match a search query from a folder in the current database.

var dc:NotesDocumentCollection = database.getAllDocuments();
var query:string = requestScope.query;
if (!query.isEmpty()) {
	query = "\"" + query + "\"";
	database.updateFTIndex(true);
	dc.FTSearch(query);
	dc.removeAllFromFolder("Search Results");
} else {
	requestScope.status = "No query";
}


stampAll


This button replaces an item value in documents that match a search query in the current database.

var dc:NotesDocumentCollection = database.getAllDocuments();
var query:string = requestScope.query;
if (!query.isEmpty()) {
	query = "\"" + query + "\"";
	database.updateFTIndex(true);
	dc.FTSearch(query);
	dc.stampAll("quantity", 15);
} else {
	requestScope.status = "No query";
}

  • Actions Show Menu▼


expanded Attachments (0)
collapsed Attachments (0)
Edit the article to add or modify attachments.
expanded Versions (2)
collapsed Versions (2)
Version Comparison     
VersionDateChanged by              Summary of changes
This version (2)Apr 15, 2011, 3:31:08 PM~Elizabeth Umkroskiettu  
1Jan 2, 2011, 6:59:12 PM~Ben Eljipypulettu  
expanded Comments (0)
collapsed Comments (0)
Copy and paste this wiki markup to link to this article from another article in this wiki.
Go ElsewhereStay ConnectedAbout
  • HCL Software
  • HCL Digital Solutions community
  • HCL Software support
  • BlogsDigital Solutions blog
  • Community LinkHCL Software forums and blogs
  • About HCL
  • Privacy
  • Accessibility