1

I have written below code to provision ContentBySearchWebPart.

ContentBySearchWebPart searchWebPart = new ContentBySearchWebPart();
searchWebPart.ID = "g_1ea2d77b_6b6c_4aca_b78f_3165a78884a8";
searchWebPart.Title = "Test webpart";
searchWebPart.NumberOfItems = 4;
searchWebPart.ItemTemplateId = "~sitecollection/_catalogs/masterpage/Display Templates/Content Web Parts/Item_Picture3Lines.js";
searchWebPart.RenderTemplateId = "~sitecollection/_catalogs/masterpage/Display Templates/Content Web Parts/Control_ListWithPaging.js";

 var QueryString = new DataProviderScriptWebPart
 {
     PropertiesJson = searchWebPart.DataProviderJSON
 };
 QueryString.Properties["QueryTemplate"] = "(IsDocument:True OR contentclass:STS_ListItem) Path:{Site.URL}";
 searchWebPart.DataProviderJSON = QueryString.PropertiesJson; 

But it is not setting query property of the web part.

When i check the web part in edit mode and see the query , it takes the default query for the web part (Not setting the query which i am passing).

Can anyone please let me know the solution for this?

1

I finally got it working.

I had to set the SourceID property as well as QueryTemplate property.

Please find the working code as below:

 var contentBySearchQueryString = new DataProviderScriptWebPart
 {
     PropertiesJson = searchWebPart.DataProviderJSON
 };

 contentBySearchQueryString.Properties["SourceID"] = "8413cd39-2156-4e00-b54d-11efd9abdb89";
 contentBySearchQueryString.Properties["QueryTemplate"] = "(IsDocument:True OR contentclass:STS_ListItem) Path:{Site.URL}";

 searchWebPart.DataProviderJSON = contentBySearchQueryString.PropertiesJson;

The source ID is the result source for the query which is "Local SharePoint Results".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.