예제 #1
0
        public static async Task GetAccountCounts(CRMWebAPI api, ITurnContext <IMessageActivity> context)
        {
            await Task.Run(async() =>
            {
                dynamic whoamiResults = await api.ExecuteFunction("WhoAmI");
                var opt = new CRMGetListOptions
                {
                    Top    = 5000,
                    Filter = "_createdby_value eq " + whoamiResults.UserId + " or _modifiedby_value eq " + whoamiResults.UserId
                };
                var count = await api.GetList("accounts", QueryOptions: opt);
                if (count == null)
                {
                    count      = new Xrm.Tools.WebAPI.Results.CRMGetListResult <ExpandoObject>();
                    count.List = new List <ExpandoObject>();
                }
                else if (count.List == null)
                {
                    count.List = new List <ExpandoObject>();
                }

                var card = new HeroCard();

                var action = new CardAction()
                {
                    Type  = ActionTypes.MessageBack,
                    Title = $"Accounts owned by me: {count.List.Count}",
                    Value = count.ToString()
                };
                card.Buttons = new List <CardAction>();
                card.Buttons.Add(action);
                await DisplayMessage(card, context);
            });
        }
예제 #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Press S for clientid/secret, C for certificate auth");
            var key = Console.ReadKey();

            Task.Run(async() =>
            {
                string clientId     = ConfigurationManager.AppSettings["ClientId"];
                string clientSecret = ConfigurationManager.AppSettings["ClientSecret"];
                string apiUrl       = ConfigurationManager.AppSettings["ApiUrl"];
                string thumb        = ConfigurationManager.AppSettings["CertThumb"];

                var authParams  = await AuthenticationParameters.CreateFromResourceUrlAsync(new Uri(apiUrl));
                var authContext = new AuthenticationContext(authParams.Authority);

                // clientid/secret
                var clientCreds = new ClientCredential(clientId, clientSecret);

                // certificate
                X509Store store = new X509Store(StoreLocation.CurrentUser);
                store.Open(OpenFlags.ReadOnly);
                var certs     = store.Certificates.Find(X509FindType.FindByThumbprint, thumb, false);
                var cert      = certs[0];
                var certCreds = new ClientAssertionCertificate(clientId, certs[0]);

                AuthenticationResult result;
                if (key.Key == ConsoleKey.S)
                {
                    result = await authContext.AcquireTokenAsync(authParams.Resource,
                                                                 clientCreds);
                }
                else
                {
                    result = await authContext.AcquireTokenAsync(authParams.Resource,
                                                                 certCreds);
                }

                CRMWebAPI api  = new CRMWebAPI(apiUrl, result.AccessToken);
                dynamic whoAmI = await api.ExecuteFunction("WhoAmI");
                Console.WriteLine($"{whoAmI.OrganizationId},{whoAmI.BusinessUnitId},{whoAmI.UserId}");
                Console.ReadLine();
            }).Wait();
        }