Domanda

We need to access a sharepoint 2007 site from javascript. Basically we need to use the search.asmx service but since that does not support rest nor jsonp it can't be used directly.

The original plan was to make custom wcf service with support for rest and jsonp. This was a small undertaking but when I gave the service to the Sharepoint guys none of them could package it to a wsp package for installation in sharepoint 2007 and get it working.

According to this question Rest Webservices for Sharepoint 2007 this might not be so easy and a httpmodule is required for rest-typed urls. The other idea about running it as a standalone app might not be enough since I think that the service needs access to SPContext.

Would it be possible to just create an Application Page and there in the code behind override Render, clear the output buffer, change mime type and render the json-serialized data? So the url would be http://spsite/mycustomquery.aspx?q=mysearchtext&start=0&count=200&callback=mycallbackfunction.

An application page would at least support Get but does it have access to SPContext?

Here is the wcf service I started with.

Contract

[ServiceContract]
public interface IRestSPQuery
{
    [OperationContract]
    [WebGet(UriTemplate = "query/{queryText}/{startAt}/{count}?callback={callback}", ResponseFormat = WebMessageFormat.Json)]
    [JSONPBehavior(callback = "callback")]
    ResultTable Query(string queryText, string startAt, string count, string callback);
}

Implementation

    public ResultTable Query(string queryText, string startAt, string count, string callback)
    {
        //http://sharepointsite/_vit_bin/CustomQuery/RestSPQuery.svc/Query/searchtext/0/200?callback=myfunction

        KeywordQuery keywordQuery = new KeywordQuery(SPContext.Current.Site);
        keywordQuery.StartRow = startAt;
        keywordQuery.RowLimit = count;
        keywordQuery.SortList.Add("Rank", SortDirection.Descending);
        keywordQuery.QueryText = queryText;
        ResultTableCollection searchResults = keywordQuery.Execute();
        ResultTable relevantResultsTable = searchResults[ResultType.RelevantResults];
        return relevantResultsTable;
    }
È stato utile?

Soluzione 2

I ended up creating a custom aspx page and override the Render method and there output json/jsonp and also change the content type to application/json.

The solution and a ready to deploy wsp-file can be found here http://www.filedropper.com/restqueryservice.

Altri suggerimenti

You could try adding an ".ashx" file to your solution that implements IHttpHandler. According to this blog article you can do it by adding an Application Page to your solution but save it as a ".ashx" extension. The article is written for SharePoint 2010 but you will have to check if it works for 2007. Following the rest of the article you should be able to set it up for REST/JSONP.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top