Calling a function from SP.JS (Open word document directly using client application)


When calling a function from SP.JS during the page load, just make sure that you allow the page to load before you call any method, otherwise you will get undefined error.

in my case, I was trying to open a word document using the function “editDocumentWithProgID2” defined in SP.JS as below:

<script type="text/javascript">

   $(function () {

        ExecuteOrDelayUntilScriptLoaded(LoadDoc, "sp.js");

        function LoadDoc() {

            JSRequest.EnsureSetup();
            var file2Open = JSRequest.QueryString["file2Open"];
            var baseUrl = JSRequest.QueryString["return2address"];
            if (typeof file2Open != "undefined" && file2Open != "") {

                var host = location.protocol + "//" + location.host;
                var docUrl = file2Open.replace(host, "");
                var i = file2Open.lastIndexOf("/");
                var listUrl = file2Open.substr(0, i);
                editDocumentWithProgID2(docUrl, ”, ‘SharePoint.OpenDocuments’, ‘0’, baseUrl, ‘0’);
                location.href = listUrl;
            }

        }
    });
   
</script>

without the document ready check this will complain that the “editDocumentWithProgID2” is undefined.

Btw, make sure that these two lines are present in your masterpage:

 

<SharePoint:ScriptLink Language="javascript" runat="server" Name="SP.js" Localizable="false" LoadAfterUI="true" OnDemand="true" />
        <SharePoint:ScriptLink Language="javascript" runat="server" Name="SP.Runtime.js" Localizable="false" LoadAfterUI="true" OnDemand="true" />