I used following code block within an event receiver to do the task. In this example my search query is such that all documents except .aspx web pages within the site are returned.
- var web = properties.Feature.Parent as SPWeb;
- //create search results webpart
- var resultsWebPart = new ResultScriptWebPart();
- resultsWebPart.Title = "Team Search Results";
- var querySettings = new DataProviderScriptWebPart
- {
- PropertiesJson = resultsWebPart.DataProviderJSON
- };
- //setting the search query text
- querySettings.Properties["QueryTemplate"] =
- "(IsDocument:True) Path:{Site.URL} -FileName:*.aspx";
- resultsWebPart.DataProviderJSON = querySettings.PropertiesJson;
- //add the search results webpart to the page
- var homePage = web.GetFile("SitePages/Home.aspx");
- using (var webPartManager = homePage.GetLimitedWebPartManager(PersonalizationScope.Shared))
- {
- webPartManager.AddWebPart(resultsWebPart, "Left", 0);
- homePage.Update();
- }
This will add the webpart with expected results as below
2 comments:
Hi,
I'm creating a page layout using Sharepoint Designer 2013. I'm adding content search webparts into the page layout and would like to add a page title to the query in the content search web part. This sounds a lot like the problem that you described on the post.
Can this be done for content search webpart also? Is it possible with plain ASPX or do I need Visual Studio?
Br,
Kati
i tried to find answer all day and this topic helped me to resolve my problem
Post a Comment