Creating a Lock on an Item

Creating a Lock on an Item

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

The following example uses the LOCK Method to put an exclusive write lock on an item. If the request is successful, the lock token returned in the Lock-Token response header may later be used to remove the lock on the item.

VBScript

dim strBody
dim strURL
dim req
dim strLockToken

' The URL of the item that the lock is being placed on.
strURL = "https://server/public/TestFolder1/test.txt"

' Build the body of the LOCK request.
strBody = "<?xml version='1.0'?><D:lockinfo xmlns:D='DAV:'>"

' Make it an exclusive write lock.
strBody = strBody & "<D:lockscope><D:exclusive/></D:lockscope>"
strBody = strBody & "<D:locktype><D:write/></D:locktype>"

' Set the owner of the lock.
strBody = strBody & "<D:owner><D:href>https://server/lockowner</D:href></D:owner>"
strBody = strBody & "</D:lockinfo>"

' Create the XMLHTTP object.
set req = createobject("microsoft.xmlhttp")

' Specify the LOCK method, the URL of the resource to lock, that the request will be sent synchronously,
' the user name, and the password.
req.open "LOCK", strURL, false, "Domain\Username", "!Password"

' Set the Translate header to False.
req.setrequestheader "Translate", "f"

' Set the Content-Type header to "text/xml".
req.setrequestheader "Content-Type", "text/xml"

' Set a depth of 0.
req.setrequestheader "Depth", "0"

' Set the lock time-out for 100 seconds.
req.setrequestheader "Timeout", "Second-100"

' Send the LOCK request.
req.send strBody

If req.status >= 500 Then
   ' An error occurred on the server.
   wscript.echo "Status: " & req.status
   wscript.echo "Status text: An error occurred on the server."

Else
   ' Display the request status, response text, and returned lock token.
   wscript.echo "Status: " & req.status
   wscript.echo "Status text:  " & req.statustext
   wscript.echo "Response text: " & req.responsetext

   ' Get the lock token from the Lock-Token response header. The lock token
   ' may be used later to remove the lock on the item.
   strLockToken = req.getresponseheader("Lock-Token")

   ' Display the lock token.
   wscript.echo "Lock-Token: " & strLockToken
End If

The following example uses the LOCK Method to put an exclusive write lock on an item. If the request is successful, the lock token returned in the Lock-Token response header may later be used to remove the lock on the item.

C++

#include <stdio.h>
#include <iostream.h>

// If necessary, change the file path if your msxml.dll file is
// in a different location.

#import "c:\windows\system32\msxml.dll"

// To use MSXML 4.0, import the dll msxml4.dll instead of msxml.dll as follows:
// #import "c:\windows\system32\msxml4.dll"
// using namespace MSXML2;

using namespace MSXML;

int main(int argc, char* argv[])
{
   CoInitialize(NULL);

   // Variables.
   MSXML::IXMLHttpRequest* pXMLHttpReq=NULL;
   bstr_t sUrl = "https://server/public/TestFolder1/test.txt";
   bstr_t sMethod = "LOCK";
   _variant_t vUser = L"Domain\\Username";
   _variant_t vPassword = L"!Password";
   _variant_t vAsync = (bool)FALSE;
   long lStatus = 0;
   BSTR bstrResp;
   BSTR bstrResponseText;
   HRESULT hr;
   bstr_t sBody = "";
   BSTR bstrLockToken;

   // Initialize the XMLHTTPRequest object pointer.
   hr = ::CoCreateInstance(__uuidof(XMLHTTPRequest),
                           NULL,
                           CLSCTX_INPROC_SERVER,
                           __uuidof(IXMLHttpRequest),
                           (LPVOID*)&pXMLHttpReq);

   // If you are using MSXML 4.0, use the following to initialize pXMLHttpReq:
   // IXMLHTTPRequestPtr pXMLHttpReq= NULL;
   // HRESULT hr=pXMLHttpReq.CreateInstance("Msxml2.XMLHTTP.4.0");

   // Check the status of pointer creation.
   if (S_OK != hr)
   {
      cout << "XMLHttpRequest pointer creation failed." << endl;
      return 1;
   }

   try
   {
      // Open the XMLHTTPRequest object with the LOCK method and
      // specify that it will be sent asynchronously.
      pXMLHttpReq->open(sMethod,
                        sUrl,
                        vAsync,
                        vUser,
                        vPassword);

      // Set the Content-Type header.
      pXMLHttpReq->setRequestHeader((bstr_t)"Content-Type", (bstr_t)"text/xml");

      // Set the Depth header to 0.
      pXMLHttpReq->setRequestHeader((bstr_t)"Depth", (bstr_t)"0");

      // Set the Translate header to False.
	  pXMLHttpReq->setRequestHeader((bstr_t)"Translate", (bstr_t)"F");

      // Set the lock time-out to 100 seconds.
      pXMLHttpReq->setRequestHeader((bstr_t)"Timeout", (bstr_t)"Second-100");


      // Set the LOCK method request body.
      sBody = "<?xml version='1.0'?><D:lockinfo xmlns:D='DAV:'>";

      // Make it an exclusive write lock.
      sBody += "<D:lockscope><D:exclusive/></D:lockscope>";
      sBody += "<D:locktype><D:write/></D:locktype>";

      // Set the lock owner.
      sBody += "<D:owner><D:href>https://server/lockowner</D:href></D:owner>";
      sBody += "</D:lockinfo>";

      // Send the LOCK method request.
      pXMLHttpReq->send(sBody);

      // Get the response status.
      pXMLHttpReq->get_status(&lStatus);

      // An error occurred on the server.
      if(lStatus >= 500)
      {
         cout << "Status: " << lStatus << endl
              << "Status text: An error occurred on the server."
              << endl;
      }

      else
      {
         // Display the response status.
         cout << "Status: " << lStatus << endl;

         // Display the response status text.
         pXMLHttpReq->get_statusText(&bstrResp);
         cout << "Status text: " << (char*)(bstr_t)bstrResp << endl;

         // Display the response text.
         pXMLHttpReq->get_responseText(&bstrResponseText);
         cout << "Response text: " << (char*)(bstr_t)bstrResponseText << endl;

		 // Display the lock token.
		 bstrLockToken = pXMLHttpReq->getResponseHeader((bstr_t)"Lock-Token");
		 cout << "Lock-Token: " << (char*)(bstr_t)bstrLockToken <<endl;
      }

      // Release the memory.
      pXMLHttpReq->Release();
   }

   catch(_com_error &e)
   {
      // Display the error information.
      cout << "Error code: " << (char*)e.Error() << endl
           << "Error message: " << (char*)e.ErrorMessage()
           <<endl;

      // Release the memory.
      pXMLHttpReq->Release();

      return 1;
   }

   CoUninitialize();

   return 0;
}   

Send us your feedback about the Microsoft Exchange Server 2003 SDK.

Build: June 2007 (2007.618.1)

© 2003-2006 Microsoft Corporation. All rights reserved. Terms of use.