Ejemplo n.º 1
0
        /// <summary>
        /// Button to login to CRM and create a CrmService Client
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoginButton_Click(object sender, RoutedEventArgs e)
        {
            List <EntityObj> lst = new List <EntityObj>();

            List <SystemUser> lstActiveUsr = new List <SystemUser>();


            #region Login Control
            // Establish the Login control
            CrmLogin ctrl = new CrmLogin();
            // Wire Event to login response.
            ctrl.ConnectionToCrmCompleted += ctrl_ConnectionToCrmCompleted;
            // Show the dialog.
            ctrl.ShowDialog();

            // Handel return.
            if (ctrl.CrmConnectionMgr != null && ctrl.CrmConnectionMgr.CrmSvc != null && ctrl.CrmConnectionMgr.CrmSvc.IsReady)
            {
                MessageBox.Show("Good Connect");
            }
            else
            {
                MessageBox.Show("BadConnect");
            }

            #endregion

            #region CRMServiceClient
            if (ctrl.CrmConnectionMgr != null && ctrl.CrmConnectionMgr.CrmSvc != null && ctrl.CrmConnectionMgr.CrmSvc.IsReady)
            {
                svcClient = ctrl.CrmConnectionMgr.CrmSvc;
                ctrl.Close();
                this.loginbutton.Visibility = Visibility.Hidden;


                if (svcClient.IsReady)
                {
                    this.label.Visibility    = Visibility.Visible;
                    this.comboBox.Visibility = Visibility.Visible;

                    //RetrieveEntityRequest req = new RetrieveEntityRequest()
                    //{
                    //    EntityFilters = EntityFilters.All,
                    //    RetrieveAsIfPublished=true,
                    //    LogicalName="account"
                    //};

                    //RetrieveEntityResponse res = (RetrieveEntityResponse)svcClient.OrganizationServiceProxy.Execute(req);



                    RetrieveAllEntitiesRequest request = new RetrieveAllEntitiesRequest()
                    {
                        EntityFilters         = EntityFilters.Entity,
                        RetrieveAsIfPublished = true
                    };

                    // Retrieve the MetaData.
                    RetrieveAllEntitiesResponse response = (RetrieveAllEntitiesResponse)svcClient.OrganizationServiceProxy.Execute(request);

                    foreach (EntityMetadata currentEntity in response.EntityMetadata)
                    {
                        //if (currentEntity.DisplayName.UserLocalizedLabel!=null)
                        //{
                        EntityObj obj = new EntityObj();
                        obj.EntityName = currentEntity.LogicalName;
                        obj.EntityOC   = currentEntity.ObjectTypeCode.ToString();

                        lst.Add(obj);
                        //}
                    }

                    lst = lst.OrderBy(o => o.EntityName).ToList();
                    this.comboBox.DisplayMemberPath = "EntityName";
                    this.comboBox.SelectedValuePath = "EntityOC";
                    this.comboBox.ItemsSource       = lst;
                    this.comboBox.Text = "Select Entity Name";

                    this.label3.Visibility    = Visibility.Visible;
                    this.comboBox2.Visibility = Visibility.Visible;

                    DataCollection <Entity> activeUsrsColl = MainWindow.FindEnabledUsers(svcClient.OrganizationServiceProxy);

                    foreach (Entity user in activeUsrsColl)
                    {
                        SystemUser sysUser = new SystemUser();
                        sysUser.FullName     = user["fullname"].ToString();
                        sysUser.SystemUserID = user["systemuserid"].ToString();

                        lstActiveUsr.Add(sysUser);
                    }

                    lstActiveUsr = lstActiveUsr.OrderBy(o => o.FullName).ToList();
                    this.comboBox2.DisplayMemberPath = "FullName";
                    this.comboBox2.SelectedValuePath = "SystemUserID";
                    this.comboBox2.ItemsSource       = lstActiveUsr;
                    this.comboBox2.Text = "Select Active User Name";

                    // Get data from CRM .
                    //string FetchXML =
                    //    @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                    //                    <entity name='account'>
                    //                        <attribute name='name' />
                    //                        <attribute name='primarycontactid' />
                    //                        <attribute name='telephone1' />
                    //                        <attribute name='accountid' />
                    //                        <order attribute='name' descending='false' />
                    //                      </entity>
                    //                    </fetch>";

                    //var Result = svcClient.GetEntityDataByFetchSearchEC(FetchXML);
                    //if (Result != null)
                    //{
                    //    MessageBox.Show(string.Format("Found {0} records\nFirst Record name is {1}", Result.Entities.Count, Result.Entities.FirstOrDefault().GetAttributeValue<string>("name")));
                    //}


                    //// Core API using SDK OOTB
                    //CreateRequest req = new CreateRequest();
                    //Entity accENt = new Entity("account");
                    //accENt.Attributes.Add("name", "TESTFOO");
                    //req.Target = accENt;
                    //CreateResponse res = (CreateResponse)svcClient.OrganizationServiceProxy.Execute(req);
                    ////CreateResponse res = (CreateResponse)svcClient.ExecuteCrmOrganizationRequest(req, "MyAccountCreate");
                    //MessageBox.Show(res.id.ToString());



                    //// Using Xrm.Tooling helpers.
                    //Dictionary<string, CrmDataTypeWrapper> newFields = new Dictionary<string, CrmDataTypeWrapper>();
                    //// Create a new Record. - Account
                    //newFields.Add("name", new CrmDataTypeWrapper("CrudTestAccount", CrmFieldType.String));
                    //Guid guAcctId = svcClient.CreateNewRecord("account", newFields);

                    //MessageBox.Show(string.Format("New Record Created {0}", guAcctId));
                }
            }
            #endregion
        }
Ejemplo n.º 2
0
        private void comboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //RetrieveRecordChangeHistoryRequest changeRequest = new RetrieveRecordChangeHistoryRequest();

            ArrayList  lstUserRoles = new ArrayList();
            SystemUser objSysUsr    = new SystemUser();

            objSysUsr = (SystemUser)this.comboBox2.SelectedItem;
            //changeRequest.Target = new EntityReference("systemuser", Guid.Parse(objSysUsr.SystemUserID));

            //RetrieveRecordChangeHistoryResponse changeResponse = (RetrieveRecordChangeHistoryResponse)svcClient.OrganizationServiceProxy.Execute(changeRequest);

            QueryExpression queryExpression = new QueryExpression();

            queryExpression.EntityName = "role"; //role entity name
            ColumnSet cols = new ColumnSet();

            cols.AddColumn("name"); //We only need role name
            queryExpression.ColumnSet = cols;
            ConditionExpression ce = new ConditionExpression();

            ce.AttributeName = "systemuserid";
            ce.Operator      = ConditionOperator.Equal;
            ce.Values.Add(objSysUsr.SystemUserID);
            //system roles
            LinkEntity lnkEntityRole = new LinkEntity();

            lnkEntityRole.LinkFromAttributeName = "roleid";
            lnkEntityRole.LinkFromEntityName    = "role"; //FROM
            lnkEntityRole.LinkToEntityName      = "systemuserroles";
            lnkEntityRole.LinkToAttributeName   = "roleid";
            //system users
            LinkEntity lnkEntitySystemusers = new LinkEntity();

            lnkEntitySystemusers.LinkFromEntityName    = "systemuserroles";
            lnkEntitySystemusers.LinkFromAttributeName = "systemuserid";
            lnkEntitySystemusers.LinkToEntityName      = "systemuser";
            lnkEntitySystemusers.LinkToAttributeName   = "systemuserid";
            lnkEntitySystemusers.LinkCriteria          = new FilterExpression();
            lnkEntitySystemusers.LinkCriteria.Conditions.Add(ce);
            lnkEntityRole.LinkEntities.Add(lnkEntitySystemusers);
            queryExpression.LinkEntities.Add(lnkEntityRole);
            EntityCollection entColRoles = svcClient.OrganizationServiceProxy.RetrieveMultiple(queryExpression);

            if (entColRoles != null && entColRoles.Entities.Count > 0)
            {
                foreach (Entity entRole in entColRoles.Entities)
                {
                    lstUserRoles.Add(entRole["name"].ToString());
                }
            }

            lstUserRoles.Sort();

            this.listView2.Visibility  = Visibility.Visible;
            this.listView2.ItemsSource = lstUserRoles;

            QueryExpression query = new QueryExpression("team");

            query.ColumnSet = new ColumnSet(true);
            LinkEntity link = query.AddLink("teammembership", "teamid", "teamid");

            link.LinkCriteria.AddCondition(new ConditionExpression("systemuserid", ConditionOperator.Equal, objSysUsr.SystemUserID));

            ArrayList        lstTeam     = new ArrayList();
            EntityCollection entColTeams = svcClient.OrganizationServiceProxy.RetrieveMultiple(query);

            foreach (Entity entTeams in entColTeams.Entities)
            {
                lstTeam.Add(entTeams["name"].ToString());
            }

            lstTeam.Sort();
            this.listView3.Visibility  = Visibility.Visible;
            this.listView3.ItemsSource = lstTeam;
        }