The example below demonstrates how to write and use a channel agent. A channel agent can be written in any scripting language with an installed scripting engine. The agent uses the COM objects passed to its namespace during a refresh cycle to update the channel content. Any channel in a project can have an agent refresh its content during the refresh cycle.
This example agent will scan a specified folder for all Microsoft® Word files and include them as content in a channel during a refresh cycle. In this case, the project is set up using a Windows Scripting Host Script (WSH). The top-level Channel object is configured to run the agent as a "customagent" meaning that the RefreshScript property will point to the script's location on the file system.
It should be stressed that this example is for illustration purposes only. Usually, it would be easier to build the Project object and Channel using one of the administrative tools such as MMC or WebAdmin. However, for testing purposes, the procedure listed below is quite useful.
(VBScript and Windows Scripting Host)
' File: sample.vbs
' This WSH script builds a Project object, sets up the
' top level channel object's refresh information,
' and runs the refresh cycle on the project
'
' It will scan the specified directory for all
' word documents (.doc) and automatically incorporate
' them into the channel
Option Explicit
Dim Project
Dim Channel
Dim Options
' Set up the Project with useful info
Set Project = CreateObject("Push.Project")
Project.Name="Test Project"
Project.CDFFile="c:\channels\LatestSpecs.cdf"
Project.Schedule.Startdate=#1/1/1998 19:00:00#
Project.Schedule.IntervalTime = 360
' suggest to subscribers that they 'check in'
' every 6 hours for new content
Set Channel = Project.Channel
Set Options = Channel.RefreshOptions
' Some useful (and hypothetical) Bibliographic info
Channel.Base = "file://\\somemachine\TechSpecs\"
Channel.Title="The Latest Specs on Widgets"
Channel.Abstract="This channel contains all the latest specification documents for the Widget research"
Channel.Authors.Add "Dr. John Doe"
Channel.Authors.Add "Dr. Jane Doe"
' Set up the refresh information
Channel.RefreshType="USERSCRIPT"
' here we will specify a custom script
' that is listed below. The path to the
' agent script is "c:\agentscripts\CustomTechRefresh"
Channel.RefreshScript="c:\scripts\CustomTechRefresh"
Options("DocsDir") = "c:\TechSpecs"
' this option specifies where to find the .doc files
' Now we run the refresh explicitly
Project.Refresh
' finished adding the items. Let's Schedule the refresh
' on the server for every 6 hours
call Project.SetTask("SomeUser",720,0)
Project.Task.Update "password"
' task should now be scheduled. The refresh cycle
' will occur every 6 hours starting a 12 am midnight
' every day
Project.Save "TestProject"
' save the project
The code listed below is the channel agent that runs on behalf of the channel to update the content. Notice the use of the built-in channel and options objects. These objects are automatically included in the namespace of the script when the refresh cycle runs and the scripts execute. They need not be created, and can be used as if they were intrinsic objects of the script. This is the same as within an ASP script, where the script writer can refer to the Response or Application object without having to create it explicitly.
The Active Channel Server object names are listed in bold in the example below.
' File: c:\scripts\CustomTechRefresh
'
' This script is an agent called by a Push.Channel object
' to refresh its content. It checks first to make sure
' a valid "DocDir" option is passed, and if so, does the
' following:
' deletes all items marked with "DeleteonFresh" set to TRUE
' Scans the directory for .doc files
' Adds any it finds as items to the Channel object on whose
' behalf the agent is being run.
If not util.IsValidData(Options("DocDir"), vbString ) Then
RaiseError("A valid string was not passed to the agent")
End If
Set fs = createobject("Scripting.FileSystemObject")
set folder = fs.GetFolder(Options("DocDir"))
For each File in folder.files
NameExt = split(File.Name,".")
If UBound(NameExt) > 0 Then
If NameExt(UBound(NameExt)) = "doc" Then
Set Item = Channel.AddItem
Item.HREF=File.Name
Item.Title=File.Name
End If
End If
Next