Creating a Custom Channel Agent

The example below demonstrates building and using a custom Channel Agent.  For the purposes of this SDK, a custom channel agent is any agent other than the agents that come installed with Site Server 3.0.

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.

Building the Channel and Running the Refresh Cycle

(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 Custom Agent

The code listed below is the custom agent.  Notice the use of the built-in objects.

' 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

© 1997-1998 Microsoft Corporation. All rights reserved.