Using filtered view in Callout and Plugin

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

 


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

2 thoughts on “Using filtered view in Callout and Plugin”

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s