Esempio n. 1
0
 public PagePipeBind(string name)
 {
     this.name         = ClientSidePageUtilities.EnsureCorrectPageName(name);
     this.pageListItem = null;
 }
Esempio n. 2
0
        internal ListItem GetPage(Web web, string listToLoad)
        {
            bool loadViaId = false;
            int  idToLoad  = -1;

            // Check what we got via the pagepipebind constructor and prep for getting the page
            if (!string.IsNullOrEmpty(this.name))
            {
                if (int.TryParse(this.Name, out int pageId))
                {
                    idToLoad  = pageId;
                    loadViaId = true;
                }
                else
                {
                    if (!this.BlogPage && !this.DelveBlogPage)
                    {
                        this.name = ClientSidePageUtilities.EnsureCorrectPageName(this.name);
                    }
                    this.pageListItem = null;
                }
            }
            else if (this.pageListItem != null)
            {
                if (this.pageListItem != null)
                {
                    if (this.BlogPage || this.DelveBlogPage)
                    {
                        this.name = this.pageListItem.FieldValues["Title"].ToString();
                    }
                    else
                    {
                        this.name = this.pageListItem.FieldValues["FileLeafRef"].ToString();
                    }
                }
            }

            if (!string.IsNullOrEmpty(this.Library))
            {
                listToLoad = this.Library;
            }

            // Blogs live in a list, not in a library
            if (this.BlogPage && !listToLoad.StartsWith("lists/", StringComparison.InvariantCultureIgnoreCase))
            {
                listToLoad = $"lists/{listToLoad}";
            }

            web.EnsureProperty(w => w.ServerRelativeUrl);
            var listServerRelativeUrl = UrlUtility.Combine(web.ServerRelativeUrl, listToLoad);

            List libraryContainingPage = null;

#if !NETSTANDARD2_1
            if (BaseTransform.GetVersion(web.Context) == SPVersion.SP2010)
            {
                libraryContainingPage = web.GetListByName(listToLoad);
            }
            else
            {
                libraryContainingPage = web.GetList(listServerRelativeUrl);
            }
#else
            libraryContainingPage = web.GetList(listServerRelativeUrl);
#endif

            if (libraryContainingPage != null)
            {
                if (loadViaId)
                {
                    var page = libraryContainingPage.GetItemById(idToLoad);
                    web.Context.Load(page);
                    web.Context.ExecuteQueryRetry();
                    return(page);
                }
                else
                {
                    CamlQuery query = null;
                    if (!string.IsNullOrEmpty(this.name))
                    {
                        if (this.BlogPage || this.DelveBlogPage)
                        {
                            query = new CamlQuery
                            {
                                ViewXml = string.Format(CAMLQueryForBlogByTitle, System.Text.Encodings.Web.HtmlEncoder.Default.Encode(this.name))
                            };
                        }
                        else
                        {
                            query = new CamlQuery
                            {
                                ViewXml = string.Format(CAMLQueryByExtensionAndName, System.Text.Encodings.Web.HtmlEncoder.Default.Encode(this.name))
                            };
                        }

                        if (!string.IsNullOrEmpty(this.Folder))
                        {
                            libraryContainingPage.EnsureProperty(p => p.RootFolder);
                            query.FolderServerRelativeUrl = $"{libraryContainingPage.RootFolder.ServerRelativeUrl}/{Folder}";
                        }

                        var page = libraryContainingPage.GetItems(query);
                        web.Context.Load(page);
                        web.Context.ExecuteQueryRetry();

                        if (page.Count >= 1)
                        {
                            // Return the first match
                            return(page[0]);
                        }
                    }
                }
            }

            return(null);
        }