locked
Loading .js files RRS feed

  • Question

  • Hi guys,

    since I am quite new to Javascript development with SharePoint I have some questions to include .js files in my code. 

    Before I start: I'm not directly developing an app/add-in for SharePoint. Instead I upload some .js files and a html file in a doc library and include the .html file as a content editor web part in SharePoint to access the .js files (e.g. for some JSOM commands).

    In my first program I only needed the sp.js file since I only wanted to retrieve some ListInformations. For that I used the SP.SOD.executefunc command.

    In my second program I need some of the TaxonomyValues (a specific termset of the site). Now I am a little bit irritated what I have to load and how to do it. 

    If I am right I first have to register the .js files with the SP.SOD.registerSod function, right?
    And then I have to load them with SP.SOD.executefunc? Can I load more than one .js file with a single executefunc command?

    I also read that you can load .js files with "LoadSodByKey" and "SP.SOD.loadMultiple".

    Regardless of which way I choose, my second program doesn't work. You can see the code below. My console says "Cannot read TaxonomySession of undefined". So I assume there is a mistake in loading the .js files? Maybe someone can help me to understand the problem :-)

    <html>
    <head>
    <script type="text/javascript" src="jquery.js"></script> >
    <script 
        type="text/javascript" 
        src="sp.runtime.js">
    </script>
    <script 
        type="text/javascript" 
        src="sp.js">
    </script>
    <script 
        type="text/javascript" 
        src="init.js">
    </script>
    <script 
        type="text/javascript" 
        src="sp.taxonomy.js">
    </script>
    <script>
    function scriptLoad(){
       SP.SOD.registerSod('sp.taxonomy.js','https://mycompany.sharepoint.com/sites/Tesst/DokuTest/sp.taxonomy.js');
       LoadSodByKey("sp.taxonomy.js", Zugriff());
       SP.SOD.loadMultiple(['sp.runtime.js','sp.js','sp.taxonomy.js'],Zugriff());
    }
    
    
    function Zugriff (){
     var context = new SP.ClientContext('https://abb.sharepoint.com/sites/Tesst'); 
     var session = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
     var termStores = session.get_termStores();
     context.load(session);
     context.load(termStores);
      context.executeQueryAsync(
     function(){
      var termStoresEnum = termStores.getEnumerator();
      var termStores = "TermStores: /n";
      while (termStoresEnum.moveNext()){
       var currentTermStore = termStoresEnum.get_current();
       var termStoreID = currentTermStore.get_id();
       var termStoreName = currentTermStore.get_name();
       termStores += "Name: " + termStoreName+ " ID:" + termStoreID;
       alert (termStores);
       }
      }, function(){
      //failure loading termstores
      });
    }  
    </script>
    <title></title>
    </head>
    <body>
    <input type="button" name="clickme" value="Absenden" onclick="scriptLoad();">
    </body>
    </html>

    Best regards,

    André

    Friday, October 16, 2015 7:01 AM

Answers

  • Hi,

    We can use the following code snippet to achieve it.

    $(document).ready(function () {
    	var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
    
    	$.getScript(scriptbase + "SP.Runtime.js", function () {
    		$.getScript(scriptbase + "SP.js", function () {
    			$.getScript(scriptbase + "SP.Taxonomy.js", function () {
    				context = SP.ClientContext.get_current();
    
    				//Call your code here.
    
    				//getTermStores();
    			});
    		});
    	});
    });
    

    More information is here:

    https://cann0nf0dder.wordpress.com/2013/04/09/accessing-taxonomy-term-store-with-jsom/

    Best Regards,

    Dennis


    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    • Marked as answer by Aquen Friday, October 23, 2015 7:36 AM
    Thursday, October 22, 2015 9:24 AM