Tip

Delete documents over the Web using Ajax and JavaScript

VIEW MEMBER FEEDACK TO THIS TIP

The following step-by-step instructions explain how to delete document(s) over the Web using Ajax -- without having to refresh the current open window.

  1. Write your own table tag inside a view that you're going to show on the Web. This will give you the freedom to have complete control over the feel and look.

  2. In the first column, write the following code for showing a checkbox:
    "<tr id="+@Text(@DocumentUniqueID)+">
    <td width=10%><input id="$$SelectDoc_"+
    @Text(@DocNumber)  +""  type=checkbox  value=" + 
    @Text(@DocumentUniqueID) + "></td>"
  3. Now, make more columns with what you need and put them into the <td> as follows:
    "<td align=left>" + @Text(urfield) + "</td>"
  4. Check the view property "Treat view contents as HTML."

  5. Embed this view into a form inside the <table> tag as follows:
    <ur delete button goes here>
    <table border=0 cellspacing=2 cellpadding=2  
    width=100%> <ur embedded view 
    come here> </table>
  6. On the "Delete" button, click the call function called "getChkBoxValue()."

  7. Put the code below in your JavaScript:
    var strUIDs="";
    var Sep="";
    var req;
    var count=0;
    function getChkBoxValue()
    {
         var msg;
    msg= "Are you sure you want 
    to delete the selected document ? ";
    var agree=confirm(msg);
         if (agree)
         {
    var chkboxes = 
    document.getElementsByTagName("input");
    for(i=0;i<chkboxes.length;i++)
      {
     if(chkboxes.item(i)
    .attributes["type"].value == "checkbox") 
      {
     if (chkboxes.item(i).checked &
    & chkboxes.item(i).value !="0")
             {
      strUIDs = strUIDs +Sep+chkboxes.item(i).value  ;
      document.getElementById(chkboxes.item(i).value).
    style.display='none';
        Sep="~@~";
          }  
          }
      }   
     url="http://137.55.222.154/Rishi/CustomerDetails.nsf/
    deleteDocument?OpenAgent&processUNID="+strUIDs;
      loadXMLDoc(url);
    }
         else
            {
                return false 
             }     
        
    }
    
    function loadXMLDoc(url)
    {
     req = false;
      
        if(window.XMLHttpRequest) {
         try {
    
    req = new XMLHttpRequest();
            } catch(e) {
    
    req = false;
            }
    
        } else if(window.ActiveXObject) {
        try {
    req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
    try {
      req = new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {
    req = false;
     }
    }
        }
    if(req)
      {
    req.onreadystatechange = processReqChange;
    req.open("GET", url, true);
    req.send("");
     }
    }
    
    
    function processReqChange()
     {
     
        if (req.readyState == 4) 
       {
    
            if (req.status == 200) 
             {
                  xmlStr=req.responseText
                alert(xmlStr);
            } 
           else 
           {
    alert("There was a problem 
    retrieving the XML data:n" + req.statusText);
            }
        }
    }
    
    function toggleChkBox()
    {
       
     if (document.forms[0].chkAll.checked) 
       {
          selectAll();
         }
         else
         {
         deselectAll();
         }
    }
    
    
    function selectAll() {
     d = document.forms[0];
     for (i = 0; i < d.elements.length; i++) 
    {
       if (d.elements[i].type == "checkbox")
      {
         d.elements[i].checked = true;
       }
     }
    }
    
    function deselectAll() {
     d = document.forms[0];
     for (i = 0; i < d.elements.length; i++) {
       if (d.elements[i].type == "checkbox") {
         d.elements[i].checked = false;
       }
     }
    }
    
    var j=1;
    function HideBorder()
    {
       var chkboxes = 
    document.getElementsByTagName("input");
       for(i=0;i<chkboxes.length;i++)
       {
     if(chkboxes.item(i).attributes[
    "type"].value == "checkbox") 
          {
    if (chkboxes.item(i).id
    .indexOf("$$SelectDoc_") ==0)
                {
    document
    .getElementById("$$SelectDoc_"+j)
    .style.borderRight='0';
    document
    .getElementById("$$SelectDoc_"+j)
    .style.borderLeft='0';
    document
    .getElementById("$$SelectDoc_"+j)
    .style.borderTop='0';
    j++;
      }   
          }
     }   
      
    }
    

    ***In the above JavaScript, there is: an XMLHTTp call for the agent to delete the document; the function to get selected document UID; the function to remove ugly borders from the checkbox; and the last function called "toggleChkBox" -- which is used to select and deselect all checkboxes from one single checkbox , per Yahoo style.

  8. The next step is to write an agent called "deleteDocument" and paste the below code :
    On Error Goto errhandler
     
    Print "Content-Type:text/plain"
    
    Dim session As New NotesSession
    Dim currDb As NotesDatabase
    Dim coll As NotesDocumentCollection 
    Dim strEmpId  As String 
    Dim doc As NotesDocument 
    Dim currDoc As NotesDocument
    Dim tmpdoc As NotesDocument 
    Dim view As NotesView 
    
    Set currDb= session.CurrentDatabase 
    Set currDoc=session.DocumentContext 
    Set view=currDb.GetView("DeleteDocument")
     
    strRowDocUID= 
    Strrightback(currDoc.QUERY_STRING(0),"=")
    strDocUID=Split(strRowDocUID,"~@~")
    
    For i=0 To Ubound(strDocUID)
    Set tmpdoc= 
    currDb.GetDocumentByUNID(strDocUID(i))
    Call tmpdoc.replaceitemvalue("Delete","Yes")
    Call tmpdoc.save(True,False)
    Next
     
    Print i & "  document(s) successfully deleted" 
     
    Exit Sub
    errhandler:
    Print "Error" & Str(Err) & ": " & Error$
    Exit Sub
  9. Now, write the following selection formula to the view you are showing on the Web.
    SELECT Form="frmCustomerDetails" 
    & Delete !="Yes"
  10. The last step is to write one agent to delete those documents whose flag is Delete="Yes."

MEMBER FEEDBACK TO THIS TIP

The method used above is only secure if the agent "deleteDocument" is flagged as "Run as Web User." Otherwise, anyone with read access could delete documents in the database.
—Markus K.

******************************************

In my opinion, just because you can accomplish this with Ajax, doesn't necessarily mean you should. With something like deleting documents, that could be misused. I think this tip adds a layer of unneeded complexity. The function already exists using various standard methods over the Web, such as URL commands, etc. I think you should wait for a reload if they are deleting multiple documents.
—Jason H.

Do you have comments on this tip? Let us know.

This tip was submitted to the SearchDomino.com tip library by member Rishikesh Sahi. Please let others know how useful it is via the rating scale below. Do you have a useful Lotus Notes, Domino, Workplace or WebSphere tip or code snippet to share? Submit it to our monthly tip contest and you could win a prize.

This was first published in September 2006

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.