Home > Uncategorized > A handy function to fetch shared Accounts by a Team in MSCRM 3.0

A handy function to fetch shared Accounts by a Team in MSCRM 3.0


Okay after going through the following article I was able to build an
account team in MSCRM 3.0 but of course with some modifications.

Microsoft has a very good tutorial on Account Team Management:

http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/dnmbscrm1_2/html/mbs_crmCAAT.asp

Unfortunately the article is related to the old version MSCRM 1.2.
Now I was assigned the mammoth task of creating a report to display
all the shared accounts by a Team. I searched the technical articles
to get a starting point for the solution but in vain. All the
articles and code snippets consisted of Sql based solutions etc. So
finally I had to come up with my own intuition once again, ļ and came
up with the following function that I would like to share with all
you developers of MSCRM 3.0. Love to get feedback on this and if some
one has alternate method, do let me know.

void ListSharedObjectByTeams(Guid TeamId)
{
try
{
CrmService service = new CrmService();
service.Credentials =
System.Net.CredentialCache.DefaultCredentials;

// Create the ColumnSet indicating
the fields to be retrieved
ColumnSet cols = new ColumnSet();

// Sets the ColumnSet’s Properties
cols.Attributes = new string []
{“name”, “accountid”};

// Create the QueryExpression Object
QueryExpression query = new
QueryExpression();

// Set the QueryExpression Object’s
Properties
query.EntityName =
EntityName.account.ToString();
query.ColumnSet = cols;

// Retrieve the Contacts
BusinessEntityCollection bus =
service.RetrieveMultiple(query);
for (int i = 0;
i<bus.BusinessEntities.Length; i++)
{
account acct = (account)
bus.BusinessEntities[i];

TargetOwnedDynamic target =
new TargetOwnedDynamic();
target.EntityId =
acct.accountid.Value;
target.EntityName =
EntityName.account.ToString();

// Get Shared Access Rights

RetrieveSharedPrincipalsAndAccessRequest access = new
RetrieveSharedPrincipalsAndAccessRequest();
access.Target = target;

RetrieveSharedPrincipalsAndAccessResponse accessResponse =
(RetrieveSharedPrincipalsAndAccessResponse)
service.Execute
(access);

foreach (PrincipalAccess
principalAccess in accessResponse.PrincipalAccesses)
{

switch
(principalAccess.Principal.Type)
{

case
SecurityPrincipalType.Team:
if
(principalAccess.Principal.PrincipalId == TeamId)
{

lstSharedAccounts.Items.Add(acct.name);
}

break;
}

}
}
}
catch(Exception ex)
{
throw(ex);
}
}

Advertisements
Categories: Uncategorized Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

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

%d bloggers like this: