Chicago SharePoint Solutions Blog

Monday, September 18, 2006

Send Link of a Document in the Document Library

Difficulty: Medium
Applies to: WSS 2.0 and SPS 2003

Problem Statement
You store all of your documents in a Document Library which is exposed on the Home page of your site. You frequently need to point other people to your documents you would like them to review. Currently, the solution you employ is to manually copy the link to the document, open up a new email composing window, paste the link and send it to them through email. You would like to make this process more efficient.

Solution
The following solution will add a context menu item to the existing context menu actions that can be executed for each document in the document library. Currently, the context menu looks as following:

Figure 1. The original context menu of a Document Library

The final solution will augment this menu to look like the figure below:

Figure 2. The “Send Link By Email…” link has been added to the Document Library context menu

When the “Send Link By Email…” link is executed, it will bring up your desktop mail client with the link in the body of the email and a pre-populated subject line. The user can then enter an email address and send the link on its way (Figure 3).

Figure 3. Email composing window with pre-populated fields

If you have not acquainted yourself with the “Content Editor Web Part (CEWP)” yet, I would highly recommend you do so. This Web Part comes with the initial installation of both WSS and SPS 2003 and can be found in the Web Part Gallery. The purpose of this Web Part is to provide the user the ability to quickly insert content onto any Web Part page. The content can be in shape of rich text, html or even javascript or vbscript. We will use the CEWP in this scenario to insert javascript onto a WSS site’s Web Part page to enable the email functionality.

This scenario is assuming the following:
• Your Document Library Web Part is already deployed on a Web Part page somewhere in your site
• You have permission to add additional Web Parts on the page

To start, browse to the page (in WSS or SPS) where the document library is located. Make sure you are able to access the context menu for the documents in the document library (as shown in Figure 1 above). If you are not able to access the menu by hovering over each document, you will need to change the “View” of the document library. This can be done by first clicking on the “Modify Shared Web Part” link for the document library which brings up the Web Part properties pane (Figure 4 below). Change the Selected View option to “All Documents”.

Figure 4. The Document Library Web Part properties pane

The next step is to drag the Content Editor Web Part (CEWP) on this page. As I mentioned earlier, this Web Part can be found in the Web Part Gallery. Once deployed on the page, access this Web Part’s properties pane by clicking on the “Modify Shared Web Part” link. In the properties pane, click on the “Source Editor…” button. This will bring up a text entry window. Copy the following javascript into this window:

function Custom_AddDocLibMenuItems(m, ctx)
{
var strDisplayText = "Send Link By Email...";
var strAction;
var strImagePath = "";

// parse the URL out of the itemTable
var URL = "";
var index = itemTable.innerHTML.indexOf("href=");
if (index > 0)
{
var str = itemTable.innerHTML.substr(index + 6);
index = str.indexOf('"');
if (index > 0)
{
URL = str.substr(0, index);
}
}
if (URL != "")
{
strAction = 'window.navigate("mailto:%20?subject=Take a look at this document...&body=<' + URL + '>")';

// Add menu item
CAMOpt(m, strDisplayText, strAction, strImagePath);
// add a separator to the menu
CAMSep(m);
}
return false;
}

Click on the “Save” button to save the javascript in the CEWP. Close the Web Part properties pane.

That’s it!! Now if you try to access the context menu of any item in the document library, it should be rendered as in Figure 2 above and should display the “Send Link By Email…” link.

Side Tip: The CEWP will still show on the page as an empty Web Part. To hide this Web Part while keeping the functionality that it provides, access the Web Part properties pane and uncheck the “Visible on Page” option under the “Layout” section.