Request

Response

    Search in Custom UI

    This tutorial describes how to locate parts of a model using the Search extension with a custom-built UI. We will use the JavaScript library to access the search capabilities.

    For details about using the Search extension with the default built-in Viewer UI, see the Search in Default UI tutorial.

    Note that this tutorial focuses on the Search extension and does not describe how to style your UI.

    Before You Begin

    • Register an app.
    • Acquire an OAuth token with viewables:read and data:write scopes.
    • Upload a file to the Forge Object Storage Service (OSS), as described in the File Upload tutorial.
    • Prepare a source file for viewing by encoding the source URN to base64 format and translating the file to SVF format, as described in the Prepare a File for the Viewer tutorial.
    • Acquire a new OAuth token with ONLY viewables:read scope so that it can pass to the front-end JavaScript client.

    Step 1: Prepare the Viewer HTML

    Create a file called in-viewer-search.html, and copy in the following code.

    <html>
      <head>
        <meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1, user-scalable=no" />
    
        <!-- The Viewer CSS -->
        <link rel="stylesheet" href="https://developer.api.autodesk.com/modelderivative/v2/viewers/3.*/style.min.css" type="text/css">
        <link rel="stylesheet" href="https://developer.api.autodesk.com/modelderivative/v2/viewers/3.*/A360.css" type="text/css">
    
        <!-- Developer CSS -->
        <style>
            #in-viewer-search {
                position: absolute;
                left: 283px;
                color: white;
                background-color: black;
                opacity: 0.5;
                padding: 20px;
                height: 445px;
                overflow: auto;
            }
    
            .hidden {
                display: none;
            }
    
            .result-block {
                padding: 5px;
            }
    
            .load-more {
                cursor: pointer;
            }
    
            #in-viewer-search ul.result-list {
                padding-left: 5px;
            }
        </style>
      </head>
      <body>
        <div id="MyViewerDiv"></div>
    
        <div id="in-viewer-search">
            <input type="search" disabled="true" id="searchbar" /> <input type="button" disabled="true" id="search" value="search" onclick="search()" />
            <div id="this-view" class="result-block">
                <div id="this-view-no-res" class="hidden">No results for loaded model search.</div>
                <div id="this-view-results" class="hidden">
                    <label id="qtyv"></label><label> results for loaded model search</label>
                    <ul id="this-view-res" class="result-list">
                    </ul>
                </div>
            </div>
            <div id="this-item" class="result-block">
                <div id="this-item-no-res" class="hidden">No results for the related items.</div>
                <div id="this-item-results-main" class="hidden">
                    <label id="qtyi"></label><label> results for related items search</label>
                    <div id="this-item-results" class="result-list"></div>
                </div>
            </div>
        </div>
    
        <!-- The Viewer JS -->
        <script src="https://developer.api.autodesk.com/modelderivative/v2/viewers/3.*/three.min.js"></script>
        <script src="https://developer.api.autodesk.com/modelderivative/v2/viewers/3.*/Autodesk360App.js"></script>
    
        <!-- Developer JS -->
        <script>
          var viewerApp;
          var options = {
            env: 'AutodeskProduction',
            accessToken: '<YOUR_APPLICATION_TOKEN>'
          };
    
          var documentId = 'urn:<YOUR_URN_ID>';
          Autodesk.Viewing.Initializer(options, onInitialized);
    
          function onInitialized() {
              var config = {};
              config.extensions = ["Autodesk.InViewerSearch"]; //load in-viewer-search
              config.inViewerSearchConfig = {
                  uiEnabled: false,
                  relatedItemsTab:{
                      enabled: true,  //If false, the tab is hidden.
                      displayName: 'This Item',
                      pageSize: 20
                  },
                  loadedModelTab: {
                      enabled: true,  //If false, the tab is hidden.
                      displayName: 'This View',
                      pageSize: 50
                  }
              };
              viewerApp = new Autodesk.Viewing.A360ViewingApplication('MyViewerDiv', options);
              viewerApp.registerViewer(viewerApp.k3D, Autodesk.Viewing.GuiViewer3D, config);
              viewerApp.loadDocumentWithItemAndObject(documentId);
          }
    
          /* add custom script function here */
    
        </script>
      </body>
    </html>
    
    Show More

    Replace YOUR_APPLICATION_TOKEN and YOUR_URN_ID with the application token generated when the app was created, and the base64 encoded source URN.

    Note that uiEnabled is set to false to disable the UI for the inViewerSearch object. See the JavaScript Library for details about other configurable options for the Search extenstion.

    Step 2: Load the Extension

    Instruct ViewingApplication to load the inViewerSearch extension.

    var api = viewerApp.getCurrentViewer().getExtension("Autodesk.InViewerSearch");
    

    Step 3: Wait for the Extension to Initialize

    Add a listener to determine when the inViewerSearch extension is initialized.

    inViewerSearch = viewer.getExtension("Autodesk.InViewerSearch");
    
    inViewerSearch.addInitListener(function() {
        initialized = true;
        document.getElementById("searchbar").disabled = false;
        document.getElementById("search").disabled = false;
    });
    

    Step 5: Configure Multiple Pages of Results

    If the results do not fit on one page, you need to set up additional requests to retrieve the rest of the results.

    The following code sets up requests to retrieve results for the Current Model search.

    function loadMoreModelSearchResults(str, page) {
        inViewerSearch.searchModel(str, { page: page }, function(results) {
            var list = document.getElementById("this-view-res");
            list.lastChild.remove();
            document.getElementById("this-view-results").classList.remove("hidden");
            results.results.forEach(function(r) {
                var li = document.createElement("li");
                li.innerText = r.nodeName;
                list.appendChild(li);
            });
            if (results.moreResults) {
                var li = document.createElement("li");
                li.className = "load-more";
                li.innerText = "Load more...";
                li.onclick = function(){
                    loadMoreModelSearchResults(str, page+1);
                };
                list.appendChild(li);
            }
        });
    }
    
    Show More

    The following code sets up requests to retrieve results for the Document search.

    function loadMoreDocumentSearchResults(str, page, geom) {
        inViewerSearch.searchDocument(str, {page: page, geometryId: geom}, function(resultsItem) {
            var list = document.getElementById("geometry_" + geom);
            list.removeChild(list.lastChild);
            resultsItem.results.forEach(function(r) {
                r.results.forEach(function(i) {
                    var li = document.createElement("li");
                    li.innerText = i.nodeName;
                    list.appendChild(li);
                });
                if (r.moreResults) {
                    var li = document.createElement("li");
                    li.className = "load-more";
                    li.innerText = "Load more...";
                    li.onclick = function() {
                        loadMoreDocumentSearchResults(str, page+1, geom);
                    };
                    list.appendChild(li);
                }
            });
        });
    }
    
    Show More

    For details about understanding the search results, see the Search in Default UI tutorial.

     
    ______
    icon-svg-close-thick

    Cookie preferences

    Your privacy is important to us and so is an optimal experience. To help us customize information and build applications, we collect data about your use of this site.

    May we collect and use your data?

    Learn more about the Third Party Services we use and our Privacy Statement.

    Strictly necessary – required for our site to work and to provide services to you

    These cookies allow us to record your preferences or login information, respond to your requests or fulfill items in your shopping cart.

    Improve your experience – allows us to show you what is relevant to you

    These cookies enable us to provide enhanced functionality and personalization. They may be set by us or by third party providers whose services we use to deliver information and experiences tailored to you. If you do not allow these cookies, some or all of these services may not be available for you.

    Customize your advertising – permits us to offer targeted advertising to you

    These cookies collect data about you based on your activities and interests in order to show you relevant ads and to track effectiveness. By collecting this data, the ads you see will be more tailored to your interests. If you do not allow these cookies, you will experience less targeted advertising.

    icon-svg-close-thick

    THIRD PARTY SERVICES

    Learn more about the Third-Party Services we use in each category, and how we use the data we collect from you online.

    icon-svg-hide-thick

    icon-svg-show-thick

    Strictly necessary – required for our site to work and to provide services to you

    Qualtrics
    W
    Akamai mPulse
    W
    Digital River
    W
    Dynatrace
    W
    Khoros
    W
    Launch Darkly
    W
    New Relic
    W
    Salesforce Live Agent
    W
    Wistia
    W
    Tealium
    W
    Upsellit
    W
    CJ Affiliates
    W
    Commission Factory
    W
    Google Analytics (Strictly Necessary)
    W
    Typepad Stats
    W
    Geo Targetly
    W
    SpeedCurve
    W
    Qualified
    #

    icon-svg-hide-thick

    icon-svg-show-thick

    Improve your experience – allows us to show you what is relevant to you

    Google Optimize
    W
    ClickTale
    W
    OneSignal
    W
    Optimizely
    W
    Amplitude
    W
    Snowplow
    W
    UserVoice
    W
    Clearbit
    #
    YouTube
    #

    icon-svg-hide-thick

    icon-svg-show-thick

    Customize your advertising – permits us to offer targeted advertising to you

    Adobe Analytics
    W
    Google Analytics (Web Analytics)
    W
    AdWords
    W
    Marketo
    W
    Doubleclick
    W
    HubSpot
    W
    Twitter
    W
    Facebook
    W
    LinkedIn
    W
    Yahoo! Japan
    W
    Naver
    W
    Quantcast
    W
    Call Tracking
    W
    Wunderkind
    W
    ADC Media
    W
    AgrantSEM
    W
    Bidtellect
    W
    Bing
    W
    G2Crowd
    W
    NMPI Display
    W
    VK
    W
    Adobe Target
    W
    Google Analytics (Advertising)
    W
    Trendkite
    W
    Hotjar
    W
    6 Sense
    W
    Terminus
    W
    StackAdapt
    W
    The Trade Desk
    W
    RollWorks
    W

    Are you sure you want a less customized experience?

    We can access your data only if you select "yes" for the categories on the previous screen. This lets us tailor our marketing so that it's more relevant for you. You can change your settings at any time by visiting our privacy statement

    Your experience. Your choice.

    We care about your privacy. The data we collect helps us understand how you use our products, what information you might be interested in, and what we can improve to make your engagement with Autodesk more rewarding.

    May we collect and use your data to tailor your experience?

    Explore the benefits of a customized experience by managing your privacy settings for this site or visit our Privacy Statement to learn more about your options.
    Version 3