Donnerstag, 18. April 2013

Setting the Dataprovider for Resultscriptwebpart programmatically (eg. for setting the QueryTemplate)

For doing that you have to create a instance of the DataProviderScriptWebpart, initialize it with the JsonData from the ResultScriptWebPart. There you can set the Propertiy you need and write the PropertiesJason-Member back to the ResultScriptWebpart.DataProviderJson. And of course save the Webpart finally.


           var resultWebPart =  
             webPartManager.WebParts.Cast<WebPart>()  
                    .FirstOrDefault(  
                      wp => wp is ResultScriptWebPart) as ResultScriptWebPart;  
   
           var querySettings = new DataProviderScriptWebPart  
             {  
               PropertiesJson =  
                 resultWebPart.DataProviderJSON  
             };  
   
           querySettings.Properties["QueryTemplate"] =  
             "{searchboxquery} (contentclass:STS_ListItem OR IsDocument:True)";  
   
           resultWebPart.DataProviderJSON = querySettings.PropertiesJson;  
   
           webPartManager.SaveChanges(resultWebPart);  

But beware: Altough there is a Property DataProviderScriptWebPart.QueryTemplate which you can set, it is not included if you request the DataProviderScriptWebPart.PropertiesJson. You have to set the property using the  Properties-Collection of the object.

(of course you could also simple edit the Json-String itself, but I prefer this way).

Keine Kommentare:

Kommentar veröffentlichen