All the virtual root and virtual name configuration that we have done in this chapter by utilizing a GUI also can be carried out programmatically. Microsoft has provided an object model called the Virtual Directory Management Object Model to accomplish this. It consists of the following objects:
In an object model, the objects contained in an application provide the state, functionality, and characteristics of an application. A collection object, however, contains related objects (a collection). A collection object's Item method is usually employed to gain access to one of its objects. The Web, which utilizes the HTTP protocol, has several collection objects, one of which is the Form Collection. How many of you have filled out a form on the Internet and then submitted your information by clicking on a button? What's actually going on is that the names of the form elements that you have put data in are passed to a Web server along with the data itself. The Form Collection contains these names and their associated data. After that collection arrives at the server, the data can be accessed and processed as the developer sees fit. Take a look at Listing 3.1, which shows the relationship of these different entities and how to iterate through all the individual objects contained in the Form Collection via Active Server Pages and VBScript (this code assumes that the user has clicked the Submit button).This example contains both a small portion of HTML code and server-side code. Listing 3.1 Collection Object, Objects, and Item<% for each Item in Request.Form %> Name = <% =Item %> Value = <% =Request.Form(Item) %> <BR> <% Next %> In the Virtual Directory Management Object Model, SQLVDirControl is the top-level object or the collection, and the only one that can be directly created. All other objects must be obtained from the SQLVDirControl object, or a derivative of it. A hierarchy diagram (object model) illustrates the relative arrangement of objects to one another. The diagram for creating a virtual root is given in Figure 3.9. Figure 3.9. Virtual Directory Management Object Model hierarchy.
SQLVDirControl ObjectThis is the only object in the object hierarchy that can be directly accessed programmatically. All other objects must be accessed through it or derived from it. Table 3.7 gives the SQLVDirControl methods. The rest of this chapter is dedicated to providing examples to accomplish the manipulation of all the other objects. Table 3.7. SQLVDirControl Methods
Listing 3.2 is an example showing server and Web site connection methods and how to connect to the first Web site on a Web server. For a more comprehensive example of the usage of this object, and the other objects in the hierarchy, see the section "Using the Object Model" toward the end of this chapter. Listing 3.2 Selecting the Server's First Web SiteSet objDirControl = Server.CreateObject("SQLVDir.SQLVDirControl") 'create the 'base object ObjDirControl.Connect "IISServer", "1" 'connect to 'the first web 'site on the 'server 'IISServer Set objDirs = objDirControl.SQLVDirs 'get the 'virtual 'directory 'collection 'object ... ObjDirControl.Disconnect SQLVDirs Collection ObjectAs seen in the small example provided in the preceding section, the SQLVDirs , which is the collection object of virtual directories, is returned by the SQLVDirControl.SQLVDirs methods as given in Table 3.8. After you have the SQLVDirs collection you can:
Table 3.8. SQLVDirs Methods
Listing 3.3 shows an example of connecting to a Web site and accessing the first virtual directory object. Listing 3.3 Accessing the First Virtual Directory Object, Item(0)Set objDirControl = Server.CreateObject("SQLVDir.SQLVDirControl") 'create the 'base object ObjDirControl.Connect "IISServer", "1" 'connect to 'the first 'web site on 'the server 'IISServer Set objDirs = objDirControl.SQLVDirs 'get the 'virtual 'directory 'collection object Set objDir = objDirs.Item(0) 'get the first virtual 'directory object. You 'could say objDirs(0) 'since Item() is the 'default ... ObjDirControl.Disconnect SQLVDir ObjectThe SQLVDir object is obtained by calling the Item method, the SQLVDirs object, and the AddVirtualDirectory method when creating a new virtual directory. The SQLVDir object has the following properties, as given in Table 3.9, all of which are read/write (you can set or get them), except Password . Password is write only (it can only be set). Table 3.9. SQLVDir Object Properties
In addition to these properties, the SQLVDir object supports the VirtualNames method. This method returns the collection of virtual names for the virtual directory. Listing 3.4 shows an example of connecting to a Web site and setting the PhysicalPath property of the first virtual directory object. Listing 3.4 Setting the PhysicalPath PropertySet objDirControl = Server.CreateObject("SQLVDir.SQLVDirControl") 'create the 'base object ObjDirControl.Connect "IISServer", "1" 'connect to 'the first web 'site on the 'server 'IISServer Set objDirs = objDirControl.SQLVDirs 'get the 'virtual 'directory 'collection 'object Set objDir = objDirs.Item(0) 'get the first 'virtual 'directory 'object. You 'could say 'objDirs(0) 'since Item() 'is the 'default objDir.PhysicalPath = "C:\" 'these two lines 'just show two objDir.PhysicalPath = objDir.PhysicalPath & "inetpub" 'different ways 'of setting the 'value. It can 'be done either 'way ObjDirControl.Disconnect VirtualNames Collection ObjectThe VirtualNames collection object is a collection of virtual names in the virtual directory object. It is similar to the SQLVDirs object (a collection of virtual roots). The VirtualNames collection object contains the following methods given in Table 3.10. Table 3.10. VirtualNames Methods
Listing 3.5 provides the steps required to access a VirtualNames collection object. Listing 3.5 Accessing a VirtualNames Collection ObjectSet objDirControl = Server.CreateObject("SQLVDir.SQLVDirControl") 'create the 'base object ObjDirControl.Connect "IISServer", "1" 'connect to 'the first 'web site on 'the server 'IISServer Set objDirs = objDirControl.SQLVDirs 'get the 'virtual 'directory 'collection 'object Set objDir = objDirs.Item(0) 'get the first 'virtual 'directory 'object. You 'could say 'objDirs(0) 'since Item() 'is the 'default Set objNames = objDir.VirtualNames 'get the 'Virtualnames 'collection ObjDirControl.Disconnect VirtualName ObjectThe VirtualName object is obtained by calling the Item method (or the AddVirtualName object if you are creating a new virtual name). Listing 3.6 demonstrates this. The VirtualName collection object contains the following properties given in Table 3.11. Table 3.11. VirtualName Properties
Listing 3.6 Accessing a VirtualName Object and Setting AttributesSet objDirControl = Server.CreateObject("SQLVDir.SQLVDirControl") 'create the base 'object ObjDirControl.Connect "IISServer", "1" 'connect to the 'first web site 'on the server 'IISServer Set objDirs = objDirControl.SQLVDirs 'get the virtual 'directory 'collection 'object Set objDir = objDirs.Item(0) 'get the first 'virtual 'directory 'object. You 'could say 'objDirs(0) 'since Item() is 'the default Set objNames = objDir.VirtualNames 'get the 'Virtualnames 'collection Set objName1 = objNames.Item(0) 'get the first 'virtual name 'object ObjNamem1.Type = 2 'set the 'properties of 'the virtual ObjNamem1.Name = "MySchema" 'name object 'obtained above ObjNamem1.Path = "C:\inetpub\schema" ... ObjDirControl.Disconnect To create a new virtual name, do something similar to this: Set NewVName = objNames.AddVirtualName "MyNewSchema", 2, "C:\inetpub\schema" |