Esempio n. 1
0
        static async Task Main(string[] args)
        {
            try
            {
                var _crmUrl           = "https://crm.test.ru/";
                var _organizationName = "Org";
                var config            = Microsoft.Xrm.Sdk.Client.ServiceConfigurationFactory
                                        .CreateConfiguration <Microsoft.Xrm.Sdk.IOrganizationService>(
                    new Uri($"{_crmUrl}{_organizationName}/XRMServices/2011/Organization.svc"));


                var clientCredentials = new System.ServiceModel.Description.ClientCredentials();
                clientCredentials.Windows.ClientCredential.UserName = "******";
                clientCredentials.Windows.ClientCredential.Password = "******";

                var client = new Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy(config, clientCredentials);
                //client.CallerId = new Guid("5c073647-da06-e611-80f9-005056971789");

                var q = new Microsoft.Xrm.Sdk.Query.QueryExpression("role")
                {
                    ColumnSet = { Columns = { "name", "businessunitid", "roleid" } },
                    Orders    =
                    {
                        new Microsoft.Xrm.Sdk.Query.OrderExpression("name", Microsoft.Xrm.Sdk.Query.OrderType.Ascending)
                    },
                    LinkEntities =
                    {
                        new Microsoft.Xrm.Sdk.Query.LinkEntity("role",                                              "systemuserroles", "roleid",       "roleid",
                                                               Microsoft.Xrm.Sdk.Query.JoinOperator.Inner)
                        {
                            LinkEntities =
                            {
                                new Microsoft.Xrm.Sdk.Query.LinkEntity("systemuserroles",                           "systemuser",                           "systemuserid",
                                                                       "systemuserid",                              Microsoft.Xrm.Sdk.Query.JoinOperator.Inner)
                                {
                                    LinkCriteria =
                                    {
                                        Filters            =
                                        {
                                            new Microsoft.Xrm.Sdk.Query.FilterExpression(Microsoft.Xrm.Sdk.Query
                                                                                         .LogicalOperator.And)
                                            {
                                                Conditions =
                                                {
                                                    new Microsoft.Xrm.Sdk.Query.ConditionExpression("systemuserid",
                                                                                                    Microsoft.Xrm.Sdk.Query.ConditionOperator.EqualUserId)
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                };

                var rq = new Microsoft.Xrm.Sdk.Messages.RetrieveMultipleRequest()
                {
                    Query = q
                };

                /*var n = new Microsoft.Crm.Sdk.Messages.SetStateRequest
                 * {
                 *  EntityMoniker =
                 *      new Microsoft.Xrm.Sdk.EntityReference("lead", new Guid("c5dc1b6f-1712-e811-8229-005056977311")),
                 *  State = new Microsoft.Xrm.Sdk.OptionSetValue(0),
                 *  Status = new Microsoft.Xrm.Sdk.OptionSetValue(100000003)
                 * };
                 *
                 * var r = client.Execute(n);*/

                var result = await client.RetrieveMultiple(q);

                //return result.Entities.Select(i => i.GetAttributeValue<string>("name"));
                foreach (var role in result.Entities)
                {
                    Console.WriteLine(role.GetAttributeValue <string>("name"));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            Console.WriteLine();
            Console.WriteLine("Press key");
            Console.ReadKey();
        }
        /// <summary>
        /// Finds and returns the given user's groups.
        /// </summary>
        /// <param name="userName">A string representing the username of the user whose groups should be returned.</param>
        /// <param name="properties">An IDictionary representing the properties used to filter the groups returned.</param>
        /// <returns>An IGroupCollection representing the groups which were found.</returns>
        public IGroupCollection FindGroups(string userName, IDictionary<string, object> properties)
        {
            //the collection that we will populate and finally return
            GroupCollection groups = new GroupCollection();

            #region CRM code

            try
            {
                ServerConnection serverConnect = new ServerConnection(this._crmconfigurations);
                ServerConnection.Configuration config = serverConnect.GetServerConfiguration();
                if (config == null)
                {
                    if (this._logger != null)
                    {
                        this._logger.LogErrorMessage("K2Community.CSP.CRM", "CRM URL not found");
                    }
                }
                else
                {
                    string FetchXml = @"
                    <fetch mapping='logical'>
	                    <entity name='team'>
		                    <attribute name='name' />
	                    </entity>
                    </fetch>";

                    using (_serviceProxy = ServerConnection.GetOrganizationProxy(config))
                    {
                        _serviceProxy.EnableProxyTypes();

                        // Build fetch request and obtain results.
                        Microsoft.Xrm.Sdk.Messages.RetrieveMultipleRequest efr = new Microsoft.Xrm.Sdk.Messages.RetrieveMultipleRequest()
                        {
                            Query = new FetchExpression(FetchXml)
                        };
                        Microsoft.Xrm.Sdk.EntityCollection entityResults = ((Microsoft.Xrm.Sdk.Messages.RetrieveMultipleResponse)_serviceProxy.Execute(efr)).EntityCollection;

                        //sample of logging debug output
                        _logger.LogDebugMessage(base.GetType().ToString() + ".FindGroups", "Finding groups for user: "******".FindGroups", "Team Name: {0}", e.Attributes["name"].ToString());
                            Group group1 = new Group(this.SecurityLabel, e.Attributes["name"].ToString(), "", "");

                            //add the group to the collection
                            groups.Add(group1);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (this._logger != null)
                {
                    _logger.LogErrorMessage(base.GetType().ToString() + ".FindGroups error", "User: "******" Error: " + ex.Message + ex.StackTrace);
                }
            }

            #endregion

            //return the collection of groups that the user belongs to
            return groups;
        }