private void AddFilterToFetch(Fetch fetch, Filter filter)
        {
            if (fetch.Entity.Filters == null)
            {
                fetch.Entity.Filters = new List <Filter>
                {
                    new Filter {
                        Type = LogicalOperator.And, Filters = new List <Filter> {
                            filter
                        }
                    }
                };
            }
            else
            {
                var rootAndFilter = fetch.Entity.Filters.FirstOrDefault(f => f.Type == LogicalOperator.And);

                if (rootAndFilter != null)
                {
                    rootAndFilter.Conditions.Add(filter.Conditions.First());
                }
                else
                {
                    fetch.Entity.Filters.Add(new Filter
                    {
                        Type    = LogicalOperator.And,
                        Filters = new List <Filter> {
                            filter
                        }
                    });
                }
            }
        }
        private static Entity GetIdeaForum(string ideaForumPartialUrl, OrganizationServiceContext serviceContext, HttpContextBase httpContext)
        {
            var website = httpContext.GetWebsite();

            var filter = new Adxstudio.Xrm.Services.Query.Filter
            {
                Conditions = new[]
                {
                    new Condition("adx_websiteid", ConditionOperator.Equal, website.Id),
                    new Condition("adx_partialurl", ConditionOperator.Equal, ideaForumPartialUrl),
                    new Condition("statecode", ConditionOperator.Equal, 0)
                }
            };

            var languageInfo = httpContext.GetContextLanguageInfo();

            if (languageInfo.IsCrmMultiLanguageEnabled)
            {
                filter.Filters = new[]
                {
                    new Adxstudio.Xrm.Services.Query.Filter
                    {
                        Type       = LogicalOperator.Or,
                        Conditions = new[]
                        {
                            new Condition("adx_websitelanguageid", ConditionOperator.Null),
                            new Condition("adx_websitelanguageid", ConditionOperator.Equal, languageInfo.ContextLanguage.EntityReference.Id)
                        }
                    }
                };
            }

            var fetch = new Fetch
            {
                Entity = new FetchEntity("adx_ideaforum")
                {
                    Filters = new[] { filter }
                }
            };

            var ideaForum = serviceContext.RetrieveSingle(fetch);

            return(ideaForum);
        }
Beispiel #3
0
        public void UpdateEntitySet(string entityLogicalName, string entityAttribute, List <Guid> entityIds)
        {
            var filter = new Fetch.Filter
            {
                Type       = Microsoft.Xrm.Sdk.Query.LogicalOperator.Or,
                Conditions = new List <Fetch.Condition>()
                {
                    new Fetch.Condition
                    {
                        Attribute = entityAttribute,
                        Operator  = Microsoft.Xrm.Sdk.Query.ConditionOperator.In,
                        Values    = entityIds.Cast <object>().ToList()
                    },
                }
            };

            var entityIndexers = _index.GetIndexers(entityLogicalName, filters: new List <Fetch.Filter> {
                filter
            });

            UpdateWithIndexers(entityLogicalName, entityIndexers);
        }
Beispiel #4
0
        private static IEnumerable <DisabledItemActionLink> AddActionLinkToDisabledActionLinksList(OrganizationServiceContext context, EntityMetadata entityMetadata, ViewActionLink link, EntityRecord[] records)
        {
            var disabledLinks = new List <DisabledItemActionLink>();

            if (string.IsNullOrEmpty(link.FilterCriteria) || records == null || !records.Any())
            {
                return(disabledLinks);
            }

            // Get Entity Record IDs for filter.
            var ids = records.Select(record => record.Id).Cast <object>().ToList();

            // The condition for the filter on primary key
            var primaryAttributeCondition = new Condition
            {
                Attribute = entityMetadata.PrimaryIdAttribute,
                Operator  = ConditionOperator.In,
                Values    = ids
            };

            // Primary key filter
            var primaryAttributeFilter = new Filter
            {
                Conditions = new[] { primaryAttributeCondition },
                Type       = LogicalOperator.And
            };

            Fetch fetch = null;

            try
            {
                fetch = Fetch.Parse(link.FilterCriteria);
            }
            catch (Exception ex)
            {
                ADXTrace.Instance.TraceError(TraceCategory.Application, ex.Message);
                return(disabledLinks);
            }

            if (fetch.Entity == null)
            {
                ADXTrace.Instance.TraceError(TraceCategory.Application, "Fetch XML query is not valid. Entity can't be Null.");
                return(disabledLinks);
            }
            // Set number of fields to fetch to 0.
            fetch.Entity.Attributes = FetchAttribute.None;

            if (fetch.Entity.Filters == null)
            {
                fetch.Entity.Filters = new List <Filter>();
            }
            // Add primary key filter
            fetch.Entity.Filters.Add(primaryAttributeFilter);

            RetrieveMultipleResponse response;

            try
            {
                response = (RetrieveMultipleResponse)context.Execute(new RetrieveMultipleRequest {
                    Query = fetch.ToFetchExpression()
                });
            }
            catch (Exception ex)
            {
                ADXTrace.Instance.TraceError(TraceCategory.Application, ex.Message);
                return(disabledLinks);
            }

            if (response == null)
            {
                return(disabledLinks);
            }

            disabledLinks.AddRange(from record in records
                                   where response.EntityCollection.Entities.All(entity => entity.Id != record.Id)
                                   select new DisabledItemActionLink(record.Id, link.FilterCriteriaId));

            return(disabledLinks);
        }
Beispiel #5
0
        public void UpdateCmsEntityTree(string entityLogicalName, Guid rootEntityId, int?lcid = null)
        {
            ADXTrace.Instance.TraceInfo(TraceCategory.Application, string.Format("Begin updating Cms Entity Tree for logical name: {0}, rootEntityId: {1}", entityLogicalName, rootEntityId));
            var timer = Stopwatch.StartNew();

            if (entityLogicalName == "adx_webpage")
            {
                IContentMapProvider contentMapProvider  = AdxstudioCrmConfigurationManager.CreateContentMapProvider();
                Guid[] descendantLocalizedWebPagesGuids = CmsIndexHelper.GetDescendantLocalizedWebpagesForWebpage(contentMapProvider, rootEntityId, lcid).ToArray();
                Guid[] descendantRootWebPagesGuids      = CmsIndexHelper.GetDescendantRootWebpagesForWebpage(contentMapProvider, rootEntityId).ToArray();

                // -------------------- WEB PAGES ------------------------------
                if (descendantLocalizedWebPagesGuids.Any())
                {
                    var localizedWebPagesUnderTargetWebPageFilter = new Fetch.Filter
                    {
                        Type       = Microsoft.Xrm.Sdk.Query.LogicalOperator.Or,
                        Conditions = new List <Fetch.Condition>()
                        {
                            new Fetch.Condition
                            {
                                Attribute = "adx_webpageid",
                                Operator  = Microsoft.Xrm.Sdk.Query.ConditionOperator.In,
                                Values    = descendantLocalizedWebPagesGuids.Cast <object>().ToList()
                            },
                        }
                    };

                    var webPageIndexers = _index.GetIndexers("adx_webpage", filters: new List <Fetch.Filter> {
                        localizedWebPagesUnderTargetWebPageFilter
                    });

                    UpdateWithIndexers("adx_webpage", webPageIndexers);
                }

                // -------------------- FORUMS ------------------------------
                if (descendantRootWebPagesGuids.Any())
                {
                    var rootWebPagesUnderTargetWebPageFilter = new Fetch.Filter
                    {
                        Type       = Microsoft.Xrm.Sdk.Query.LogicalOperator.Or,
                        Conditions = new List <Fetch.Condition>()
                        {
                            new Fetch.Condition
                            {
                                Attribute = "adx_webpageid",
                                Operator  = Microsoft.Xrm.Sdk.Query.ConditionOperator.In,
                                Values    = descendantRootWebPagesGuids.Cast <object>().ToList()
                            },
                        }
                    };

                    var forumBlogToParentPageLink = new Fetch.Link
                    {
                        Name          = "adx_webpage",
                        FromAttribute = "adx_webpageid",
                        ToAttribute   = "adx_parentpageid",
                        Filters       = new List <Fetch.Filter>()
                        {
                            rootWebPagesUnderTargetWebPageFilter
                        }
                    };

                    Fetch.Link languageFilter = null;

                    if (lcid.HasValue)
                    {
                        languageFilter = new Fetch.Link
                        {
                            Name          = "adx_websitelanguage",
                            FromAttribute = "adx_websitelanguageid",
                            ToAttribute   = "adx_websitelanguageid",
                            Type          = Microsoft.Xrm.Sdk.Query.JoinOperator.Inner,
                            Alias         = "websitelangforupdatefilter",
                            Links         = new List <Fetch.Link>()
                            {
                                new Fetch.Link
                                {
                                    Name          = "adx_portallanguage",
                                    FromAttribute = "adx_portallanguageid",
                                    ToAttribute   = "adx_portallanguageid",
                                    Type          = Microsoft.Xrm.Sdk.Query.JoinOperator.Inner,
                                    Alias         = "portallangforupdatefilter",
                                    Filters       = new List <Fetch.Filter>()
                                    {
                                        new Fetch.Filter
                                        {
                                            Type       = Microsoft.Xrm.Sdk.Query.LogicalOperator.And,
                                            Conditions = new List <Fetch.Condition>()
                                            {
                                                new Fetch.Condition
                                                {
                                                    Attribute = "adx_lcid",
                                                    Operator  = Microsoft.Xrm.Sdk.Query.ConditionOperator.Equal,
                                                    Value     = lcid.Value
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        };
                    }

                    var forumBlogLinks = new List <Fetch.Link>()
                    {
                        forumBlogToParentPageLink
                    };
                    if (languageFilter != null)
                    {
                        forumBlogLinks.Add(languageFilter);
                    }

                    var forumIndexers = _index.GetIndexers("adx_communityforum", links: forumBlogLinks);
                    UpdateWithIndexers("adx_communityforum", forumIndexers);

                    var forumThreadForumLinks = new List <Fetch.Link>()
                    {
                        forumBlogToParentPageLink
                    };
                    if (languageFilter != null)
                    {
                        forumThreadForumLinks.Add(languageFilter);
                    }

                    var forumThreadToParentPageLink = new Fetch.Link
                    {
                        Name          = "adx_communityforum",
                        FromAttribute = "adx_communityforumid",
                        ToAttribute   = "adx_forumid",
                        Links         = forumThreadForumLinks
                    };

                    var forumThreadIndexers = _index.GetIndexers("adx_communityforumthread",
                                                                 links: new List <Fetch.Link>()
                    {
                        forumThreadToParentPageLink
                    });
                    UpdateWithIndexers("adx_communityforumthread", forumThreadIndexers);

                    var forumPostToParentPageLink = new Fetch.Link
                    {
                        Name          = "adx_communityforumthread",
                        FromAttribute = "adx_communityforumthreadid",
                        ToAttribute   = "adx_forumthreadid",
                        Alias         = "adx_communityforumpost_communityforumthread",
                        Links         = new List <Fetch.Link>()
                        {
                            forumThreadToParentPageLink
                        }
                    };

                    var forumPostIndexers = _index.GetIndexers("adx_communityforumpost",
                                                               links: new List <Fetch.Link>()
                    {
                        forumPostToParentPageLink
                    });
                    UpdateWithIndexers("adx_communityforumpost", forumPostIndexers);

                    // -------------------- BLOGS ------------------------------
                    var blogIndexers = _index.GetIndexers("adx_blog", links: forumBlogLinks);
                    UpdateWithIndexers("adx_blog", blogIndexers);

                    var blogPostBlogLinks = new List <Fetch.Link>()
                    {
                        forumBlogToParentPageLink
                    };
                    if (languageFilter != null)
                    {
                        blogPostBlogLinks.Add(languageFilter);
                    }

                    var blogPostParentPageLink = new Fetch.Link
                    {
                        Name          = "adx_blog",
                        FromAttribute = "adx_blogid",
                        ToAttribute   = "adx_blogid",
                        Alias         = "adx_blog_blogpost",
                        Links         = blogPostBlogLinks
                    };

                    var blogPostIndexers = _index.GetIndexers("adx_blogpost", links: new List <Fetch.Link> {
                        blogPostParentPageLink
                    });
                    UpdateWithIndexers("adx_blogpost", blogPostIndexers);
                }
            }
            else if (entityLogicalName == "adx_communityforum")
            {
                UpdateEntity("adx_communityforum", rootEntityId);

                var inForumFilterForThread = new Fetch.Filter
                {
                    Type       = Microsoft.Xrm.Sdk.Query.LogicalOperator.And,
                    Conditions = new List <Fetch.Condition>()
                    {
                        new Fetch.Condition
                        {
                            Attribute = "adx_forumid",
                            Operator  = Microsoft.Xrm.Sdk.Query.ConditionOperator.Equal,
                            Value     = rootEntityId
                        }
                    }
                };

                var forumThreadIndexers = _index.GetIndexers("adx_communityforumthread", filters: new List <Fetch.Filter> {
                    inForumFilterForThread
                });
                UpdateWithIndexers("adx_communityforumthread", forumThreadIndexers);

                var inForumFilterForPost = new Fetch.Link
                {
                    Name          = "adx_communityforumthread",
                    FromAttribute = "adx_communityforumthreadid",
                    ToAttribute   = "adx_forumthreadid",
                    Alias         = "adx_communityforumpost_communityforumthread",
                    Filters       = new List <Fetch.Filter>()
                    {
                        inForumFilterForThread
                    }
                };

                var forumPostIndexers = _index.GetIndexers("adx_communityforumpost", links: new List <Fetch.Link> {
                    inForumFilterForPost
                });
                UpdateWithIndexers("adx_communityforumpost", forumPostIndexers);
            }
            else if (entityLogicalName == "adx_ideaforum")
            {
                UpdateEntity("adx_ideaforum", rootEntityId);

                var inIdeaForumFilter = new Fetch.Filter
                {
                    Type       = Microsoft.Xrm.Sdk.Query.LogicalOperator.And,
                    Conditions = new List <Fetch.Condition>()
                    {
                        new Fetch.Condition
                        {
                            Attribute = "adx_ideaforumid",
                            Operator  = Microsoft.Xrm.Sdk.Query.ConditionOperator.Equal,
                            Value     = rootEntityId
                        }
                    }
                };

                var ideaIndexers = _index.GetIndexers("adx_idea", filters: new List <Fetch.Filter> {
                    inIdeaForumFilter
                });
                UpdateWithIndexers("adx_idea", ideaIndexers);
            }

            timer.Stop();
            ADXTrace.Instance.TraceInfo(TraceCategory.Application, string.Format("Cms Entity Tree updated for logical name: {0}, rootEntityId: {1}, timespan: {2}", entityLogicalName, rootEntityId, timer.ElapsedMilliseconds));
        }
        /// <summary>
        /// Add the necessary filter conditions to filter related records
        /// </summary>
        /// <param name="serviceContext"><see cref="OrganizationServiceContext"/></param>
        /// <param name="fetch"><see cref="Fetch"/></param>
        /// <param name="id">Id of the record to filter related</param>
        protected void AddFiltersToFetch(OrganizationServiceContext serviceContext, Fetch fetch, string id)
        {
            if (string.IsNullOrWhiteSpace(Metadata.ViewRelationshipName))
            {
                return;
            }

            var linkEntityAliasGenerator = LinkEntityAliasGenerator.CreateInstance(fetch);
            var metadataManyToMany       = EntityMetadata.ManyToManyRelationships.FirstOrDefault(r => r.SchemaName == Metadata.ViewRelationshipName);
            var relationshipManyToOne    = EntityMetadata.ManyToOneRelationships.FirstOrDefault(r => r.SchemaName == Metadata.ViewRelationshipName);
            var relationshipOneToMany    = EntityMetadata.OneToManyRelationships.FirstOrDefault(r => r.SchemaName == Metadata.ViewRelationshipName);

            if (metadataManyToMany != null)
            {
                var    linkIntersectName = metadataManyToMany.IntersectEntityName;
                string linkTargetFromAttribute;
                string linkTargetToAttribute;
                string linkIntersectFromAttribute;
                string linkIntersectToAttribute;
                if (metadataManyToMany.Entity1LogicalName == metadataManyToMany.Entity2LogicalName)
                {
                    linkIntersectFromAttribute = metadataManyToMany.Entity2IntersectAttribute;
                    linkIntersectToAttribute   = EntityMetadata.PrimaryIdAttribute;
                    linkTargetFromAttribute    = EntityMetadata.PrimaryIdAttribute;
                    linkTargetToAttribute      = metadataManyToMany.Entity1IntersectAttribute;
                }
                else
                {
                    linkTargetFromAttribute = linkTargetToAttribute = metadataManyToMany.Entity1LogicalName == Metadata.TargetEntityName
                                                ? metadataManyToMany.Entity1IntersectAttribute
                                                : metadataManyToMany.Entity2IntersectAttribute;
                    linkIntersectFromAttribute = linkIntersectToAttribute = metadataManyToMany.Entity1LogicalName == Metadata.ViewTargetEntityType
                                                ? metadataManyToMany.Entity1IntersectAttribute
                                                : metadataManyToMany.Entity2IntersectAttribute;
                }

                var link = new Link
                {
                    Name          = linkIntersectName,
                    FromAttribute = linkIntersectFromAttribute,
                    ToAttribute   = linkIntersectToAttribute,
                    Intersect     = true,
                    Visible       = false,
                    Links         = new List <Link>
                    {
                        new Link
                        {
                            Name          = Metadata.TargetEntityName,
                            FromAttribute = linkTargetFromAttribute,
                            ToAttribute   = linkTargetToAttribute,
                            Alias         = linkEntityAliasGenerator.CreateUniqueAlias(Metadata.TargetEntityName),
                            Filters       = new List <Filter>
                            {
                                new Filter
                                {
                                    Type       = LogicalOperator.And,
                                    Conditions = new List <Condition>
                                    {
                                        new Condition
                                        {
                                            Attribute = linkTargetFromAttribute,
                                            Operator  = ConditionOperator.Equal,
                                            Value     = id
                                        }
                                    }
                                }
                            }
                        }
                    }
                };

                if (fetch.Entity.Links == null)
                {
                    fetch.Entity.Links = new List <Link> {
                        link
                    };
                }
                else
                {
                    fetch.Entity.Links.Add(link);
                }
            }
            else if (relationshipManyToOne != null)
            {
                var link = new Link
                {
                    Name          = Metadata.TargetEntityName,
                    FromAttribute = relationshipManyToOne.ReferencedAttribute,
                    ToAttribute   = relationshipManyToOne.ReferencingAttribute,
                    Alias         = linkEntityAliasGenerator.CreateUniqueAlias(Metadata.TargetEntityName),
                    Filters       = new List <Filter>
                    {
                        new Filter
                        {
                            Type       = LogicalOperator.And,
                            Conditions = new List <Condition>
                            {
                                new Condition
                                {
                                    Attribute = relationshipManyToOne.ReferencedAttribute,
                                    Operator  = ConditionOperator.Equal,
                                    Value     = id
                                }
                            }
                        }
                    }
                };

                if (fetch.Entity.Links == null)
                {
                    fetch.Entity.Links = new List <Link> {
                        link
                    };
                }
                else
                {
                    fetch.Entity.Links.Add(link);
                }
            }
            else if (relationshipOneToMany != null)
            {
                var attribute = relationshipOneToMany.ReferencedEntity == Metadata.TargetEntityName
                                        ? relationshipOneToMany.ReferencedAttribute
                                        : relationshipOneToMany.ReferencingAttribute;

                var filter = new Filter
                {
                    Type       = LogicalOperator.And,
                    Conditions = new List <Condition>
                    {
                        new Condition
                        {
                            Attribute = attribute,
                            Operator  = ConditionOperator.Equal,
                            Value     = id
                        }
                    }
                };

                AddFilterToFetch(fetch, filter);
            }
            else
            {
                throw new ApplicationException(string.Format("RetrieveRelationshipRequest failed for view relationship name {0}", Metadata.ViewRelationshipName));
            }
        }