コード例 #1
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));
        }