Geeks With Blogs

Lance's TextBox

This post is the seventh in a series of postings, containing examples of SharePoint WebParts that anybody can build all by themselves.  To read all posts in this series, or to get started with the RSSBus WebPart, go here.

#7 – Search Email History

One of my good friends here recently asked me to give him an easy way to quickly bring up a list of email correspondence to and from a particular email address over the past 6 months.  This way, if he is assisting someone on the telephone or email, he can quickly get up-to-date on what has gone on with this particular person in the recent past.  In theory this is easy to do, but doing it in SharePoint would be difficult  - well…it used to be.  Now you can just copy and paste this template into an RSSBus Web Part and voila.  :)  Customize to your liking. 

Step one is to make sure you have the RSSBus Web Part installed.  See here for instructions.

Step two, make sure you have the Imap Connector installed.  If you don’t already know the hostname/ip address of your IMAP mail server (e.g. MS Exchange) and a username/password to authenticate to that server, find that information next.  This is the same information that you would enter into Outlook when configuring it to connect to your mail server.

Step three, add the RSSBus WebPart to your page, paste the following template into its source editor, and click “Apply”.

  <input name="server" required="true" />
  <input name="email" required="true" />
  <input name="password" required="true" />
  <input  name="mailbox" required="true" />
  <input  name="months" default="-6" />
  <input  name="sortcriteria" default="REVERSE DATE" />

<!-- Find out what the current query is, form variable overrides querystring variable -->
<rsb:check attr="_request.form:email">
  <rsb:set attr="query" value="[[_request.form:email]]" />
  <rsb:check attr="_request.querystring:email">
    <rsb:set attr="query" value="[[_request.querystring:email]]" />

<!-- regular html form to get an email to search for -->
    <input id="email" type="text" name="email" size="25" value="[[query | def('')]]" />
    <input type=submit value="Search" onclick="clearDiv('myemailsnippet');" />
<!-- if an email is specified in the page request, execute this section -->
<rsb:check attr="">
  <rsb:import lib="RSSBus.ImapOps" />
  <div id="myemails" style="height:15em;overflow:auto">

    <!-- set the search criteria to get emails to and from email address over the past x months -->
    <rsb:set attr="sentsince" value="[[sentsince | date() | dateadd('Month', '[[months]]') | todate('dd-MMM-yyyy') ]]" />
    <!-- this search criteria format is the standard defined in the IMAPv4 RFC -->
    <rsb:set attr="searchcriteria" value="SENTSINCE [[sentsince]] OR FROM [[query]] TO [[query]]" />

    <!-- search for matching emails -->
    <rsb:set attr="uidmode" value="TRUE" />
    <rsb:call op="imapSearch">
        <div style="overflow:auto">
          <table width="100%">
              <th style="background-color:LightGrey; white-space:nowrap">From</th>                              
              <th style="background-color:LightGrey; white-space:nowrap">Subject</th>                        
              <th style="background-color:LightGrey; white-space:nowrap">Date</th>                            
              <th style="background-color:LightGrey; white-space:nowrap">Size</th>                                
      <!-- usually, the first part of an email will be plain text -->      
      <rsb:set attr="" value="1" />

      <!-- for multi-part emails, find the best (text/html) part for display -->
      <rsb:notnull attr="part:contenttype">
        <rsb:enum attr="part:contenttype">
            <rsb:set attr="contenttype" value="part:contenttype#[_index]" />
            <rsb:equals attr="contenttype" value="text/html">
            <rsb:set attr="" value="[_index]" />
             <td style="white-space:nowrap">[[imap:fromname]]</td>
             <td style="white-space:nowrap">
               <a href="?msgnumber=[[imap:id]]&partid=[[]]&email=[[query]]">[[imap:subject | truncate('55')]]</a>
             <td style="white-space:nowrap">[[imap:date]]</td>  
             <td style="white-space:nowrap">[[imap:size]]</td>

  <!-- if a specific message number has been clicked on, display its text/html or 1st part -->
  <rsb:check attr="_request.msgnumber">
    <div id="myemailsnippet" style="height:30em;overflow:auto">
      <rsb:call op="imapGetPart?id=[[_request.msgnumber]]&partid=[[_request.partid]]">
        <div id="headers" style="height:50px">
          <table width="100%" style="background-color:LightGrey">
              <td width="75px">From:</td>
              <td><a href="mailto:[[imap:fromemail | htmlencode]]">[[imap:fromname]]</a></td>
            <tr><td width="75px">Date:</td><td>[[imap:date]]</td></tr>
            <tr><td width="75px">Subject:</td><td>[[imap:subject]]</td></tr>
            <tr />   
        <div id="body" style="overflow:auto">
          [[imap:content | Replace('0A','<br>','true')]]


function clearDiv(div) {
    var mydiv = document.getElementById(div); 
    mydiv.innerHTML = "";

Step four is to specify your IMAP server and authentication credentials.  If you’ve clicked “Apply” in the source editor of the web part, you should now see a new property appear in the properties editor of the web part in your browser.  Under “Web Part Inputs”, you’ll see “server”, “email”, “password”, “mailbox” inputs.  These inputs will need to be populated with the values corresponding to your IMAP server, mailbox name (e.g. “Inbox”), and account credentials.  Optionally you can change the “months” and “sortcriteria” inputs to alter the output generated.  By default, this web part will display messages to/from a specified email address in the past 6 months (the “months” input property) and then sort them by reverse-date (most recent first – the “sortcriteria” input).

After you have set these inputs appropriately, click “Apply” in the properties window and the page will reload.  Enter an email address in the search box and click “Search”.  The emails sent to or from that email address will be displayed inside the web part!  Click on any one of those messages to load a plain text or html view of that email.

As always, you can customize the html in the template if you want, or leave it as is.

Advanced Tip – Note that the “sortcriteria” input expects a sort order formatted according to the IMAP Sort RFC (RFC 5256).  Other common sort criteria:  ARRIVAL, DATE, CC, FROM, SUBJECT, TO, SIZE.  Any of those criteria can be preceeded with REVERSE as I’ve shown in my example.  Similarly, the searchcriteria specified inside the template itself can also be modified in the format specified in the IMAPv4 RFC (Section 6.4.4 in RFC 3501).

See all posts in this series:  10 DIY SharePoint Web Parts.
Browse RSSBus Connectors.
Basic RSSBus Scripting Cheat Sheet.

Technorati Tags: SharePoint, RSSBus, WebPart, Exchange, IMAP, Email

Posted on Wednesday, October 21, 2009 5:07 AM | Back to top

Comments on this post: 10 DIY SharePoint Web Parts #7

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Lance Robinson | Powered by: