Sample code to find all Teams a System User is member of:
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Tooling.Connector;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using Xrm;
private List<Team> GetAllUserTeams(Guid systemUserId)
{
// Initiate CRM-connection
var conn = new CrmServiceClient(ConfigurationManager.ConnectionStrings["CONN"].ConnectionString);
var service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;
var context = new OrganizationServiceContext(service);
var teams = (from tm in context.CreateQuery<TeamMembership>()
join t in context.CreateQuery<Team>() on tm.TeamId equals t.TeamId
where tm.SystemUserId == systemUserId
select t).ToList();
return teams;
}
Observe: The above code requires a generated XRM early bound entity file for the Team and TeamMembership entities.