locked
Reusing javascript file across display templates in different site collections RRS feed

  • Question

  • Is it possible to reuse the same javascript file across display templates in different site collections? For example, I have a search center site at http://xyz.com/search and a root site at http://xyz.com. These sites have their own set of display templates located at /_catalogs/masterpage/display templates.

    I have a javascript that I would like to reuse in display templates in both search site and root site. I am using the following code to include javascript file:

    <body>
        <script type="text/javascript">
            RegisterSod("customtransform.js", _spPageContextInfo.webAbsoluteUrl + "/Style%20Library/People/SearchBox_Functions.js");
            EnsureScriptFunc("customtransform.js", null, function () { });
        </script>
        <div id="Control_SearchBox">

    Not sure how I can reuse the javascript file. Can someone provide pointers?

    Thank you!

    Monday, April 22, 2019 9:59 PM

Answers

  • Hi,

    For reuse script file, suggest to place into SharePoint layout folder and reference like this:

    SP.SOD.registerSod('customtransform.js',"/_layouts/15/SearchBox_Functions.js");

    Layouts folder path:

    C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\LAYOUTS

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by diffident Wednesday, May 8, 2019 7:07 PM
    Tuesday, April 30, 2019 3:12 AM

All replies

  • You need to use SharePoint Hosted app (CSOM) to achieve this .  You need to use the SP.AppContextSite() method to map your root site collection context to the search center site.

    Please refer this https://social.msdn.microsoft.com/Forums/sharepoint/en-US/1b9ba2b2-a9a6-4a35-ad28-927ab0f94bd9/cross-site-collection-or-cross-web-application-call-in-csom?forum=sharepointdevelopment

    Monday, April 22, 2019 11:06 PM
  • Hi,

    You could simply upload the custom JavaScript file into the root site Style library and then reference the script file same using RegisterSod with _spPageContextInfo.webAbsoluteUrl in the root site display template:

      RegisterSod("customtransform.js", _spPageContextInfo.webAbsoluteUrl + "/Style%20Library/People/SearchBox_Functions.js");

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Tuesday, April 23, 2019 5:16 AM
  • Hi,

    You could simply upload the custom JavaScript file into the root site Style library and then reference the script file same using RegisterSod with _spPageContextInfo.webAbsoluteUrl in the root site display template:

      RegisterSod("customtransform.js", _spPageContextInfo.webAbsoluteUrl + "/Style%20Library/People/SearchBox_Functions.js");

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Thanks Jerry. But how about referencing the same script file (that is uploaded to root site style library) from search site?
    Tuesday, April 23, 2019 2:57 PM
  • Hi,

    Could use the server relative url for the JavaScript file in root site style library like this:

     RegisterSod('customtransform.js', '/Style Library/People/SearchBox_Functions.js');

    Reference:

    SP.SOD.registerSod(key, url) Method

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Wednesday, April 24, 2019 6:32 AM
  • That did not work. For the time being I am duplicating the script file across site collections. Not desirable but looking for better options.

    My goal is to be able to reuse script file.

    Monday, April 29, 2019 9:50 PM
  • Hi,

    For reuse script file, suggest to place into SharePoint layout folder and reference like this:

    SP.SOD.registerSod('customtransform.js',"/_layouts/15/SearchBox_Functions.js");

    Layouts folder path:

    C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\LAYOUTS

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by diffident Wednesday, May 8, 2019 7:07 PM
    Tuesday, April 30, 2019 3:12 AM