Posted by: Arvind Singh | December 12, 2008

Using FilteredView to retrieve data in Plug-In or Callout

To retrieve data from CRM Database( FilteredView)…..

SqlConnection connection = new SqlConnection(“DataSource=<dsName>;Initial Catalog=<databaseName> ;Integrated Security=SSPI;”);

string sqlQuery = @” select max(new_uniqueid) from FilteredView” ;

SqlCommand cmd = new SqlCommand(sqlQuery, connection);

connection.Open();

/// Write 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(“DataSource=<dsName>;Initial Catalog=<databaseName> ;Integrated Security=SSPI; Pooling=false);

string sqlQuery = @” SETUSER ‘domainName\administrator’ select max(new_uniqueid) from FilteredView” ;

SqlCommand cmd = new SqlCommand(sqlQuery, connection);

connection.Open();

/// Write Logic here

connection.Close();

SETUSER - To impersonate the admin who has access to the filtered views. More about SETUSER

http://msdn.microsoft.com/en-us/library/ms186297.aspx

If we are not using Pooling=false in connection string its giving this error.

Event Log 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…….


Leave a response

Your response:

Categories