Use SharePoint Searchservice to get content from public site

How to crawl a public website content into a SharePoint site?

Just now i developed this module, the scenario is:

I have a page in SharePoint and I want to search content from other public websites and want to show the result in a data source into my Page.

First you need to configure search service.
Create scope, each scope should be used for an individual website url, so that we can filter result on base of scope in our query.

Now once you are done with configuration and settings. Lets start our coding in Visual Studio.

Public website as a content source in SharePoint search

 First add the reference of below service into your project.

http://<sharepointsite>/_vti_bin/search.asmx

C# Code:

  

protected void btnSearch_Click(object sender, EventArgs e)
    {
        SearchResult();
    }
    private string GetXMLString()
    {
        //StringBuilder xmlString = new StringBuilder(“<QueryPacket xmlns=’urn:Microsoft.Search.Query’><Query><SupportedFormats><Format revision=’1′> urn:Microsoft.Search.Response.Document:Document</Format></SupportedFormats><Range><Count>1000</Count></Range><Context><QueryText language=’en-US’ type=’STRING’>”);
        //xmlString.Append(queryTextBox.Text);
        //xmlString.Append(“</QueryText></Context></Query></QueryPacket>”);
        string scope = string.Empty;
        if (ddlSite.SelectedIndex == 0)
        {
            scope = “Website1”; //Scope Name created in Search Service
        }

        else

        {
            scope = “Website2”;//Scope Name created in Search Service
        }
        StringBuilder xmlString = new StringBuilder(“<QueryPacket xmlns=’urn:Microsoft.Search.Query’ Revision=’1′>” +
 “<Query><SupportedFormats><Format>urn:Microsoft.Search.Response.Document.Document</Format>” +
   “</SupportedFormats><Range><Count>1000</Count></Range><Context>” +
     “<QueryText language=’en-US’ type=’MSSQLFT’>SELECT Title, Path, Description, Write, Rank, Size,HitHighlightedSummary FROM Scope() WHERE FREETEXT (‘” + queryTextBox.Text + “‘) AND ((“SCOPE” = ‘” + scope + “‘))</QueryText>” +
   “</Context></Query></QueryPacket>”);

        return xmlString.ToString();

    }

    private void SearchResult()

    {
        try
        {
            // Instantiate the Web service.
            SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    resultsLabel.Text = string.Empty;
                    QueryWebServiceProxy.QueryService queryService = new QueryWebServiceProxy.QueryService();

                  

                    //queryService.Credentials = System.Net.CredentialCache.DefaultCredentials;
                    //above line did not work for me, then i used below line.
                    NetworkCredential credentials = new NetworkCredential(“username”, “password”, “domain”);
                    queryService.PreAuthenticate = true;
                    queryService.Url = “http://<sharepointsite>/_vti_bin/search.asmx”;
                    queryService.UseDefaultCredentials = false;
                    //queryService.useDefaultCredentialsSetExplicitly = true;
                    queryService.Credentials = credentials;                    
                    System.Data.DataSet queryResults = queryService.QueryEx(GetXMLString());                   
                    resultsGrid.DataSource = queryResults.Tables[0];
                     
                    resultsGrid.DataBind();
                });
        }
        catch (Exception ex)
        {
            resultsLabel.Text = ex.ToString();
            resultsLabel.ForeColor = System.Drawing.Color.Red;
            resultsLabel.Text = “Search is unavailable!”;
        }
    }

    protected void resultsGrid_PreRender(object sender, EventArgs e)

    {
        resultsGrid.UseAccessibleHeader = false;
         
    }



ASPX Code:


<div>
            <asp:TextBox ID=”queryTextBox” runat=”server”></asp:TextBox>
            <asp:DropDownList ID=”ddlSite” runat=”server”>
                <asp:ListItem Value=”Websiteurl1″>Website1</asp:ListItem>
                <asp:ListItem Value=”Websiteurl2″>Website2</asp:ListItem>
            </asp:DropDownList><br />

            <br />

            <asp:Button ID=”btnSearch” runat=”server” Text=”Search” OnClick=”btnSearch_Click” Width=”76px” />
        </div>
        <br />
        <asp:Label ID=”resultsLabel” runat=”server” Text=””></asp:Label>
        <br />
        <asp:GridView ID=”resultsGrid” runat=”server” OnPreRender=”resultsGrid_PreRender” EnableModelValidation=”True” EmptyDataText=”No Result found” AutoGenerateColumns=”false”>

            <Columns>

                <asp:TemplateField>
                    <ItemTemplate>

                        <span style=”color: blue; font-weight: bold”><a target=”_blank” href='<%# Eval(“Path”) %>’><%# Eval(“Title”) %></a></span>

                        <p style=”color: black;”><%# Eval(“HitHighlightedSummary”) %></p>
                        <span style=”color: green;”><%# Eval(“Path”) %></span>
                        <br />
                        <hr />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>

        </asp:GridView>

5 thoughts on “Use SharePoint Searchservice to get content from public site”

Leave a Comment

RSS
YouTube
YouTube
Instagram