Ejemplo n.º 1
0
        public static Profile GetProfile(string ProfileName)
        {
            FilterExpression filter = new FilterExpression(LogicalOperator.And);

            filter.AddCondition(new ConditionExpression("appl_name", ConditionOperator.Equal, ProfileName));
            EntityCollection col = XrmCore.RetrieveByFilter("appl_profiledefinition", filter);

            if (col.Entities.Count != 1)
            {
                throw new Exception(string.Format("There are {0} profiles with the name {1}. 1 was expected", col.Entities.Count, ProfileName));
            }

            Guid ProfileDefinitionId = col.Entities.First().GetAttributeValue <Guid>("appl_profiledefinitionid");

            using (CrmOrganizationServiceContext service = new CrmOrganizationServiceContext(XrmConnection.Connection))
            {
                IQueryable <Entity> query = from field in service.CreateQuery("appl_profilefield")
                                            join related in service.CreateQuery("appl_profilefield_appl_profiledefinitio") on field["appl_profilefieldid"] equals related["appl_profilefieldid"]
                                            join profiledef in service.CreateQuery("appl_profiledefinition") on related["appl_profiledefinitionid"] equals profiledef["appl_profiledefinitionid"]
                                            where (string)profiledef["appl_name"] == ProfileName
                                            select field;

                return(Profile.Factory(new EntityCollection(query.ToList()), ProfileDefinitionId));
            }
        }
Ejemplo n.º 2
0
        public static void ApplyAction(this Entity entity, KeyValuePair <string, string> action)
        {
            string[] actionArgs = action.Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            switch (action.Key.ToUpper())
            {
            case "APPLYWORKFLOW":
                InputArgumentCollection workflowArgs = null;
                if (actionArgs.Length > 1)
                {
                    workflowArgs = new InputArgumentCollection();
                    for (int i = 1; i < actionArgs.Length; i++)
                    {
                        string[] workflowArg = actionArgs[i].Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                        workflowArgs.Add(workflowArg[0], workflowArg[1]);
                    }
                }
                Guid g;
                if (Guid.TryParse(actionArgs[0], out g))
                {
                    XrmCore.ApplyWorkFlow(entity, g, workflowArgs);
                }
                else
                {
                    XrmCore.ApplyWorkFlow(entity, actionArgs[0], workflowArgs);
                }
                break;

            case "ADDTOMARKETINGLIST":
            case "REMOVEFROMMARKETINGLIST":
                bool remove = action.Key.Equals("REMOVEFROMMARKETINGLIST", StringComparison.OrdinalIgnoreCase);
                for (int i = 0; i < actionArgs.Length; i++)
                {
                    Entity list = XrmCore.RetrieveByAttribute("list", "listname", actionArgs[i], new ColumnSet("listid")).Entities.FirstOrDefault();
                    if (list == null)
                    {
                        throw new ArgumentException(string.Format("The Marketing list {0} could not be found", actionArgs[0]));
                    }
                    else
                    {
                        if (remove)
                        {
                            XrmMarketing.RemoveFromMarketingList(entity.Id, list.Id);
                        }
                        else
                        {
                            XrmMarketing.AddToMarketingList(new Guid[] { entity.Id }, list.Id);
                        }
                    }
                }
                break;

            case "ASSIGN":
                FilterExpression filter = new FilterExpression(LogicalOperator.Or);
                filter.AddCondition("fullname", ConditionOperator.Like, new object[] { actionArgs[0] });
                filter.AddCondition("internalemailaddress", ConditionOperator.Like, new object[] { actionArgs[0] });
                Entity owner = XrmCore.RetrieveByFilter("systemuser", filter, new ColumnSet("systemuserid")).Entities.FirstOrDefault();
                if (owner == null)
                {
                    throw new ArgumentException(string.Format("The System user with the name or email {0} could not be found", actionArgs[0]));
                }
                else
                {
                    AssignRequest request = new AssignRequest()
                    {
                        Assignee = owner.ToEntityReference(),
                        Target   = entity.ToEntityReference()
                    };
                    XrmCore.Execute <AssignRequest, AssignResponse>(request);
                }
                break;

            default:
                break;
            }
        }