Thursday 13 March 2008

Retrieve Team details from a User Id in CRM 3.0

In CRM 3.0, I needed to get some of the team information of a specified user which I could not find any sample code on how to do this but with the help of the SDK, I wrote this little method to retrieve the team id or list of teams ids that a specified user belongs to. The code is written in C#.

The code makes a call to the web service and retrieves the teamid using the Request and Retrieve classes RetrieveTeamsSystemUserRequest & RetrieveTeamsSystemUserResponse on the team entity.

The user performing this action from within CRM must have access rights on the team entity instance and thats why I used the best practice impersonation technique.

In this case, I’m only retrieving the teamid attribute value (Guid) but you can easily extend it to retrieve any of the following team attributes by creating a ColumnSet and set the attributes to retrieve for this or each team:

businessunitid Specifies the ID of the business unit with which the team is associated.
createdby Specifies the ID of the user who created the team.
createdon Specifies the date and time when the team was created.
description Contains the description of the team.
emailaddress Specifies the e-mail address for the team.
importsequencenumber
modifiedby Specifies the ID of the user who last modified the team.
modifiedon Specifies the date and time when the team was last modified.
name Specifies the name of the team.
organizationid Specifies the ID of the organization associated with the team.
teamid Specifies the ID for the team.

The EntityId is basically the SystemUserId performing this action.

I then passed an instance of the RetrieveTeamsSystemUserRequest class as the request parameter in the web service Execute method.

Finally, extracting the Guid from the returned instance of the response class (RetrieveTeamsSystemUserResponse) after casting it to the team entity.


private Guid GetTeamId(Guid userId)

{

Guid teamid = Guid.Empty;

try

{

CrmService service = new CrmService();

service.Credentials = System.Net.CredentialCache.DefaultCredentials;

// impersonate the user who performed the action

service.CallerIdValue = new CallerId();

service.CallerIdValue.CallerGuid = userId;

// Retrieves the team id of which the specified user is a member

RetrieveTeamsSystemUserRequest retrieveTeam = new RetrieveTeamsSystemUserRequest();

ColumnSet tcs = new ColumnSet();

tcs.Attributes = new String[] {"teamid"};

retrieveTeam.EntityId = userId;

retrieveTeam.ColumnSet = tcs;

//Get the teams details of which the specified user is a member

RetrieveTeamsSystemUserResponse responseTeam = (RetrieveTeamsSystemUserResponse)service.Execute(retrieveTeam);

teamid = ((team)((responseTeam.BusinessEntityCollection.BusinessEntities.GetValue(0)))).teamid.Value;

}

catch(Exception ex)

{

// TODO: Log errors

}

return teamid;

}

No comments: