For using filtered view within the callout/plugin I used the following code
SqlConnection connection = new SqlConnection(“Data Source=dsname;Initial Catalog=dbname ;Integrated Security=SSPI;”);
string sqlQuery = @”select * from filteredOpportunity”;
SqlCommand cmd = new SqlCommand(sqlQuery, connection);
connection.Open();
//// your logic here
connection.Close();
But this code wasn’t returning any results .It was because callout/plugin run under the context of NT Authority\Network Service.
So we need to use impersonation in this case, for this we can use the following code
SqlConnection connection = new SqlConnection(“Data Source=dsname;Initial Catalog=dbname ;Integrated Security=SSPI; Pooling=false”);
string sqlQuery = @” SETUSER ‘domainname\administrator’ select * from filteredOpportunity“;
SqlCommand cmd = new SqlCommand(sqlQuery, connection);
//// your logic here
connection.Open();
SETUSER – To impersonate the admin who has access to the filtered views. More about SETUSER
http://msdn.microsoft.com/en-us/library/ms186297.aspx
Pooling-False This is important otherwise we will get the below error
The connection has been dropped because the principal that opened it subsequently assumed a new security context, and then tried to reset the connection under its impersonated security context.
Check this wonderful post as well
http://blogs.msdn.com/b/crm/archive/2007/05/21/writing-crm-callouts-with-filtered-views.aspx
Bye