Partager via


Content Search Web Part "Hello World"

"Hello world"?

"Hello world" refers to a program that prints out the text "Hello world" to an output device.

In Basic we would simply write

PRINT "Hello, world!"

SharePoint CSWP "Hello world"?

So, what is the "Hello World" program for the Content Search Web Part (CSWP) in SharePoint?

Well, the purpose of the CSWP is to display a web page by rendering a predefined embedded search query. It is possible to get the web part to display "Hello world" without any query at all since the CSWP can display its own title, but we'll use the CSWP according to its intention. Thus, here is what we will do:

  1. Create a Word document with title "Hello world"
  2. Store that document in SharePoint
  3. Create a SharePoint web page
    1. Insert the CSWP to that page
    2. Configure the CSWP with a query to match our Word document
    3. Choose a display template to show one document only (the control display template)
    4. Choose another display template to show the title of the result
  4. Load the page and look for "Hello world"

Create the document

First create a document locally in MS Word (or another file format with text):

Then upload the file to a SharePoint document library, either via drag'n'drop or the menu inside SharePoint, and set its properties:

That completes the first part of this "Hello world", getting content into SharePoint.

Create the page with a Content Search Web Part

The next task is to create a page with the CSWP, and set an appropriate query and rendering for it.

First create a new SharePoint page. This is done from the Page menu of SharePoint:

Give the page a nice name:

Notice the URL for this page. Then add a CSWP to the page from the menu:

Choose "Edit Web Part" on the right-hand side of the newly inserted web part to edit the CSWP:

Press the "Change Query" button to choose query. Click "advanced mode" and set query text to title:"Hello world" to search for the document we just uploaded:

You can test the query with the test button or the test tab card. Press OK to come back to the web part properties. Then choose how the CSWP shall render its results by setting the display templates as shown here:

Press OK, to come back to the page:

Oh, if it looks like this you have been quicker to edit the page than search has been to index the uploaded page. Just wait a few minutes and the result will show up.

Save the page and you are done with your "Hello world" page with the Content Search Web Part. Congratulations, it will look something like this:

Hope this was a somewhat useful (though cumbersome) "Hello world" sample of the Content Search Web Part! When you have completed this "Hello world" page you can easily extend it using all the power of search to amaze your colleagues with cool pages that always have up to date content!

Comments