コード例 #1
0
ファイル: PagePipeBind.cs プロジェクト: ohaak/PnP-PowerShell
        internal ListItem GetPage(Web web, string listToLoad)
        {
            if (!string.IsNullOrEmpty(this.Library))
            {
                listToLoad = this.Library;
            }

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

            List libraryContainingPage = null;

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

            if (libraryContainingPage != null)
            {
                CamlQuery query = null;
                if (!string.IsNullOrEmpty(this.name))
                {
                    query = new CamlQuery
                    {
                        ViewXml = string.Format(CAMLQueryByExtensionAndName, 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);
        }
コード例 #2
0
        /// <summary>
        /// Returns a file as string
        /// </summary>
        /// <param name="web">The Web to process</param>
        /// <param name="serverRelativeUrl">The server relative URL to the file</param>
        /// <returns>The file contents as a string</returns>
        /// <remarks>#
        ///
        ///     Based on https://github.com/SharePoint/PnP-Sites-Core/blob/master/Core/OfficeDevPnP.Core/Extensions/FileFolderExtensions.cs
        ///     Modified to force onpremises support
        ///
        /// </remarks>
        public static string GetFileByServerRelativeUrlAsString(this Web web, string serverRelativeUrl)
        {
            var file    = web.GetFileByServerRelativeUrl(serverRelativeUrl);
            var context = web.Context;

            context.Load(file);
            context.ExecuteQueryRetry();

            var spVersion = BaseTransform.GetVersion(context);

            Stream sourceStream;

            if (spVersion == SPVersion.SP2010)
            {
                throw new System.Exception("SharePoint 2010 is not supported");
                //sourceStream = new MemoryStream();

                //if (context.HasPendingRequest)
                //{
                //    context.ExecuteQueryRetry();
                //}

                //var fileBinary = File.OpenBinaryDirect((ClientContext)context, serverRelativeUrl);
                //context.ExecuteQueryRetry();
                //Stream tempSourceStream = fileBinary.Stream;

                //CopyStream(tempSourceStream, sourceStream);
                //sourceStream.Seek(0, SeekOrigin.Begin);
            }
            else
            {
                ClientResult <Stream> stream = file.OpenBinaryStream();
                web.Context.ExecuteQueryRetry();
                sourceStream = stream.Value;
            }
            string returnString = string.Empty;

            using (Stream memStream = new MemoryStream())
            {
                CopyStream(sourceStream, memStream);
                memStream.Position = 0;

                using (var reader = new StreamReader(memStream))
                {
                    returnString = reader.ReadToEnd();
                }
            }

            return(returnString);
        }
コード例 #3
0
        /// <summary>
        /// Method to bypass missing property in SharePoint 2010
        /// </summary>
        /// <param name="web">Web to operate on</param>
        /// <remarks>Only required on source contexts</remarks>
        /// <returns>web url</returns>
        public static string GetUrl(this Web web)
        {
            if (BaseTransform.GetVersion(web.Context) == SPVersion.SP2010)
            {
                var siteCtx = web.Context.GetSiteCollectionContext();
                siteCtx.Site.EnsureProperties(p => p.ServerRelativeUrl, p => p.Url);
                web.EnsureProperty(p => p.ServerRelativeUrl);

                var    siteUri = new Uri(siteCtx.Site.Url);
                string host    = $"{siteUri.Scheme}://{siteUri.DnsSafeHost}";

                var serverRelativeUrl = web.ServerRelativeUrl;

                return(UrlUtility.Combine(host, serverRelativeUrl));
            }
            else
            {
                return(web.EnsureProperty(p => p.Url));
            }
        }
コード例 #4
0
        /// <summary>
        /// Get translation for the publishing pages library
        /// </summary>
        /// <param name="context">Context of the site</param>
        /// <returns>Translated name of the pages library</returns>
        public string GetPublishingPagesLibraryName(ClientContext context)
        {
            // Simplier implementation - Get the Pages library then get the relative URL of the rootfolder of the library

            //Keys:
            //  Web Property: __PagesListId
            //  Found in 2010, SPO

            string pagesLibraryName = "pages";

            if (context == null)
            {
                return(pagesLibraryName);
            }

            uint lcid = context.Web.EnsureProperty(p => p.Language);

            var propertyBagKey = Constants.WebPropertyKeyPagesListId;

            if (publishingPagesLibraryNames.ContainsKey(lcid))
            {
                if (publishingPagesLibraryNames.TryGetValue(lcid, out string name))
                {
                    return(name);
                }
                else
                {
                    // let's fallback to the default...we should never get here unless there's some threading issue
                    return(pagesLibraryName);
                }
            }
            else
            {
                if (BaseTransform.GetVersion(context) == SPVersion.SP2010)
                {
                    var keyVal = context.Web.GetPropertyBagValueString(propertyBagKey, string.Empty);
                    if (!string.IsNullOrEmpty(keyVal))
                    {
                        var list = context.Web.GetListById(Guid.Parse(keyVal), o => o.RootFolder.ServerRelativeUrl);
                        var webServerRelativeUrl = context.Web.EnsureProperty(w => w.ServerRelativeUrl);

                        pagesLibraryName = list.RootFolder.ServerRelativeUrl.Replace(webServerRelativeUrl, "").Trim('/').ToLower();

                        // add to cache
                        publishingPagesLibraryNames.TryAdd(lcid, pagesLibraryName);

                        return(pagesLibraryName);
                    }
                }
                else
                {
                    // Fall back to older logic
                    ClientResult <string> result = Microsoft.SharePoint.Client.Utilities.Utility.GetLocalizedString(context, "$Resources:List_Pages_UrlName", "osrvcore", int.Parse(lcid.ToString()));
                    context.ExecuteQueryRetry();

                    var altPagesLibraryName = new Regex(@"['´`]").Replace(result.Value, "");

                    if (string.IsNullOrEmpty(altPagesLibraryName))
                    {
                        return(pagesLibraryName);
                    }

                    // add to cache
                    publishingPagesLibraryNames.TryAdd(lcid, altPagesLibraryName.ToLower());

                    return(altPagesLibraryName.ToLower());
                }
            }

            return(pagesLibraryName);
        }
コード例 #5
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);
        }