private Entity GetPrimaryContactAndSetCompanyName()
        {
            if (OpenOpportunity == null)
            {
                return(null);
            }

            Entity primaryContact = null;

            var customer = OpenOpportunity.GetAttributeValue <EntityReference>("customerid");

            if (customer.LogicalName == "account")
            {
                var account = XrmContext.CreateQuery("account").First(a => a.GetAttributeValue <Guid>("accountid") == customer.Id);

                CompanyName.Text = account.GetAttributeValue <string>("name");

                primaryContact = account.GetRelatedEntity(XrmContext, new Relationship("account_primary_contact"));

                var channelPermission = XrmContext.GetChannelAccessByContact(Contact);

                var channelWriteAccess = (channelPermission != null && channelPermission.GetAttributeValue <bool?>("adx_write").GetValueOrDefault(false));

                var channelReadAccess = (channelPermission != null && channelPermission.GetAttributeValue <bool?>("adx_read").GetValueOrDefault(false));

                var parentAccount = (account.GetAttributeValue <EntityReference>("msa_managingpartnerid") != null) ? XrmContext.CreateQuery("account").FirstOrDefault(a => a.GetAttributeValue <Guid>("accountid") == account.GetAttributeValue <EntityReference>("msa_managingpartnerid").Id) : null;

                if (parentAccount != null && ((channelPermission != null && channelPermission.GetAttributeValue <EntityReference>("adx_accountid") != null) && channelPermission.GetAttributeValue <EntityReference>("adx_accountid").Equals(parentAccount.ToEntityReference())) && (parentAccount.GetAttributeValue <OptionSetValue>("accountclassificationcode") != null && parentAccount.GetAttributeValue <OptionSetValue>("accountclassificationcode").Value == 100000000))
                {
                    if (channelWriteAccess)
                    {
                        CompanyName.Text = string.Format(@"<a href=""{0}"" class=""Edit"">{1}</a>",
                                                         HttpUtility.HtmlEncode(EditAccountUrl(account.GetAttributeValue <Guid>("accountid"))),
                                                         HttpUtility.HtmlEncode(CompanyName.Text));
                    }
                    else if (channelReadAccess)
                    {
                        CompanyName.Text = string.Format(@"<a href=""{0}"" class=""Edit"">{1}</a>",
                                                         HttpUtility.HtmlEncode(ReadOnlyAccountUrl(account.GetAttributeValue <Guid>("accountid"))),
                                                         HttpUtility.HtmlEncode(CompanyName.Text));
                    }
                }


                //CompanyName.Attributes.Add("style", "white-space: nowrap;");
            }
            else if (customer.LogicalName == "contact")
            {
                primaryContact = XrmContext.CreateQuery("contact").First(c => c.GetAttributeValue <Guid>("contactid") == customer.Id);

                var account = primaryContact.GetRelatedEntity(XrmContext, new Relationship("account_primary_contact"));

                CompanyName.Text = account != null?account.GetAttributeValue <string>("name") : primaryContact.GetAttributeValue <string>("adx_organizationname");
            }

            return(primaryContact);
        }
Ejemplo n.º 2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            RedirectToLoginIfAnonymous();

            if (ContactToEdit == null)
            {
                RecordNotFoundError.Visible = true;

                ManagePermissions.Visible = false;

                return;
            }

            var parentAccount            = Contact.GetAttributeValue <EntityReference>("parentcustomerid") == null ? null : XrmContext.CreateQuery("account").FirstOrDefault(a => a.GetAttributeValue <Guid>("accountid") == Contact.GetAttributeValue <EntityReference>("parentcustomerid").Id);
            var accountAccessPermissions = parentAccount == null ? new List <Entity>() : ServiceContext.GetAccountAccessByContact(Contact).ToList();
            var accountAccessPermissionsForParentAccount = parentAccount == null ? new List <Entity>() : accountAccessPermissions.Where(a => a.GetAttributeValue <EntityReference>("adx_accountid") != null && a.GetAttributeValue <EntityReference>("adx_accountid").Equals(parentAccount.ToEntityReference())).ToList();
            var contactAccessPermissions = parentAccount == null ? new List <Entity>() : XrmContext.GetContactAccessByContact(Contact).ToList();
            var contactAccessPermissionsForParentAccount = parentAccount == null ? new List <Entity>() : contactAccessPermissions.Where(c => c.GetAttributeValue <EntityReference>("adx_accountid") != null && c.GetAttributeValue <EntityReference>("adx_accountid").Equals(parentAccount.ToEntityReference()) && c.GetAttributeValue <OptionSetValue>("adx_scope").Value == (int)Enums.ContactAccessScope.Account).ToList();
            var channelPermissions     = XrmContext.GetChannelAccessByContact(Contact);
            var opportunityPermissions = XrmContext.GetOpportunityAccessByContact(Contact).FirstOrDefault();
            var contactToEditOpportunityPermissions = XrmContext.CreateQuery("adx_opportunitypermissions").FirstOrDefault(c => c.GetAttributeValue <EntityReference>("adx_contactid") != null && c.GetAttributeValue <EntityReference>("adx_contactid").Equals(ContactToEdit.ToEntityReference()));
            var contactToEditChannelPermissions     = XrmContext.CreateQuery("adx_channelpermissions").FirstOrDefault(c => c.GetAttributeValue <EntityReference>("adx_contactid") != null && c.GetAttributeValue <EntityReference>("adx_contactid").Equals(ContactToEdit.ToEntityReference()));
            var canEditContacts               = false;
            var canManagePermissions          = false;
            var canEditChannelPermissions     = channelPermissions != null;
            var canEditOpportunityPermissions = opportunityPermissions != null;

            if (parentAccount == null)
            {
                ManagePermissions.Visible = false;

                NoParentAccountError.Visible = true;

                return;
            }

            if (!accountAccessPermissions.Any())
            {
                ManagePermissions.Visible = false;

                NoAccountAccessPermissionsRecordError.Visible = true;

                return;
            }

            if (!accountAccessPermissionsForParentAccount.Any())
            {
                ManagePermissions.Visible = false;

                NoAccountAccessPermissionsForParentAccountError.Visible = true;

                return;
            }

            if (!contactAccessPermissions.Any())
            {
                ManagePermissions.Visible = false;

                NoContactAccessPermissionsRecordError.Visible = true;

                return;
            }

            if (!contactAccessPermissionsForParentAccount.Any())
            {
                ManagePermissions.Visible = false;

                NoContactAccessPermissionsForParentAccountError.Visible = true;

                return;
            }

            foreach (var access in accountAccessPermissionsForParentAccount)
            {
                if (access.GetAttributeValue <bool?>("adx_managepermissions").GetValueOrDefault(false))
                {
                    canManagePermissions = true;
                }
            }

            if (!canManagePermissions)
            {
                ManagePermissions.Visible = false;

                AccountAccessManagePermissionsDeniedError.Visible = true;

                return;
            }

            foreach (var access in contactAccessPermissionsForParentAccount)
            {
                if (access.GetAttributeValue <bool?>("adx_write").GetValueOrDefault(false))
                {
                    canEditContacts = true;
                }
            }

            if (!canEditContacts)
            {
                ManagePermissions.Visible = false;

                ContactAccessPermissionsError.Visible = true;

                return;
            }

            if (channelPermissions == null && opportunityPermissions == null)
            {
                ManagePermissions.Visible = false;

                NoPermissionsError.Visible = true;

                return;
            }

            if (contactToEditOpportunityPermissions == null & contactToEditChannelPermissions == null)
            {
                ManagePermissions.Visible = false;

                PermissionsRecordsForContactEditNotFoundError.Visible = true;

                return;
            }

            if (!canEditOpportunityPermissions)
            {
                OpportunityPermissions.Visible = false;

                NoOpportunityPermissionsRecordWarning.Visible = true;
            }
            else
            {
                if (contactToEditOpportunityPermissions == null)
                {
                    OpportunityPermissions.Visible = false;

                    OpportunityPermissionsRecordForContactEditNotFoundError.Visible = true;
                }
                else
                {
                    var opportunityPermissionsFormViewDataSource = new CrmDataSource {
                        ID = "OpportunityPermissionsFormViewDataSource", CrmDataContextName = OpportunityPermissionsFormView.ContextName
                    };

                    var opportunityPermissionsFetchXml =
                        string.Format(
                            "<fetch mapping='logical'><entity name='{0}'><all-attributes /><filter type='and'><condition attribute = '{1}' operator='eq' value='{{{2}}}'/></filter></entity></fetch>",
                            "adx_opportunitypermissions", "adx_opportunitypermissionsid", contactToEditOpportunityPermissions.Id);

                    opportunityPermissionsFormViewDataSource.FetchXml = opportunityPermissionsFetchXml;

                    OpportunityPermissions.Controls.Add(opportunityPermissionsFormViewDataSource);

                    OpportunityPermissionsFormView.DataSourceID = "OpportunityPermissionsFormViewDataSource";
                }
            }

            if (!canEditChannelPermissions)
            {
                ChannelPermissions.Visible = false;

                NoChannelPermissionsRecordWarning.Visible = true;
            }
            else
            {
                if (contactToEditChannelPermissions == null)
                {
                    ChannelPermissions.Visible = false;

                    ChannelPermissionsRecordForContactEditNotFoundError.Visible = true;
                }
                else
                {
                    var channelPermissionsFormViewDataSource = new CrmDataSource {
                        ID = "ChannelPermissionsWebViewDataSource", CrmDataContextName = ChannelPermissionsFormView.ContextName
                    };

                    var channelPermissionsFetchXml =
                        string.Format(
                            "<fetch mapping='logical'><entity name='{0}'><all-attributes /><filter type='and'><condition attribute = '{1}' operator='eq' value='{{{2}}}'/></filter></entity></fetch>",
                            "adx_channelpermissions", "adx_channelpermissionsid", contactToEditChannelPermissions.Id);

                    channelPermissionsFormViewDataSource.FetchXml = channelPermissionsFetchXml;

                    ChannelPermissions.Controls.Add(channelPermissionsFormViewDataSource);

                    ChannelPermissionsFormView.DataSourceID = "ChannelPermissionsWebViewDataSource";
                }
            }

            if (!IsPostBack)
            {
                SetControlValues(contactToEditOpportunityPermissions, contactToEditChannelPermissions);
            }

            EnableControlsBasedOnPartnerAccess(opportunityPermissions, channelPermissions);
        }