Share via


Removing a Lock from an Item

Topic Last Modified: 2006-06-12

The following example uses the UNLOCK Method to remove a write lock from an item. The lock token submitted in the Lock-Token request header is the lock token that was returned from the LOCK Method request that placed the lock on the item.

Example

VBScript

Example

dim strURL
dim req

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

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

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

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

' Set the Lock-Token header to the value of the Lock-Token returned in the LOCK response.
req.setrequestheader "Lock-Token", "<opaquelocktoken:BADAOF19-BE44-4BF2-87B3-95EBD896AE6:XN63>"

' Send the UNLOCK request.
req.send

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

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

End If

The following example uses the UNLOCK Method to remove a write lock from an item. The lock token submitted in the Lock-Token request header is the lock token that was returned from the LOCK Method request that placed the lock on the item.

C++

Example

#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 = "UNLOCK";
   _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;

   // 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 Depth header to 0.
      pXMLHttpReq->setRequestHeader((bstr_t)"Depth", (bstr_t)"0");

      // Set the lock token-header to the lock token returned in the LOCK response.
      pXMLHttpReq->setRequestHeader( (bstr_t)"Lock-Token",
                                     (bstr_t)"<opaquelocktoken:BADAOF19-BE44-4BF2-87B3-95EBD896AE6:XN63>" );

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

      // 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;
      }

      // 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;
}   

See Also

Other Resources

Creating a Lock on an Item