Beispiel #1
0
        protected override bool RemoveUsersFromRoles(List <CRMUser> users, List <CRMRole> roles)
        {
            const string RemoveUsersFromRolesKey = "removeUsersFromRoles";

            ConditionalLog.Info("RemoveUsersFromRoles(...). Started.", this, TimerAction.Start, RemoveUsersFromRolesKey);

            var result = true;

            foreach (var role in roles)
            {
                foreach (var user in users)
                {
                    var request = new RemoveMemberListRequest();
                    request.EntityId = user.ID;
                    request.ListId   = role.ID;

                    try
                    {
                        this.CrmService.Execute(request);
                        ConditionalLog.Info(String.Format("RemoveUsersFromRoles(...). User {0} has been removed from {1} role.", user.Name, role.Name), this, TimerAction.Tick, RemoveUsersFromRolesKey);

                        this.CacheService.MembersCache.Remove(role.Name);
                        this.CacheService.MemberOfCache.Remove(user.Name);
                    }
                    catch (Exception e)
                    {
                        ConditionalLog.Error(String.Format("Couldn't remove contact {0} from marketing list {1} in CRM.", user.Name, role.Name), e, this);
                        result = false;
                    }
                }
            }

            ConditionalLog.Info("RemoveUsersFromRoles(...). Finished.", this, TimerAction.Stop, RemoveUsersFromRolesKey);
            return(result);
        }
        public static void RemoveFromMarketingList(Guid listMember, Guid listId)
        {
            RemoveMemberListRequest request = new RemoveMemberListRequest();

            request.EntityId = listMember;
            request.ListId   = listId;
            RemoveMemberListResponse response = XrmCore.Execute <RemoveMemberListRequest, RemoveMemberListResponse>(request);
        }
        private void RemoveListMember(Guid memberId, Guid listId)
        {
            var remove = new RemoveMemberListRequest
            {
                EntityId = memberId,
                ListId   = listId
            };

            XrmService.Execute(remove);
        }
        /// <summary>
        /// Remove member to marketing list.
        /// <para>
        /// For more information look at https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.removememberlistrequest(v=crm.7).aspx
        /// </para>
        /// </summary>
        /// <param name="listId">Marketing List Id</param>
        /// <param name="memberId">Member Id</param>
        /// <returns>
        /// <see cref="RemoveMemberListResponse"/>
        /// </returns>
        public RemoveMemberListResponse RemoveMember(Guid listId, Guid memberId)
        {
            ExceptionThrow.IfGuidEmpty(listId, "listId");
            ExceptionThrow.IfGuidEmpty(memberId, "memberId");

            RemoveMemberListRequest request = new RemoveMemberListRequest()
            {
                ListId   = listId,
                EntityId = memberId
            };

            return((RemoveMemberListResponse)this.OrganizationService.Execute(request));
        }
        protected override void Execute(CodeActivityContext executionContext)
        {
            #region "Load CRM Service from context"

            Common objCommon = new Common(executionContext);
            objCommon.tracingService.Trace("Load CRM Service from context --- OK");
            #endregion

            #region "Read Parameters"
            EntityReference marketingList = this.MarketingList.Get(executionContext);
            objCommon.tracingService.Trace(String.Format("marketingList: {0} ", marketingList.Id.ToString()));

            EntityReference account = this.account.Get(executionContext);

            EntityReference contact = this.contact.Get(executionContext);

            EntityReference lead = this.lead.Get(executionContext);

            #endregion

            Guid idToRemove = Guid.Empty;

            if (account != null)
            {
                idToRemove = account.Id;
            }
            else if (contact != null)
            {
                idToRemove = contact.Id;
            }
            else if (lead != null)
            {
                idToRemove = lead.Id;
            }
            objCommon.tracingService.Trace(String.Format("idToRemove: {0} ", idToRemove.ToString()));

            RemoveMemberListRequest removeRequest = new RemoveMemberListRequest();
            removeRequest.ListId   = marketingList.Id;
            removeRequest.EntityId = idToRemove;
            RemoveMemberListResponse removeResponse = (RemoveMemberListResponse)objCommon.service.Execute(removeRequest);
        }
Beispiel #6
0
 public void UnsubscribeSubscribers(string ListId, List <Entities.Subscriber> subscribers)
 {
     try
     {
         Guid listGuid = new Guid(ListId);
         RemoveMemberListRequest req = new RemoveMemberListRequest();
         req.ListId = listGuid;
         foreach (Entities.Subscriber subscriber in subscribers)
         {
             Contact contact = (Contact)context.ContactSet.Where(x => x.EMailAddress1.Equals(subscriber.E_mail)).FirstOrDefault();
             if (contact != null)
             {
                 req.EntityId = contact.Id;
                 RemoveMemberListResponse resp = (RemoveMemberListResponse)context.Execute(req);
             }
         }
     }
     catch (Exception e)
     {
         throw new Exception("Unsubscribe subscribers CRM (" + e.Message.ToString() + ")");
     }
 }
        protected override void Execute(CodeActivityContext executionContext)
        {
            #region "Load CRM Service from context"

            Common objCommon = new Common(executionContext);
            var context = executionContext.GetExtension<IWorkflowContext>();

            objCommon.tracingService.Trace("Load CRM Service from context --- OK");
            #endregion

            #region "Read Parameters"


            #endregion


            string entityName=(context.PrimaryEntityName);
            if (entityName != "account" && entityName != "contact" && entityName != "lead")
            {
                throw new Exception("MSG_UNSUPPORTED_MARKETING_LIST_MEMBER_TYPE");
            }


            if (!DoesCrmRecordExist(objCommon.service, context.PrimaryEntityName, context.PrimaryEntityId))
            {
                return;
            }

            var query = new QueryExpression
            {
                EntityName = "listmember",
                ColumnSet = new ColumnSet("entityid", "listid"),

                Criteria =
                    {
                        FilterOperator = LogicalOperator.And,
                        Conditions =
                                            {
                                                new ConditionExpression
                                                {
                                                    AttributeName = "entityid",
                                                    Operator = ConditionOperator.Equal,
                                                    Values = { context.PrimaryEntityId }
                                                },
                                            }
                    }
            };

            var listMembers = objCommon.service.RetrieveMultiple(query).Entities;

            foreach (Entity member in listMembers)
            {
                EntityReference ent= (EntityReference)member.Attributes["entityid"];
                EntityReference list = (EntityReference)member.Attributes["listid"];
                var request = new RemoveMemberListRequest
                {
                    EntityId = ent.Id,
                    ListId = list.Id
                };
                objCommon.service.Execute(request);
            }


        }
Beispiel #8
0
        public void Delete()
        {
            using (var reader = new StreamReader(filePath, Encoding.Default))
            {
                string line;
                int lineNumber = 0;
                while ((line = reader.ReadLine()) != null)
                {
                    lineNumber++;
                    try
                    {
                        var data = line.Split(',');

                        Guid firstGuid;
                        Guid secondGuid;

                        if (settings.FirstAttributeIsGuid)
                        {
                            firstGuid = new Guid(data[0]);
                        }
                        else
                        {
                            var records = service.RetrieveMultiple(new QueryExpression(settings.FirstEntity)
                            {
                                Criteria =
                                {
                                    Conditions =
                                    {
                                        new ConditionExpression(settings.FirstAttributeName, ConditionOperator.Equal,
                                            data[0])
                                    }
                                }
                            });

                            if (records.Entities.Count == 1)
                            {
                                firstGuid = records.Entities.First().Id;
                            }
                            else if (records.Entities.Count > 1)
                            {
                                if (RaiseError != null)
                                    RaiseError(this,
                                        new ResultEventArgs
                                        {
                                            LineNumber = lineNumber,
                                            Message = string.Format("More than one record ({0}) were found with the value specified", settings.FirstEntity)
                                        });

                                continue;
                            }
                            else
                            {
                                if (RaiseError != null)
                                    RaiseError(this,
                                    new ResultEventArgs
                                    {
                                        LineNumber = lineNumber,
                                        Message = string.Format("No record ({0}) was found with the value specified", settings.FirstEntity)
                                    });

                                continue;
                            }
                        }

                        if (settings.SecondAttributeIsGuid)
                        {
                            secondGuid = new Guid(data[1]);
                        }
                        else
                        {
                            var records = service.RetrieveMultiple(new QueryExpression(settings.SecondEntity)
                            {
                                Criteria =
                                {
                                    Conditions =
                                    {
                                        new ConditionExpression(settings.SecondAttributeName, ConditionOperator.Equal,
                                            data[1])
                                    }
                                }
                            });

                            if (records.Entities.Count == 1)
                            {
                                secondGuid = records.Entities.First().Id;
                            }
                            else if (records.Entities.Count > 1)
                            {
                                if (RaiseError != null)
                                    RaiseError(this,
                                    new ResultEventArgs
                                    {
                                        LineNumber = lineNumber,
                                        Message = string.Format("More than one record ({0}) were found with the value specified", settings.SecondEntity)
                                    });

                                continue;
                            }
                            else
                            {
                                if (RaiseError != null)
                                    RaiseError(this,
                                    new ResultEventArgs
                                    {
                                        LineNumber = lineNumber,
                                        Message = string.Format("No record ({0}) was found with the value specified", settings.SecondEntity)
                                    });

                                continue;
                            }
                        }

                        if (settings.Relationship == "listcontact_association"
                           || settings.Relationship == "listaccount_association"
                           || settings.Relationship == "listlead_association")
                        {
                            var request = new RemoveMemberListRequest
                            {
                                ListId = settings.FirstEntity == "list" ? firstGuid : secondGuid,
                                EntityId = settings.FirstEntity == "list" ? secondGuid : firstGuid
                            };

                            service.Execute(request);
                        }
                        else
                        {
                            var request = new DisassociateRequest
                            {
                                Target = new EntityReference(settings.FirstEntity, firstGuid),
                                Relationship = new Relationship(settings.Relationship),
                                RelatedEntities = new EntityReferenceCollection
                                {
                                    new EntityReference(settings.SecondEntity, secondGuid)
                                }
                            };

                            if (request.Target.LogicalName == request.RelatedEntities.First().LogicalName)
                            {
                                request.Relationship.PrimaryEntityRole = EntityRole.Referenced;
                            }

                            service.Execute(request);
                        }

                        OnRaiseSuccess(new ResultEventArgs { LineNumber = lineNumber });
                    }
                    catch (FaultException<OrganizationServiceFault> error)
                    {
                        if (error.Detail.ErrorCode.ToString("X") == "80040237")
                        {
                            OnRaiseError(new ResultEventArgs { LineNumber = lineNumber, Message = "Relationship was not created because it already exists" });
                        }
                        else
                        {
                            OnRaiseError(new ResultEventArgs { LineNumber = lineNumber, Message = error.Message });
                        }
                    }
                }
            }
        }
Beispiel #9
0
        public void Delete()
        {
            using (var reader = new StreamReader(filePath, Encoding.Default))
            {
                string line;
                int    lineNumber = 0;
                while ((line = reader.ReadLine()) != null)
                {
                    lineNumber++;
                    try
                    {
                        var data = line.Split(',');

                        Guid firstGuid;
                        Guid secondGuid;

                        if (settings.FirstAttributeIsGuid)
                        {
                            firstGuid = new Guid(data[0]);
                        }
                        else
                        {
                            var records = service.RetrieveMultiple(new QueryExpression(settings.FirstEntity)
                            {
                                Criteria =
                                {
                                    Conditions =
                                    {
                                        new ConditionExpression(settings.FirstAttributeName, ConditionOperator.Equal,
                                                                data[0])
                                    }
                                }
                            });

                            if (records.Entities.Count == 1)
                            {
                                firstGuid = records.Entities.First().Id;
                            }
                            else if (records.Entities.Count > 1)
                            {
                                if (RaiseError != null)
                                {
                                    RaiseError(this,
                                               new ResultEventArgs
                                    {
                                        LineNumber = lineNumber,
                                        Message    = string.Format("More than one record ({0}) were found with the value specified", settings.FirstEntity)
                                    });
                                }

                                continue;
                            }
                            else
                            {
                                if (RaiseError != null)
                                {
                                    RaiseError(this,
                                               new ResultEventArgs
                                    {
                                        LineNumber = lineNumber,
                                        Message    = string.Format("No record ({0}) was found with the value specified", settings.FirstEntity)
                                    });
                                }

                                continue;
                            }
                        }

                        if (settings.SecondAttributeIsGuid)
                        {
                            secondGuid = new Guid(data[1]);
                        }
                        else
                        {
                            var records = service.RetrieveMultiple(new QueryExpression(settings.SecondEntity)
                            {
                                Criteria =
                                {
                                    Conditions =
                                    {
                                        new ConditionExpression(settings.SecondAttributeName, ConditionOperator.Equal,
                                                                data[1])
                                    }
                                }
                            });

                            if (records.Entities.Count == 1)
                            {
                                secondGuid = records.Entities.First().Id;
                            }
                            else if (records.Entities.Count > 1)
                            {
                                if (RaiseError != null)
                                {
                                    RaiseError(this,
                                               new ResultEventArgs
                                    {
                                        LineNumber = lineNumber,
                                        Message    = string.Format("More than one record ({0}) were found with the value specified", settings.SecondEntity)
                                    });
                                }

                                continue;
                            }
                            else
                            {
                                if (RaiseError != null)
                                {
                                    RaiseError(this,
                                               new ResultEventArgs
                                    {
                                        LineNumber = lineNumber,
                                        Message    = string.Format("No record ({0}) was found with the value specified", settings.SecondEntity)
                                    });
                                }

                                continue;
                            }
                        }

                        if (settings.Relationship == "listcontact_association" ||
                            settings.Relationship == "listaccount_association" ||
                            settings.Relationship == "listlead_association")
                        {
                            var request = new RemoveMemberListRequest
                            {
                                ListId   = settings.FirstEntity == "list" ? firstGuid : secondGuid,
                                EntityId = settings.FirstEntity == "list" ? secondGuid : firstGuid
                            };

                            service.Execute(request);
                        }
                        else
                        {
                            var request = new DisassociateRequest
                            {
                                Target          = new EntityReference(settings.FirstEntity, firstGuid),
                                Relationship    = new Relationship(settings.Relationship),
                                RelatedEntities = new EntityReferenceCollection
                                {
                                    new EntityReference(settings.SecondEntity, secondGuid)
                                }
                            };

                            if (request.Target.LogicalName == request.RelatedEntities.First().LogicalName)
                            {
                                request.Relationship.PrimaryEntityRole = EntityRole.Referenced;
                            }

                            service.Execute(request);
                        }

                        OnRaiseSuccess(new ResultEventArgs {
                            LineNumber = lineNumber
                        });
                    }
                    catch (FaultException <OrganizationServiceFault> error)
                    {
                        if (error.Detail.ErrorCode.ToString("X") == "80040237")
                        {
                            OnRaiseError(new ResultEventArgs {
                                LineNumber = lineNumber, Message = "Relationship was not created because it already exists"
                            });
                        }
                        else
                        {
                            OnRaiseError(new ResultEventArgs {
                                LineNumber = lineNumber, Message = error.Message
                            });
                        }
                    }
                }
            }
        }
        public override void ProcessItemSetChanges()
        {
            var orgContex              = this.PluginContext.OrgCtx;
            var savedEntityId          = this.EntityRef.Id;
            var savedEntityLogicalName = this.EntityRef.LogicalName;
            var itemSetsConfig         =
                orgContex.pavelkh_advancedmultiselectitemsetconfigurationSet.Where(
                    r => r.pavelkh_ItemSetName == this.ItemSetName)
                .Select(r => new pavelkh_advancedmultiselectitemsetconfiguration
            {
                pavelkh_EntityName           = r.pavelkh_EntityName,
                pavelkh_ItemSetEntityName    = r.pavelkh_ItemSetEntityName,
                pavelkh_FetchXmlForIntersect = r.pavelkh_FetchXmlForIntersect
            }).FirstOrDefault();

            if (itemSetsConfig == null)
            {
                throw new NullReferenceException(nameof(itemSetsConfig));
            }

            var fetchXmlQuery = itemSetsConfig.pavelkh_FetchXmlForIntersect.Replace(
                ItemSetBuilder.FetchXmlEntityIdPlaceHolder,
                savedEntityId.ToString("D"));
            var fetchExpression  = new FetchExpression(fetchXmlQuery);
            var service          = this.PluginContext.Service;
            var entityCollection = service.RetrieveMultiple(fetchExpression);
            var existingSelected = entityCollection.Entities.Select(r => r.Id).ToList();
            var itemsToAssociate = this.SelectedIdList
                                   .Except(existingSelected)
                                   .ToArray();
            var itemsToDisassociate = existingSelected
                                      .Except(this.SelectedIdList)
                                      .ToArray();
            var associateRequest    = new AddListMembersListRequest();
            var disassociateRequest = new RemoveMemberListRequest();

            if (savedEntityLogicalName == MarketingListEntityName)
            {
                if (itemsToAssociate.Any())
                {
                    associateRequest.ListId    = savedEntityId;
                    associateRequest.MemberIds = itemsToAssociate;
                    service.Execute(associateRequest);
                }

                disassociateRequest.ListId = savedEntityId;
                foreach (var entityId in itemsToDisassociate)
                {
                    disassociateRequest.EntityId = entityId;
                    service.Execute(disassociateRequest);
                }
            }
            else
            {
                associateRequest.MemberIds = new[] { savedEntityId };
                foreach (var listId in itemsToAssociate)
                {
                    associateRequest.ListId = listId;
                    service.Execute(associateRequest);
                }

                disassociateRequest.EntityId = savedEntityId;
                foreach (var listId in itemsToDisassociate)
                {
                    disassociateRequest.ListId = listId;
                    service.Execute(disassociateRequest);
                }
            }
        }