I am using sp 2010. I have a search result webpart with some query inside of it to filter on some content type. I can see results. Is it possible to run this search result webpart under another useraccount with full control permissions? Or something programmatically with runwithelevatedprivileges ?
1 Answer
Yes you can extend the CoreResultsWebPart
and create a new webpart.
public class ElevatedSearchResults : Microsoft.Office.Server.Search.WebControls.CoreResultsWebPart
{
....
}
Now one problem with your approach is, if you elevate the code then all results will get displayed which the current user is not suppose to see. Also if the user who tries to open the link (which he don't have permission) will get an Access Denied
error, which will not be a good user experience.
To elevate you should override
method GetXPathNavigator
.
The complete implementation will look like below
public class ElevatedSearchResults : Microsoft.Office.Server.Search.WebControls.CoreResultsWebPart
{
protected override System.Xml.XPath.XPathNavigator GetXPathNavigator(string viewPath)
{
try
{
SPSecurity.RunWithElevatedPrivileges(() =>
{
return base.GetXPathNavigator(viewPath);
});
}
catch
{
//LOG ERROR
}
}
}
PS: The above code is not tested.