How to create SharePoint pages and add web parts using PowerShell

To create a SharePoint page using PowerShell, you can use the New-SPWebPartPage cmdlet. This cmdlet creates a new Web Part page in the specified document library, with the specified name and layout.

Here is an example of how to use this cmdlet to create a new Web Part page in a document library called "Pages" in the root site of a SharePoint site:

$web = Get-SPWeb http://yoursite $library = $web.Lists["Pages"] $page = $library.RootFolder.Files.Add("page.aspx", [Microsoft.SharePoint.SPTemplateFileType]::WebPartPage) $web.Dispose()

To add a Web Part to the page, you can use the AddWebPart method of the SPLimitedWebPartManager class. This method takes two arguments: the Web Part to add, and a zone to add it to.

Here is an example of how to add a Web Part to the page that was created in the previous example:

$web = Get-SPWeb http://yoursite $page = $web.GetFile("/Pages/page.aspx") $manager = $page.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared) $webPart = New-Object Microsoft.SharePoint.WebPartPages.ContentEditorWebPart $webPart.Content = "Hello, world!" $manager.AddWebPart($webPart, "Main", 1) $web.Dispose()

This will add a Content Editor Web Part to the page, with the content "Hello, world!".

We hope this helps!

We have a skilled SharePoint team and if you need some help implementing your intranet, reach out to us.

Copyright © 2019 Torpedo. All Rights Reserved

Powered by Kentico CMS | Cookie Policy

By using our website you agree to our cookies policy. Read More Close