フォームに統合することを目的とした Silverlight Web リソースを作成していますが、次のすべての情報を知る必要があります。
- 現在のユーザーのID
- 現在のユーザーが所属するチームの ID
- 現在のユーザーのセキュリティ ロールの ID
私は事前にバインドされた方法で作業しており、サービス参照を OData エンドポイント ( ) に追加しました。これにより、コンテキストが提供されます (コードでの実際の名前であるhttp://server/org/XRMservices/2011/OrganizationData.svc
という名前にしましょう)。cmtestcontext
私はこのクラスを介してデータにアクセスします (作成したわけではありません。少し前にネットでググっただけです。これは簡略化された簡潔なバージョンです)。
public class QueryInterface
{
//NOTE: ServiceReference1 is the name of the OData service reference
//Add Service Reference -> point to CRM OData url
public ServiceReference1.cmtextcontext CrmContext;
public QueryInterface()
{
var crmServerUrl = (string)GetContext().Invoke("getServerUrl");
if (crmServerUrl.EndsWith("/")) crmServerUrl = crmServerUrl.Substring(0, crmServerUrl.Length - 1);
Uri ODataUri = new Uri(crmServerUrl + "/xrmservices/2011/organizationdata.svc/", UriKind.Absolute);
CrmContext = new cmtestContext(ODataUri) { IgnoreMissingProperties = true };
}
}
このクラスを使用すると、次のように 1 行でフェッチを行うことができます (実際のコード スニペットは、コピーして貼り付けることができるようにダミー メソッドに含まれています)。
void RetrieveAllInformationFromCRM()
{
QueryInterface qi = new QueryInterface();
List<Guid> allData = new List<Guid>();
//NOTE: STEP 1 - USER ID
//NOTE: Since this is a web resource, I can cheat and use Xrm.Page.context.getUserId()
//NOTE: Remove the extra '{}' from the result for it to be parsed!
allData.Add(new Guid(qi.GetContext().Invoke("getUserId").ToString().Substring(1,36)));
//NOTE: STEP 2a - TEAM MEMBERSHIP FOR USER
//NOTE: TeamMembership entity links users to teams in a N:N relationship
qi.crmContext.TeamMembershipSet.BeginExecute(new AsyncCallback((result) =>
{
var teamMemberships = qi.crmContext.TeamMembershipSet.EndExecute(result)
.Where(tm => tm.TeamId.HasValue && (tm.SystemUserId ?? Guid.Empty) == userId)
.Select(tm => tm.TeamId.Value);
//NOTE: STEP 2b - TEAMS RELATED TO TEAMMEMBERSHIPS
qi.crmContext.TeamSet.BeginExecute(new AsyncCallback((result2) =>
{
var teamDetails = qi.crmContext.TeamSet.EndExecute(result2)
.Where(t => teamMemberships.Contains(t.TeamId));
foreach (var team in teamDetails)
allData.Add(team.TeamId);
//NOTE: FINAL STEP - allData is filled and ready to be used.
}), null);
}), null);
}
上記のコードでは、 myFINAL STEP
がそれを取得しallData
て処理し、フローが続きます。私の懸念は、この「リーダー」メソッドを変更する必要がある場合は、「最終」コードをカットアンドペーストして、すべての読み取り後に配置されるようにする必要があることです。読み取りが互いに続くようにすることができれば、もっと良い方法が欲しいので、これを行うことができます:
void MyReaderMethod()
{
ReadEverything();
ProcessData();
}
基本的には依頼が終わるのを待っていただけますか?ぶら下がっている UI は問題ではありBackgroundWorker
ません。「お待ちください」というスプラッシュとともにコードをラップするだけです。