Tuesday, January 18, 2011

How to open .eml file through client program

How to open .eml file through client program such as "Outlook" and "windows live mail"?  For some unknown reason, IE cannot handle it very well. If the .eml file hyperlink doesn't contain the javascript even handler such as "onclick", then the file will always be opened by IE ! This means we cannot check the email attachments!

I did some research, learned a few tricks about the registry table modification and IIS MIME settings, however, none of them works.  (My environment is windows 7 + SharePoint 2010 + IE 8).   IE 9 also has the same problem.

So I built the javascript code below, save it into a plain text file, then upload it to a document library.  Then in the SharePoint web part page, I inserted a content editor web part, add the reference of that javascript file into the CEWP. Then it works well although still have the warning message below:

"Some files can harm your computer. If the file information below looks suspicious, or you do not fully trust the source, do not open the file."

/////////////////////////////////////////////////////////////////////////

<script language="javascript" >
_spBodyOnLoadFunctionNames.push('AddOnclickToEmlLinks');

function strEndsWith(testString, endingString) {
    var startPos = testString.length - endingString.length;
    if (startPos < 0) {
        return false;
    }
    return (testString.lastIndexOf(endingString) == startPos);
}

function spOpen(link) {
    var stsOpen = null;
    try {
        stsOpen = new ActiveXObject("SharePoint.OpenDocuments.3");
    }
    catch (ex) {
        alert("Exception while opening the Active X object I will open the document as a normal link.");
        return;
    }

    //if (strEndsWith(link.nameProp, '.eml'))
    //{
       //stsOpen.ViewDocument3(window, link, 2, '');
       stsOpen.EditDocument3(window, link, false, "");
    //}
    return false;
}

function AddOnclickToEmlLinks() {
  try {
    var links = document.getElementsByTagName("A");
    for (var i = 0; i < links.length; i++) {
        var link = links(i);

        //var onValues = link.onclick + "";

        //if (onValues == 'null')
        if (strEndsWith(link.nameProp, '.eml'))
        {
            if (strEndsWith(link.nameProp, '.eml')) {
               link.setAttribute('onclick', 'return spOpen(this);');
               link.setAttribute('target', '_blank');
            }
        }
    }
  }
  catch (e)
  {
    alert('e=' + e);
  }
}

</script>

/////////////////////////////////////////////////////////////////////////

By the way, we can open .eml file from asp.net web site through similar code.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
    <Columns>
        <asp:TemplateField HeaderText="Edit">
            <ItemTemplate>
                <asp:HyperLink ID="linkEdit" runat="server" Text="edit" NavigateUrl='<%# Bind("Edit") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField ReadOnly="True" HeaderText="Name" InsertVisible="False" DataField="ows_LinkFilename"
            SortExpression="ows_LinkFilename"></asp:BoundField>
        <asp:BoundField HeaderText="Modified" DataField="ows_Modified" SortExpression="ows_Modified">
        </asp:BoundField>
        <asp:BoundField HeaderText="Modified By" DataField="ows_Editor" SortExpression="ows_Editor">
        </asp:BoundField>
    </Columns>
</asp:GridView>


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        HyperLink objHyperLinkEdit = (HyperLink)e.Row.Cells[0].Controls[1];
        objHyperLinkEdit.Attributes.Add("onclick", "return spOpen(this, 'edit');");
    }
}

/////////////////////////////////////////////////////////////////////////


reference links:
http://www.eggheadcafe.com/community/aspnet/69/10122714/opening-list-attachment-in-a-new-window.aspx
http://msdn.microsoft.com/en-us/library/cc264285.aspx

PS1:  To fix the same issue in the workflow task form, we have to modify the OOTB page "14\TEMPLATE\LAYOUTS\WrkTaskIP.aspx".  Add the same javascript code so new onclick event handler will replace the original "DispDocItemEx(this, 'FALSE', 'FALSE', 'FALSE', 'SharePoint.OpenDocuments.3');".   This change may be overwritten when upgrade SharePoint server.

2 comments:

  1. This looks exactly what we need but I am having trouble getting it to work. What extension should I save the JS file as and could you give an example of the CEWP reference to it?
    Many thanks

    ReplyDelete
  2. Hi Mark,

    It doesn't matter, any file extension is fine.

    Normally I use .js or .txt

    ReplyDelete