/// <summary>
        /// Method to get a folder by his Id
        /// </summary>
        /// <param name="web">Explicitly specify the SPWeb</param>
        /// <param name="folderId">the Id of the folder</param>
        /// <returns>The folder or throw an exception if not found</returns>
        public SPFolder GetFolderByIdForWeb(SPWeb web, int folderId)
        {
            SPListItem item = web.GetPagesLibrary().GetItemById(folderId);

            if (item == null || item.Folder == null)
            {
                throw new KeyNotFoundException(string.Format(CultureInfo.InvariantCulture, "Folder key <{0}> was not found.", folderId));
            }

            return item.Folder;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a page in the Pages library
        /// </summary>
        /// <param name="web">The current web</param>
        /// <param name="folderId">The folder in which to add the item</param>
        /// <param name="pageInfo">The pageInfo of the page</param>
        /// <returns>The newly created publishing page</returns>
        public PublishingPage Create(SPWeb web, int folderId, PageInfo pageInfo)
        {
            PublishingPage newPage = null;
            bool userHavePermissions = false;

            // get the root folder if no folder is specified
            var folder = folderId == int.MinValue ? web.GetPagesLibrary().RootFolder : this.folderRepository.GetFolderByIdForWeb(web, folderId);

            // if spfolder is root folder, check permissions at library level
            if (folder.Item == null)
            {
                userHavePermissions = folder.DocumentLibrary.DoesUserHavePermissions(SPBasePermissions.AddListItems);
            }
            else
            {
                userHavePermissions = folder.Item.DoesUserHavePermissions(SPBasePermissions.AddListItems);
            }

            if (userHavePermissions)
            {
                using (new Unsafe(web))
                {
                    var requestedContentType = web.AvailableContentTypes[pageInfo.ContentTypeId];

                    if (requestedContentType != null)
                    {
                        if (pageInfo.PageLayout != null)
                        {
                            var publishingWeb = PublishingWeb.GetPublishingWeb(web);

                            if (!pageInfo.Name.EndsWith(".aspx", StringComparison.OrdinalIgnoreCase))
                            {
                                pageInfo.Name += ".aspx";
                            }

                            newPage = publishingWeb.GetPublishingPages().Add(folder.ServerRelativeUrl + "/" + pageInfo.Name, pageInfo.PageLayout);
                            newPage.ListItem[BuiltInFields.ContentType.InternalName] = requestedContentType.Name;
                            newPage.ListItem[BuiltInFields.ContentTypeId.InternalName] = requestedContentType.Id;

                            if (pageInfo.Values != null)
                            {
                                foreach (var field in pageInfo.Values)
                                {
                                    newPage.ListItem[field.FieldName] = field.Value;
                                }
                            }

                            if (pageInfo.IsWelcomePage)
                            {
                                folder.WelcomePage = newPage.ListItem.Name;
                                folder.Update();
                                EnsureFolderPublish(folder);

                                if (folder.UniqueId == newPage.ListItem.ParentList.RootFolder.UniqueId)
                                {
                                    // We are setting the Pages library's root folder's welcome page, so let's assume this means we also need to set it as the website's welcome page as well
                                    var webRootFolder = newPage.ListItem.ParentList.ParentWeb.RootFolder;
                                    webRootFolder.WelcomePage = string.Format(CultureInfo.InvariantCulture, "{0}/{1}", publishingWeb.PagesListName, newPage.Name);
                                    webRootFolder.Update();

                                    EnsureFolderPublish(folder);
                                }
                            }

                            newPage.ListItem.Update();
                        }
                    }
                }
            }

            return newPage;
        }