public ConsoleResultModel Run()
        {
            TabController        tc     = new TabController();
            List <PageModelBase> lst    = new List <PageModelBase>();
            List <PageModelBase> lstOut = new List <PageModelBase>();

            if (ParentId.HasValue)
            {
                lst = GetPagesByParentId((int)ParentId);
            }
            else
            {
                TabCollection tabs = tc.GetTabsByPortal(PortalId);
                foreach (KeyValuePair <int, TabInfo> kvp in tabs)
                {
                    lst.Add(new PageModelBase(kvp.Value));
                }
            }

            // filter results if needed
            if (Deleted.HasValue)
            {
                var query = from page in lst
                            where page.IsDeleted == Deleted
                            select page;
                List <PageModelBase> filteredList = query.ToList();
                lst = filteredList;
            }


            bool   bSearchTitle     = false;
            string searchTitle      = null;
            bool   bSearchName      = false;
            string searchName       = null;
            bool   bSearchPath      = false;
            string searchPath       = null;
            bool   bSearchSkin      = false;
            string searchSkin       = null;
            bool   bMatchVisibility = PageVisible.HasValue;

            if (!string.IsNullOrEmpty(PageName))
            {
                searchName  = PageName.Replace("*", ".*");
                bSearchName = true;
            }
            if (!string.IsNullOrEmpty(PageTitle))
            {
                searchTitle  = PageTitle.Replace("*", ".*");
                bSearchTitle = true;
            }
            if (!string.IsNullOrEmpty(PagePath))
            {
                searchPath  = PagePath.Replace("*", ".*");
                bSearchPath = true;
            }
            if (!string.IsNullOrEmpty(PageSkin))
            {
                searchSkin  = PageSkin.Replace("*", ".*");
                bSearchSkin = true;
            }

            if (bSearchTitle || bSearchName || bSearchPath || bSearchSkin || bMatchVisibility)
            {
                bool bIsMatch = false;
                foreach (PageModelBase pim in lst)
                {
                    bIsMatch = true;
                    if (bSearchTitle)
                    {
                        bIsMatch = bIsMatch & Regex.IsMatch(pim.Title, searchTitle, RegexOptions.IgnoreCase);
                    }
                    if (bSearchName)
                    {
                        bIsMatch = bIsMatch & Regex.IsMatch(pim.Name, searchName, RegexOptions.IgnoreCase);
                    }
                    if (bSearchPath)
                    {
                        bIsMatch = bIsMatch & Regex.IsMatch(pim.Path, searchPath, RegexOptions.IgnoreCase);
                    }
                    if (bSearchSkin)
                    {
                        bIsMatch = bIsMatch & Regex.IsMatch(pim.Skin, searchSkin, RegexOptions.IgnoreCase);
                    }
                    if (bMatchVisibility)
                    {
                        bIsMatch = bIsMatch & (pim.IncludeInMenu == PageVisible);
                    }
                    if (bIsMatch)
                    {
                        lstOut.Add(pim);
                    }
                }
            }
            else
            {
                lstOut = lst;
            }

            var msg = string.Format("{0} page{1} found", lstOut.Count, (lstOut.Count != 1 ? "s" : ""));

            return(new ConsoleResultModel(msg)
            {
                data = lstOut,
                fieldOrder = new string[] {
                    "TabId", "ParentId", "Name", "Title", "Skin", "Path", "IncludeInMenu", "IsDeleted"
                }
            });
        }
        /// <summary>
        /// Process the request
        /// </summary>
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

            if (this.ClientContext == null)
            {
                LogWarning("Invalid client context, configure the service to run again");
                return;
            }

            string pageFileName = PageTitle.Replace(" ", string.Empty);

            if (pageFileName.IndexOf("aspx") <= -1)
            {
                pageFileName = String.Format("{0}.aspx", pageFileName);
            }



            var context = this.ClientContext;
            var web     = context.Web;

            context.Load(web);
            context.ExecuteQuery();
            string webRelativeUrl = web.ServerRelativeUrl;

            ListCollection     allLists   = web.Lists;
            IEnumerable <List> foundLists = context.LoadQuery(allLists.Where(list => list.Title == ListTitle)
                                                              .Include(wl => wl.Id, wl => wl.Title, wl => wl.RootFolder.ServerRelativeUrl));

            context.ExecuteQuery();
            List webPartList = foundLists.FirstOrDefault();

            var wikiPageUrl = web.AddWikiPage(SitePagesLibraryTitle, pageFileName);

            context.ExecuteQuery();
            if (string.IsNullOrEmpty(wikiPageUrl))
            {
                wikiPageUrl = string.Format("{0}/{1}", SitePagesRelativeUrl, pageFileName);
            }

            var listUrl = webPartList.RootFolder.ServerRelativeUrl;
            var pageUrl = string.Format("{0}/{1}", webRelativeUrl, wikiPageUrl);


            var views = webPartList.Views;

            ClientContext.Load(views);
            ClientContext.ExecuteQueryRetry();

            var viewFound = false;

            foreach (var view in views.Where(w => w.Hidden))
            {
                LogVerbose("View {0} with title {1} and hidden status:{2} and server relative url:{3}", view.Id, view.Title, view.Hidden, view.ServerRelativeUrl);
                if (view.ServerRelativeUrl.ToUpper() == pageUrl.ToUpper())
                {
                    viewFound = true;
                }
            }

            var thisview = webPartList.GetViewByName(WebPartViewTitle);

            webPartList.Context.Load(thisview, tv => tv.Id, tv => tv.ServerRelativeUrl, tv => tv.HtmlSchemaXml, tv => tv.RowLimit, tv => tv.JSLink, tv => tv.ViewFields, tv => tv.ViewQuery, tv => tv.Aggregations);
            webPartList.Context.ExecuteQueryRetry();
            Guid viewID = thisview.Id;

            if (!viewFound)
            {
                // change layout to header and two column
                web.AddLayoutToWikiPage(SitePagesRelativeUrl, OfficeDevPnP.Core.WikiPageLayout.OneColumn, pageFileName);
                web.AddHtmlToWikiPage(SitePagesRelativeUrl, "<div>This is a provisioning TEST!!!</div>", pageFileName, 1, 1);

                // get markup and place on page
                var           xlstMarkup = webPartList.GetXsltWebPartXML(pageUrl, WebPartTitle, viewID);
                WebPartEntity wpe        = new WebPartEntity();
                wpe.WebPartXml   = xlstMarkup;
                wpe.WebPartTitle = WebPartTitle;
                wpe.WebPartIndex = 1;
                web.AddWebPartToWikiPage(SitePagesRelativeUrl, wpe, pageFileName, 1, 1, true, false);

                // Hidden View was created for this
                views = webPartList.Views;
                ClientContext.Load(views);
                ClientContext.ExecuteQueryRetry();
            }

            // Get View Markup
            var viewHtml = thisview.HtmlSchemaXml;
            var viewXdoc = XDocument.Parse(viewHtml);

            foreach (var view in views.Where(w => w.Hidden && w.ServerRelativeUrl == pageUrl))
            {
                LogVerbose("Found View {0} with title {1} and hidden status:{2} and server relative url:{3}", view.Id, view.Title, view.Hidden, view.ServerRelativeUrl);
                var vview     = viewXdoc.Root.Element("Query");
                var toolbar   = viewXdoc.Root.Element("Toolbar");
                var aggregate = viewXdoc.Root.Element("Aggregations");
                if (aggregate != null)
                {
                    view.Aggregations = aggregate.ToString(SaveOptions.DisableFormatting);
                }

                view.ViewFields.RemoveAll();
                foreach (var vf in thisview.ViewFields)
                {
                    view.ViewFields.Add(vf);
                }
                view.RowLimit     = thisview.RowLimit;
                view.Toolbar      = "<Toolbar Type=\"None\"/>";
                view.ViewQuery    = thisview.ViewQuery;
                view.JSLink       = thisview.JSLink;
                view.Aggregations = thisview.Aggregations;

                view.Update();
                ClientContext.ExecuteQueryRetry();
            }
        }