Setting the Xsl property of the CoreResultsWebPart

Modifying the Xsl property of the CoreResultsWebPart using the Web Part editor is straight-forward. But doing the same thing programmatically is a nuisance. In many situations you end up with an InvalidOperation exception.
 
The reason for this trouble is the getter of the XslHash property:
 
private string XslHash
{
    get
    {
        if (this._BeforeOnInit)
        {
            throw new InvalidOperationException();
        }
Of course, the _BeforeOnInit field is "true" when you try to modify the CoreResultsWebPart using the LimitedWebPartManager.
 
The easiest way to solve this involves a bit of reflection:
 

typeof(SearchResultsBaseWebPart).GetField("_BeforeOnInit", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(wp, false);

 
After this you can set the Xsl property.
 
HTH!
 

Published: Nov-19-11 | 0 Comments | Link to this post