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.
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.
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.
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.