1

I did an export of the contentbysearchwebpart. I would like to create a new webpart with the same settings programmatically in c#. So I would like to set the property "DataProviderJSON". The json value in xml is:

{"QueryGroupName":"Default","QueryPropertiesTemplateUrl":"sitesearch://webroot","IgnoreQueryPropertiesTemplateUrl":false,"SourceID":"8413cd39-2156-4e00-b54d-11efd9abdb89","SourceName":"Local SharePoint Results","SourceLevel":"Ssa","CollapseSpecification":"","QueryTemplate":"{searchboxquery} contentclass:\"STS_Site\" ","FallbackSort":[{"p":"ViewsRecent","d":1}],"FallbackSortJson":"[{\"p\":\"ViewsRecent\",\"d\":1}]","RankRules":null,"RankRulesJson":"null","AsynchronousResultRetrieval":false,"SendContentBeforeQuery":true,"BatchClientQuery":true,"FallbackLanguage":-1,"FallbackRankingModelID":"","EnableStemming":true,"EnablePhonetic":false,"EnableNicknames":false,"EnableInterleaving":false,"EnableQueryRules":true,"EnableOrderingHitHighlightedProperty":false,"HitHighlightedMultivaluePropertyLimit":-1,"IgnoreContextualScope":true,"ScopeResultsToCurrentSite":false,"TrimDuplicates":false,"Properties":{"TryCache":true,"Scope":"{Site.URL}","UpdateLinksForCatalogItems":true,"EnableStacking":true,"ListId":"ecf7cef6-a31e-451c-b97a-fc2201bba642","ListItemId":134},"PropertiesJson":"{\"TryCache\":true,\"Scope\":\"{Site.URL}\",\"UpdateLinksForCatalogItems\":true,\"EnableStacking\":true,\"ListId\":\"ecf7cef6-a31e-451c-b97a-fc2201bba642\",\"ListItemId\":134}","ClientType":"ContentSearchRegular","UpdateAjaxNavigate":true,"SummaryLength":180,"DesiredSnippetLength":90,"PersonalizedQuery":false,"FallbackRefinementFilters":null,"IgnoreStaleServerQuery":false,"RenderTemplateId":"DefaultDataProvider","AlternateErrorMessage":null,"Title":""}

This is my current code:

ContentBySearchWebPart cbswp = new ContentBySearchWebPart();
                cbswp.Title = "Overzichtspagina";
                cbswp.ItemTemplateId = "~sitecollection/_catalogs/masterpage/Item_Landingspagina.js";
                cbswp.RenderTemplateId = "~sitecollection/_catalogs/masterpage/Control_Landingspagina.js";
                cbswp.DataProviderJSON = ???
                cbswp.ResultsPerPage = 10;
                cbswp.ShowViewDuplicates = false;
                cbswp.ChromeType = PartChromeType.None;
                webPartManager.AddWebPart(cbswp, "wpzSearchResult", 0);
                webPartManager.SaveChanges(cbswp);

What is the best practice to convert the json in xml format to a valid json string format?

2 Answers 2

0

I entered the json value in notepad++. And I installed the json viewer plugin. I check if the value format is OK. After this I add a "\" character before each " character. Now it is like this:

searchWp.DataProviderJSON = "{  \"QueryGroupName\": \"Default\",    \"QueryPropertiesTemplateUrl\": \"sitesearch://webroot\",   \"IgnoreQueryPropertiesTemplateUrl\": false,    \"SourceID\": \"8413cd39-2156-4e00-b54d-11efd9abdb89\", \"SourceName\": \"Local SharePoint Results\",   \"SourceLevel\": \"Ssa\",   \"CollapseSpecification\": \"\",    \"QueryTemplate\": \"{searchboxquery} contentclass:STS_Site Path:http://myCompany.nl/* \", \"FallbackSort\": [{     \"p\": \"ViewsRecent\",     \"d\": 1    }], \"TrimDuplicates\": false, \"SummaryLength\": 180,\"DesiredSnippetLength\": 90 }";
0

I also like using this format.

string s = @" {'QueryGroupName':'Default','QueryPropertiesTemplateUrl':'sitesearch://webroot','IgnoreQueryPropertiesTemplateUrl':false,'SourceID':'8413cd39-2156-4e00-b54d-11efd9abdb89','SourceName':'Local SharePoint Results','SourceLevel':'Ssa','CollapseSpecification':'','QueryTemplate':'{searchboxquery} contentclass:'STS_Site' ','FallbackSort':[{'p':'ViewsRecent','d':1}],'FallbackSortJson':'[{'p':'ViewsRecent','d':1}]','RankRules':null,'RankRulesJson':'null','AsynchronousResultRetrieval':false,'SendContentBeforeQuery':true,'BatchClientQuery':true,'FallbackLanguage':-1,'FallbackRankingModelID':'','EnableStemming':true,'EnablePhonetic':false,'EnableNicknames':false,'EnableInterleaving':false,'EnableQueryRules':true,'EnableOrderingHitHighlightedProperty':false,'HitHighlightedMultivaluePropertyLimit':-1,'IgnoreContextualScope':true,'ScopeResultsToCurrentSite':false,'TrimDuplicates':false,'Properties':{'TryCache':true,'Scope':'{Site.URL}','UpdateLinksForCatalogItems':true,'EnableStacking':true,'ListId':'ecf7cef6-a31e-451c-b97a-fc2201bba642','ListItemId':134},'PropertiesJson':'{'TryCache':true,'Scope':'{Site.URL}','UpdateLinksForCatalogItems':true,'EnableStacking':true,'ListId':'ecf7cef6-a31e-451c-b97a-fc2201bba642','ListItemId':134}','ClientType':'ContentSearchRegular','UpdateAjaxNavigate':true,'SummaryLength':180,'DesiredSnippetLength':90,'PersonalizedQuery':false,'FallbackRefinementFilters':null,'IgnoreStaleServerQuery':false,'RenderTemplateId':'DefaultDataProvider','AlternateErrorMessage':null,'Title':'} ";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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