Now we are going to recapture some of the WebDAV protocol through examples. Through the Mkcol sample code, we are going to see how WebDAV provides capabilities for NOTE Please spend time reading this sample carefully , because it will save you time in understanding the other sample programs, which are very similar to this. In this example, we use VBScript to -
Create a Web folder called SPSBOOK as a subfolder of the Documents folder of SPS. -
Use XML to send additional requests for setting the COMMENT property of the newly created SPSBOOK folder to "This is my SPSBOOK folder created by the mkcol method". -
Get the response back in XML format and save it to file. Listing 16.1 shows the request header and the request body that will be sent to the host named dianatr. MKCOL is a WebDAV method for creating collections (in our example, SPSBOOK) and after SPSBOOK is created, we want additional processes from the server for setting the comment property of the folder to "This is my SPSBOOK folder". Listing 16.1 How to Use MKCOL WebDAV Method as Part of the Request Header and Set dditional Properties Through the Body Section MKCOL /spsbook/documents/SPSBOOK/ HTTP/1.1 Host: dianatr Content-Type: text/xml Content-Length: XXX <?xml version="1.0"?> <D:propertyupdate xmlns:D="DAV:" > <D:set> <D:prop> <D:comment>This is my SPSBOOK folder</D:comment> </D:prop> </D:set> </D:propertyupdate> We create an xmlhttp COM object and define MKCOL in the request, as shown here: Set DAVRequest = CreateObject("MSXML2.xmlhttp") DAVRequest.open "MKCOL", strURL, False, strUser, strPassword Then we create a string that has XML as part of the request body. This XML sets the comment property of the SPSBOOK folder: strXMLRequest = "" + _ "<?xml version='1.0'?>" + _ "<D:propertyupdate xmlns:D='DAV:'>" + _ "<D:set>" + _ "<D:prop>" + _ "<D:comment>This is my SPSBOOK folder</D:comment>" + _ "</D:prop>" + _ "</D:set>" + _ "</D:propertyupdate>" We also need to indicate the content type as text/xml, which can be set up through the setRequestHeader method: DAVRequest.setRequestHeader "Content-Type", "text/xml" Finally we send the request along with the XML string DAVRequest.send strXMLRequest We get the response in XML format. I have created a DOMDOCUMENT object to load the XML response into, and save it to the mkcol.xml file for the purpose of the presentation. Set xmlResponse = CreateObject("MSXML2.domdocument") xmlResponse.async = False xmlResponse.load DAVRequest.responseXML xmlResponse.save "mkcol.xml" The content of the response in the mkcol.xml file is shown in Listing 16.2. Listing 16.2 The Response Sent in XML Format from the Web Server for the MKCOL Request <?xml version="1.0"?> <a:multistatus mlns:a="DAV:"> <a:response> <a:href>http://dianatr/spsbook/Documents/SPSBOOK</a:href> <a:status>HTTP/1.1 201 Created</a:status> <a:propstat> <a:status>HTTP/1.1 200 OK</a:status> <a:prop> <a:comment/> </a:prop> </a:propstat> </a:response> For this request, you will receive status code 207, which indicates that the response carries status codes for multiple processes. The first action was creating the SPSBOOK folder, and the status code for this action is 201, which means the SPSBOOK folder was created successfully. The second process was setting the comment property of the folder, and the status code of this process is 200, which means the property was set correctly. Listing 16.3 shows the complete VBScript code. You can execute this code from the command line by running this command: CSCRIPT mkcol.vbs The output file mkcol.xml will be created in the same folder where you ran the cscript command. Listing 16.3 The Complete VBScript Code for Creating a Collection Using MKCOL Method, and at the Same Time Set Some Properties for the Collection Dim strServer Dim strWorkspace Dim strFolder Dim strURL Dim strUser Dim strPassword Dim DAVRequest Dim strXMLRequest strServer = "dianatr" strWorkspace = "spsbook" strFolder = "SPSBOOK" strUser = "dianatr0\spsuser" strPassword = "spspw" strURL = "http://" + strServer + "/" + strWorkspace + "/DOCUMENTS/SPSBOOK" strURL = Replace(strURL, " ", "%20") strXMLRequest = "" + _ "<?xml version='1.0'?>" + _ "<D:propertyupdate xmlns:D='DAV:'>" + _ "<D:set>" + _ "<D:prop>" + _ "<D:comment>This is my SPSBOOK folder</D:comment>" + _ "</D:prop>" + _ "</D:set>" + _ "</D:propertyupdate>" Set DAVRequest = CreateObject("MSXML2.xmlhttp") DAVRequest.open "MKCOL", strURL, False, strUser, strPassword DAVRequest.setRequestHeader "Content-Type", "text/xml" DAVRequest.send strXMLRequest Set xmlResponse = CreateObject("MSXML2.domdocument") xmlResponse.async = False xmlResponse.load DAVRequest.responseXML xmlResponse.save "mkcol.xml" |