Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Selecting a Random Document from a View

Selecting a Random Document from a View

Selecting a Random Document from a View

Hello! Hope this is in the best place:

I use the agent below to pull each document from the view "MainFeature" and then format certain fields in that document as HTML using the "document.write" command. I use it to place news items and editorial features on our website. I want to modify this agent to count the number of documents in the view and then pull just one of them based on that number. I think I can replace the Set Doc = View.GetFirstDocument command with Set doc = view.getNthDocument(i+1)command, but I need help to count the number of documents in the view and then generate a random number to use to put in the getNthDocument line.

The desired end result is to have a changing main article from a certain view so that when the site is visited, the content is always changing. I have tried hard to achieve this, but as Notes and Domino is not my primary job, I have so far had no luck - help greatly appreciated!

Sub Initialize
Dim session As New NotesSession
Dim i,j,count As Integer
Dim db As NotesDatabase
Set db = session.currentdatabase
Set View = db.GetView("MainFeature")
Dim Doc As NotesDocument
Dim Picture, OutStr, UNID, BAR As String
Dim section, section1, section2, As String

Set Doc = View.GetFirstDocument
Print "Content-type: text/js" 'Prevents Domino from generating stuff
Do Until doc Is Nothing
If doc Is Nothing Then
section = Doc.MainItemHeading(0) 'Heading
section1 = doc.MainItem(0) 'Graphic to be used
section2 = doc.MainItemText(0) 'Catchphrase
Picture = "document.write('<IMG SRC=/mynotesdb.nsf/" + section1 + "?OpenImageResource>" + "<BR/>')"
OutStr= "document.write('<a href=/mynotesdb.nsf/Features/" + Doc.UniversalID + " class=mainfeature>" + section + MyDocs + "</A><BR/>')"
UNID= "document.write('<a href=/mynotesdb.nsf/Features/" + Doc.UniversalID + " class=feature>" + section2 + " </A><IMG SRC=/mynotesdb.nsf/item_arrow.gif?OpenImageResource><BR/>')"
BAR = "document.write('<HR WIDTH=100% SIZE=4 ALIGN=left COLOR=0099CC NOSHADE>')"
Print Picture
Print OutStr
Print UNID
Print BAR
Set Doc = View.GetNextDocument(Doc)
End If
count = count + 1

End Sub

RE: Selecting a Random Document from a View

I would suggest putting all docs in a collection (notesdocumentcollection). You have the property Count that tells you how many docs are in the collection, and you have the function GetNthDocument that allows you to retrieve a specific document in the collection.


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close