Fixing URL issue in FullTextSqlQuery search when same web application is used in different zones

In SharePoint we can create a web application and extend the same web application for different zones, the content will be remain same but the url will be different for each zone.

Let’s say I am having a web application on port 2020 and the url is   http://test:2020/ .

Now I want to use this web application on internet zone . So I can extend the web application with url http://test:2021/..

In this we don’t have any out-of-the-box webpart for search .We can create our own custom webpart to show the search results based on the search keyword. So I have created a webpart and used FullTextSqlQuery to perform the search .

using (SPSite site = new SPSite(url))

{

using (FullTextSqlQuery query = new FullTextSqlQuery(site))

{

ResultTableCollection results = null;

DataTable searchDataTable = null;

string queryText = "select Title, Path, Filename,SiteName,ContentType,HitHighLightedSummary,created,Author from scope() where ";

queryText += " FREETEXT(*, '" + searchString + "')";

queryText += " AND SITE='" + site.OpenWeb().Url + "'";

queryText += " AND SiteTitle='" + site.OpenWeb().Title ;

queryText += " ORDER By RANK DESC";

query.QueryText = queryText;

query.ResultTypes = ResultType.RelevantResults;

results = query.Execute();

}

}

Now when I perform a search on http://test:2020/ it show me the results with url pointing to the default url zone (http://test:2020/) which is correct . But when I perform a search on http://test:2021/

it show me the results pointing to the http://test:2020/ which is incorrect . It should point to http://test:2021/ .

But when i used the default search present in wss on http://test:2021/ it show me the results with the url http://test:2021/. So there must be something missing in my code.

To get the correct result we need to set the ‘SiteContext’ property of the query i.e

SPSite site = SPContext.Current.Site

query.SiteContext = new Uri(site.Url);

results = query.Execute();

 
Now when i perform the search on any zone it gives me the desired results

150 150 Burnignorance | Where Minds Meet And Sparks Fly!