0

I'm new to SharePoint and JSOM and i'm trying to build up a search using JSOM.

and i tried below code snippets

I FOLLOWED FOLLOWING BLOG http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2013/04/18/how-to-query-search-with-the-sharepoint-2013-javascript-client-object-model.aspx

Is there any search.js file i should linked? if so what would be the link?? However these are the links i added

1st step: Create a page and added following HTML

It has search button : #searchButton and text box : #searchTextBox

i linked JQuery and abc.js file succesfully

and added this JS code to abc.js file

'use strict';

var results;

var context = SP.ClientContext.get_current();
var user = context.get_web().get_currentUser();


$(document).ready(function () {

    $("#searchButton").click(function () {

       alert('check1');
        var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(context);
        keywordQuery.set_queryText($("#searchTextBox").val());

       alert('check2');

        var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(context);
        results = searchExecutor.executeQuery(keywordQuery);

        context.executeQueryAsync(onQuerySuccess, onQueryFail)
    });
});


function onQuerySuccess() {
    $("#resultsDiv").append('<table>');

    $.each(results.m_value.ResultTables[0].ResultRows, function () {
        $("#resultsDiv").append('<tr>');
        $("#resultsDiv").append('<td>' + this.Title + '</td>');
        $("#resultsDiv").append('<td>' + this.Author + '</td>');
        $("#resultsDiv").append('<td>' + this.Write + '</td>');
        $("#resultsDiv").append('<td>' + this.Path + '</td>');
        $("#resultsDiv").append('</tr>');
    });

    $("#resultsDiv").append('</table>');
}

function onQueryFail(sender, args) {
    alert('Query failed. Error:' + args.get_message());
}

In this code check1 alert is coming but not the check2 alert.. Whats the wrong with this..?

8

For quite some time, I used to get around a similar issue in regards to the Search API. Over and over again I was troubled with the following error-message:

Object doesn't support this action

Try to add a reference/key to Microsoft.SharePoint.Client.Search.Query.KeywordQuery instead of Microsoft.SharePoint.Client.Search.Query.

At least that worked for me.

Working sample below:

function Search() {
    var d = $.Deferred();

    SP.SOD.executeFunc("SP.js", "SP.ClientContext", function () {
        SP.SOD.executeFunc("SP.Search.js", "Microsoft.SharePoint.Client.Search.Query.KeywordQuery", function () {
            var sContext = SP.ClientContext.get_current();
            var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(sContext);
            keywordQuery.set_trimDuplicates(false);
            keywordQuery.set_queryText("*");
            var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(sContext);
            results = searchExecutor.executeQuery(keywordQuery);
            sContext.executeQueryAsync(
                function () {
                    d.resolve(results);
                },
                function (err) {
                    d.reject(null);
                }
            );
        });
    });

    return d.promise();
}
| improve this answer | |
  • The code snippet working like a charm, thanks, very helpful – Jerry_MSFT Sep 29 '17 at 3:11
2

You are most probably missing the reference to SP.Search.js file. Add following code to add reference to it:

<SharePoint:ScriptLink language="javascript" name="SP.Search.js" OnDemand="true" runat="server" Localizable="false">
</SharePoint:ScriptLink>

Then in the $(document).ready( method call it using SP.SOD.executeFunc function. Example: SP.SOD.executeFunc('SP.Search.js', 'Microsoft.SharePoint.Client.Search.Query', SetSearchSettings); - See more at: http://onlinecoder.blogspot.in/2014/03/loading-javascript-libraries-only-when.html#sthash.k2B2SvJd.dpuf

UPDATE

Based on the comments, it also looks like sp.js file is also not loaded. Add these script references also:

<SharePoint:ScriptLink Name="MicrosoftAjax.js" runat="server" Defer="False" Localizable="false"/>
<SharePoint:ScriptLink Name="SP.core.js" runat="server" Defer="False" Localizable="false"/>
<SharePoint:ScriptLink Name="SP.js" runat="server" Defer="True" Localizable="false"/>
| improve this answer | |
  • Hi, Thank you very much for repling me, I ADDED following code and as well as i added sp.search.js too.. SP.SOD.executeFunc('SP.Search.js', 'Microsoft.SharePoint.Client.Search.Query', SetSearchSettings); looks like it has some issue, after that code does not getting executed. Any idea, really appreciate ur help – SharePoint Learner Feb 2 '15 at 8:38
  • it looks code does not work after this tooo.. any libraries to be loaded to run following two code lines? var context = SP.ClientContext.get_current(); var user = context.get_web().get_currentUser(); – SharePoint Learner Feb 2 '15 at 8:48
  • Keep the code in abc.js as you had earlier and only add <SharePoint:ScriptLink language="javascript" name="SP.Search.js" runat="server"> </SharePoint:ScriptLink> to the .aspx page – Nadeem Yousuf-AIS Feb 2 '15 at 9:00
  • i just followed your instruction, and i put two alerts between those two statesments alert('above'); var results; var context = SP.ClientContext.get_current(); var user = context.get_web().get_currentUser(); alert('below'); above alert is coming up but not the below.. – SharePoint Learner Feb 2 '15 at 9:48
  • can you paste the whole code of your .aspx page in the question? – Nadeem Yousuf-AIS Feb 2 '15 at 9:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.