I am using the Microsoft.SharePoint.Client.Search dll (Client Object Model) in a webpart because Microsoft.SharePoint.Search is obsolete.
When using the ClientContext.ExecuteQuery() method we get an "401 Unauthorized access error". To solve this issue I have used this piece of code:
System.Net.NetworkCredential cr = new System.Net.NetworkCredential("user", "password");
clientContext.Credentials = cr;
.....
SearchExecutor searchExecutor = new SearchExecutor(clientContext);
ClientResult<ResultTableCollection> results = searchExecutor.ExecuteQuery(keywordQuery);
clientContext.ExecuteQuery();
The same code works in a console application with no credentials but in a WebPart I get the 401 Unauthorized access error and the webpart needs to execute the query with the current logged user (I am in a Domain).
Any ideas?
Thanks everyone.