Add search web part to page layout
Today I tried to add a search result web part to a page layout. Initially I thought to do this the same way as I would with an content editor web part.
So first the steps on how I would add a content editor web part to my page layout.
I find the following section in my page layout:
<WebPartPages:WebPartZone runat=”server” Title=”My zone” ID=”MyZone”><ZoneTemplate>
</ZoneTemplate></WebPartPages:WebPartZone>
then within the zone I add within the ZoneTemplate a bit of web part code:
<WebPartPages:ContentEditorWebPart runat=”server” __MarkupType=”xmlmarkup” WebPart=”true” __WebPartId=”{359811A8-E5ED-4B67-821A-56D65725FDBC}” >
WebPart xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns=”http://schemas.microsoft.com/WebPart/v2″>
<Title>My web part</Title>
<FrameType>None</FrameType>
<Description>Allows authors to enter rich text content.</Description>
<IsIncluded>true</IsIncluded>
…
<ID>g_59d8779b_5895_4724_9c3e_62db1dd58558</ID>
<ContentLink xmlns=”http://schemas.microsoft.com/WebPart/v2/ContentEditor” />
<Content xmlns=”http://schemas.microsoft.com/WebPart/v2/ContentEditor”><![CDATA[ ]]></Content>
<PartStorage xmlns=”http://schemas.microsoft.com/WebPart/v2/ContentEditor” />
</WebPart>
</WebPartPages:ContentEditorWebPart>
In the above the <WebPart> … </WebPart> xml is the same as when you export a web part on a SharePoint page.
Now the same with my search web part. I’m quite quickly getting errors when I tried to create pages with my page layout. As I’m working with office 365 the ULS logs are not accessible and a correlation id is less than no use to me.
My Solution
Back to added a search result web part. First of all I had to find out where the Search result web part comes form. When I exported the web part the name of the search results web part looked like ResultScriptWebPart. So I looked that up using Bing and I got to the article giving me the dll name.
Then I added the following line near the top of my page layout:
<%@ Register Tagprefix=”SearchWebControls” Namespace=”Microsoft.Office.Server.Search.WebControls” Assembly=”Microsoft.Office.Server.Search, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %>
Then I found the <ZoneTemplate> that I wanted to add my search web part to:
<WebPartPages:WebPartZone runat=”server” Title=”Left” ID=”Left”>
<ZoneTemplate>
<SearchWebControls:ResultScriptWebPart runat=”server”
DataProviderJSON=”{"QueryGroupName":"Default","QueryPropertiesTemplateUrl":"querygroup://webroot/Pages/Dummysearch.aspx?groupname=Default","IgnoreQueryPropertiesTemplateUrl":false,"SourceID":"5ce35ddf-d563-4d32-b850-c45760c9a3bf","SourceName":"Product Catalog","SourceLevel":"Ssa","CollapseSpecification":"","QueryTemplate":"{searchboxquery}","FallbackSort":null,"FallbackSortJson":"null","RankRules":null,"RankRulesJson":"null","AsynchronousResultRetrieval":false,"SendContentBeforeQuery":true,"BatchClientQuery":true,"FallbackLanguage":-1,"FallbackRankingModelID":"","EnableStemming":true,"EnablePhonetic":false,"EnableNicknames":false,"EnableInterleaving":true,"EnableQueryRules":true,"EnableOrderingHitHighlightedProperty":false,"HitHighlightedMultivaluePropertyLimit":-1,"IgnoreContextualScope":false,"ScopeResultsToCurrentSite":false,"TrimDuplicates":true,"Properties":{},"PropertiesJson":"{}","ClientType":"","UpdateAjaxNavigate":true,"SummaryLength":180,"DesiredSnippetLength":90,"PersonalizedQuery":false,"FallbackRefinementFilters":null,"IgnoreStaleServerQuery":false,"RenderTemplateId":"DefaultDataProvider","AlternateErrorMessage":null,"Title":""}”
BypassResultTypes=”True”
ItemTemplateId=”~sitecollection/_catalogs/masterpage/Display Templates/Search/_Item_Product.js”
ItemBodyTemplateId=”~sitecollection/_catalogs/masterpage/Display Templates/Search/Item_CommonItem_Body.js”
ResultTypeId=””
SelectedPropertiesJson=”["Title","Path","Description","EditorOWSUSER","LastModifiedTime","CollapsingStatus","DocId","HitHighlightedSummary","HitHighlightedProperties","FileExtension","ViewsLifeTime","ParentLink","FileType","IsContainer","SecondaryFileExtension","DisplayAuthor"]”
HitHighlightedPropertiesJson=”["Title","Path","Author","SectionNames","SiteDescription"]”
AvailableSortsJson=”[{"name":"Relevance","sorts":[]},{"name":"Date(Newest)","sorts":[{"p":"Write","d":1}]},{"name":"Date(Oldest)","sorts":[{"p":"Write","d":0}]},{"name":"Lifetime Views","sorts":[{"p":"ViewsLifeTime","d":1}]},{"name":"Recent Views","sorts":[{"p":"ViewsRecent","d":1}]}]”
ShowLanguageOptions=”False”
ShowPaging=”False”
ShowBestBets=”False”
ShowPersonalFavorites=”False”
ShowDidYouMean=”False”
ShowAdvancedLink=”False”
ShowPreferencesLink=”False”
ShowResultCount=”False”
PreloadedItemTemplateIdsJson=”["~sitecollection/_catalogs/masterpage/Display Templates/Search/Group_Default.js","~sitecollection/_catalogs/masterpage/Display Templates/Search/Item_Default.js","~sitecollection/_catalogs/masterpage/Display Templates/Search/Item_Site.js","~sitecollection/_catalogs/masterpage/Display Templates/Search/Item_Word.js","~sitecollection/_catalogs/masterpage/Display Templates/Search/Item_PowerPoint.js","~sitecollection/_catalogs/masterpage/Display Templates/Search/Item_Person_CompactHorizontal.js","~sitecollection/_catalogs/masterpage/Display Templates/Search/Item_BestBet.js","~sitecollection/_catalogs/masterpage/Display Templates/Search/Item_WebPage.js"]”
ShowAlertMe=”False”
QueryGroupName=”Default”
RenderTemplateId=”~sitecollection/_catalogs/masterpage/Display Templates/Search/Control_SearchResults.js”
StatesJson=”{}”
ServerIncludeScriptsJson=”null”
Title=”Search Web part”
FrameType=”None”
SuppressWebPartChrome=”False”
Description=”Displays the search results and the properties associated with them.”
IsIncluded=”True”
ZoneID=”Left”
PartOrder=”1″
FrameState=”Normal”
AllowRemove=”True”
AllowZoneChange=”True”
AllowMinimize=”True”
AllowConnect=”True”
AllowEdit=”True”
AllowHide=”True”
IsVisible=”True”
DetailLink=””
HelpLink=””
HelpMode=”Modeless”
Dir=”Default”
PartImageSmall=””
MissingAssembly=”Cannot import this Web Part.”
PartImageLarge=””
IsIncludedFilter=””
ExportControlledProperties=”True”
ConnectionID=”00000000-0000-0000-0000-000000000000″
ID=”g_ad1f2313_25f2_25b2_1241_b87395278af1″
ChromeType=”None”
ExportMode=”All”
__MarkupType=”vsattributemarkup”
__WebPartId=”{AD1F2313_25F2_25B2_1241_B87395278AF1}”
WebPart=”true”
Height=””
Width=””>
</SearchWebControls:ResultScriptWebPart>
</ZoneTemplate></WebPartPages:WebPartZone>
Ok, there are many properties that can be set within this web part. The above is only a small selection.