Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Is it possible to handle the click event of a file in a sharepoint document library so that instead of opening the actual file, it opens an application page?

E.g. Open letter.xml goes to letter editor.aspx in layouts.

share|improve this question
    
What's the point of the document? Why not make it a list / list item with custom view / edit forms? The list item could lookup to a file. –  ToddersLegrande Jan 26 '12 at 2:28
add comment

2 Answers 2

Here is an example JavaScript you can add to your custom masterpage:

<script type="text/javascript">
//<![CDATA[
function OpenPdfInNewWindow()
{
    try
    {
        var OpenPdfInNewWindowAnchors, OpenPdfInNewWindowAnchorsCounter;
        OpenPdfInNewWindowAnchors = document.getElementsByTagName('A');
        for (OpenPdfInNewWindowAnchorsCounter = 0; OpenPdfInNewWindowAnchorsCounter < OpenPdfInNewWindowAnchors.length; OpenPdfInNewWindowAnchorsCounter ++)
        {
            var OpenPdfInNewWindowAnchor = OpenPdfInNewWindowAnchors[OpenPdfInNewWindowAnchorsCounter].href.toLowerCase();
            if (OpenPdfInNewWindowAnchor.lastIndexOf(".pdf") != -1)
            {
                try
                {
                    if (OpenPdfInNewWindowAnchors[OpenPdfInNewWindowAnchorsCounter].onclick != "")
                    {
                        OpenPdfInNewWindowAnchors[OpenPdfInNewWindowAnchorsCounter].onclick = "";
                    }
                }
                catch (e) {}

                try
                {
                    OpenPdfInNewWindowAnchors[OpenPdfInNewWindowAnchorsCounter].setAttribute('target', "_blank");
                    OpenPdfInNewWindowAnchors[OpenPdfInNewWindowAnchorsCounter].href = L_Menu_BaseUrl + "/_layouts/pdfviewer.aspx?src=" + OpenPdfInNewWindowAnchor;
                }
                catch (e) {}
            }
        }
    }
    catch (e) {}
}

if (typeof(_spBodyOnLoadFunctionNames) != "undefined")
{
    if (_spBodyOnLoadFunctionNames != null)
    {
        _spBodyOnLoadFunctionNames.push("OpenPdfInNewWindow");
    }
}
//]]>
</script>

Though, you need to tweak it a little bit to fit your needs. You could also use jQuery but in my case I didn't want to rely on a third-party lib for such a simple task.

share|improve this answer
add comment

If you want a file to be open by making user to open it via "ECB" menu for an item then you can create custom action (check here for simple walkthrought http://codename-srini.blogspot.com/2010/04/using-custom-actions-in-sharepoint-2010.html) and check the respective location group setting name i.e. ECB (complete ref here : http://msdn.microsoft.com/en-us/library/ie/bb802730.aspx).

You can work with the UrlActions to redirect to a ASHX handler or _layouts page and open the file.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.