1

The following appears to be the way to create a web part page in Sharepoint 2010 using Powershell:

$web = Get-SPWeb http://somesite
$pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
$pageLayout = $pWeb.GetAvailablePageLayouts() | Where { $_.Name -eq "BlankWebPartPage.aspx" }
$folder = $web.GetFolder("SitePages")
$page = $pWeb.AddPublishingPage("spamDoc.aspx", $pageLayout, $folder)

But when I run it I get:

PS C:\Users\spadmin> $page = $pWeb.AddPublishingPage("spamDoc.aspx", $pageLayout, $folder)
Exception calling "AddPublishingPage" with "3" argument(s): "Invalid field name. {fa564e0f-0c70-4ab9-b863-0177e6ddd247}
At line:1 char:32
+ $page = $pWeb.AddPublishingPage <<<< ("spamDoc.aspx", $pageLayout, $folder)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

I am trying to create a new web part page in the site's Site Pages library. Any help appreciated.

  • i think change the name of the page and also try to remove the folder parameter... – Waqas Sarwar MVP Dec 18 '14 at 18:47
  • I have to have the folder parameter, I want the page built in the SitePages library. If you do not use the folder parameter, the page is built in the Pages library by default. – Michael Frederick Dec 19 '14 at 5:35
1

I think it's whining about the spamdoc.aspx, instead try without the .aspx. Try this instead.

$web = Get-SPWeb http://somesite
$pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
$pageLayout = $pWeb.GetAvailablePageLayouts() | Where { $_.Name -eq "BlankWebPartPage.aspx" }
$folder = $web.GetFolder("SitePages")
$page = $pWeb.AddPublishingPage("spamDoc", $pageLayout, $folder)
  • OK, I removed the .aspx from the page name and got: PS C:\Users\spadmin> $page = $pWeb.AddPublishingPage("spamDoc", $pageLayout, $folder) Exception calling "AddPublishingPage" with "3" argument(s): "The extension for the page URL name is not valid. Page URL names must have an .aspx exte nsion. Parameter name: name" At line:1 char:32 + $page = $pWeb.AddPublishingPage <<<< ("spamDoc", $pageLayout, $folder) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException – Michael Frederick Dec 19 '14 at 5:30
  • Nevermind, it's whining about the folder. Try without the folder. $page = $pWeb.AddPublishingPage("spamDoc", $pageLayout) – Mike Dec 19 '14 at 6:04
  • As I said above, if you don't specify the folder, it defaults to the Pages library. That's not where I want the page built. – Michael Frederick Dec 21 '14 at 0:18
  • I know, I was saying to test it to see if the folder is the issue at hand. Then we can find out what's wrong with $folder = $web.GetFolder("SitePages"). Try $web.Folders["SitePages"] or the folder GUID $web.GetFolder(GUID) – Mike Dec 21 '14 at 17:22
  • Using $web.Folders["SitePages"] gave the same result. If you mean to get the SPFolder object by using $web.GetFolder[GUID], then it will give the same result. The signature for the appropriate 3 parameter call to AddPublishingPage is Microsoft.SharePoint.Publishing.PublishingPage AddPublishingPage(string name, Microsoft.SharePoint.Publishing.PageLayout layout, Microsoft.SharePoint.SPFolder folder), which means it wants a SPFolder object, which is what I am giving it. I checked the SPFolder object in the first 2 cases and it was valid. – Michael Frederick Jan 16 '15 at 4:42
0

Try following code:

$web = Get-SPWeb http://somesite
$pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
$pageLayout = $pWeb.GetAvailablePageLayouts() | Where { $_.Name -eq "BlankWebPartPage.aspx" }
$page = $pWeb.GetPublishingPages().Add("spamDoc.aspx", $pageLayout)

UPDATE

Site Pages library allows creation of Wiki pages and Web Part Pages. BlankWebPartPage.aspx has a Page Layout Content Type and this type of page can be created in a publishing library aka Pages library. SPWeb.GetPublishingPages().Add internally looks for this Pages library and adds the page to it. If you go into the Pages library and click on New Document and select Article Page you can find BlankWebPartPage.aspx in the list of available page layouts. Now if you go ahead and add Article Page content type to Site Pages library and then try to create a BlankWebPartPage.aspx or any other page from Article Page content type, you will get an error like this:

Value does not fall within the expected range. at Microsoft.SharePoint.Utilities.SPUtility.CreateParentFoldersForFile(SPList list, String url, Boolean createAsListItem) at Microsoft.SharePoint.Publishing.Internal.CodeBehind.CreatePagePage.NewPageItemSave(String pageName, PageLayout pageLayout, String folderUrl, Boolean redirect) at Microsoft.SharePoint.Publishing.Internal.CodeBehind.CreatePagePage.CreateStandardPage(String pageName) at Microsoft.SharePoint.Publishing.Internal.CodeBehind.CreatePagePage.ButtonCreatePage_Click(Object sender, EventArgs e) at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
http://aissp2013/_layouts/15/CreatePage.aspx?List={353535BD-BF00-4280-B3C4-AB64F28A5720}&RootFolder=/sites/Team/SitePages&ContentTypeId=0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D002035528A8BBF824DA4DA7133B3E80214&Source=/sites/Team/SitePages/Forms/AllPages.aspx /_layouts/15/CreatePage.aspx

SPUtility.CreateParentFoldersForFile verifies that the new item based on a publishing content type is stored in the Pages library. If not you get the error above. This means that Publishing Content types work only in the Publishing Library, aka. Pages library.

  • How will this build the new page in the SitePages folder? – Michael Frederick Dec 21 '14 at 0:19
  • Sorry, I didn't notice that you wanted to create page in site pages library. This script will create page in pages library. – Nadeem Yousuf Dec 21 '14 at 3:50
  • So no one knows how to do this? – Michael Frederick Jan 16 '15 at 4:29
  • @MichaelFrederick,see my updated answer. – Nadeem Yousuf Jan 16 '15 at 5:39
  • That's interesting. Maybe I am trying to use the wrong page template, because our site has many web part pages in the SitePages library, and they all work fine. The UI allows you to create them there. So I guess I either need to try a different template or a different creation method. – Michael Frederick Jan 17 '15 at 6:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.