Create Wiki Pages in site pages library using Power shell in sharepoint 2010. Please help.
3 Answers
SharePoint SSOM comes with built-in method for creating wiki pages SPUtility.CreateNewWikiPage method, for example:
$WebUrl = "http://contoso.intranet.sp.dev/faq/"
$web = Get-SPWeb $WebUrl
$wikiPages = $web.Lists["Pages"]
$wikiPage = [Microsoft.SharePoint.Utilities.SPUtility]::CreateNewWikiPage($wikiPages, [System.String]::Format("{0}/{1}", $wikiPages.RootFolder.ServerRelativeUrl, "MyFAQ.aspx"));
but unfortunately it creates non-editable wiki pages due to inability to correctly specify page layout.
How to create wiki page using SSOM in PowerShell
if ((Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null)
{
Add-PsSnapin Microsoft.SharePoint.PowerShell
}
Function Create-WikiPage([string]$WebUrl,[string]$PageName,[string]$PageContent)
{
$web = Get-SPWeb $WebUrl
$wikiPages = $web.Lists["Pages"]
$pSite = New-Object Microsoft.SharePoint.Publishing.PublishingSite($web.Site)
$pageLayoutUrl = $web.Site.Url + "/_catalogs/masterpage/EnterpriseWiki.aspx"
$pageLayout = $pSite.PageLayouts[$pageLayoutUrl]
$pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
$wikiPage = $pWeb.GetPublishingPages().Add($PageName, $pageLayout);
$wikiPage.Title = [System.IO.Path]::GetFileNameWithoutExtension($PageName)
$wikiItem = $wikiPage.ListItem
$wikiItem["PublishingPageContent"] = $pageContent
$wikiItem.Update()
}
Usage
Create-WikiPage -WebUrl "http://contoso.intranet.sp.dev/faq/" -PageName "FAQ.aspx" -PageContent "Welcome to FAQ"
How to create a wiki page using CSOM in PowerShell
Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\Microsoft.SharePoint.Client.dll'
Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
function CreateWikiPage()
{
param(
[Parameter(Mandatory=$true)][string]$webUrl,
[Parameter(Mandatory=$false)][System.Net.NetworkCredential]$credentials,
[Parameter(Mandatory=$true)][string]$pageName,
[Parameter(Mandatory=$true)][string]$pageContent
)
$templateRedirectionPageMarkup = '<%@ Page Inherits="Microsoft.SharePoint.Publishing.TemplateRedirectionPage,Microsoft.SharePoint.Publishing,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" %> <%@ Reference VirtualPath="~TemplatePageUrl" %> <%@ Reference VirtualPath="~masterurl/custom.master" %>';
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($webUrl)
$ctx.Credentials = $credentials
$wikiPages = $ctx.Web.Lists.GetByTitle("Pages")
Invoke-LoadMethod -ClientObject $wikiPages
$ctx.ExecuteQuery()
$file = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$file.Url = $pageName
$file.Content = [System.Text.Encoding]::UTF8.GetBytes($templateRedirectionPageMarkup)
$file.Overwrite = $true
$wikiFile = $wikiPages.RootFolder.Files.Add($file)
Invoke-LoadMethod -ClientObject $wikiFile
$wikiPage = $wikiFile.ListItemAllFields
$wikiPage["PublishingPageContent"] = $pageContent
$wikiPage["PublishingPageLayout"] = "/_catalogs/masterpage/EnterpriseWiki.aspx, Basic Page"
$wikiPage.Update()
$ctx.ExecuteQuery();
}
Usage
$credentials = New-Object System.Net.NetworkCredential('username', 'password','domain')
$webUrl = 'http://contoso.intranet.com/knowledgebase/'
$pageName = 'MyFirstWikiPage.aspx'
$pageContent = '<h1>Welcome to the Knowledge Base!</h1>'
CreateWikiPage $webUrl $credentials $pageName $pageContent
Please follow this post for a more details.
$wiki = $web.GetList($web.Url.ToString() + "/SitePages")
$rootFolder = $wiki.RootFolder;
$wikipage = $rootFolder.Files.Add($rootFolder.ServerRelativeUrl +
"/test.aspx", [Microsoft.SharePoint.SPTemplateFileType]::WikiPage)
$wikiItem = $wikipage.Item;
$wikiItem[[Microsoft.SharePoint.SPBuiltInFieldId]::WikiField] = "Test";
$wikiItem.UpdateOverwriteVersion();
Use $rootFolder.ServerRelativeUrl to get over "Value does not fall within the expected range" exception when adding wiki apge to site pages using powershell
try this one:
$listName="A"
$siteURL="http://serverName:1111"
$site=Get-SPSite $siteURL
$web=$site.RootWeb
$list=$web.Lists[$listName]
$rootFolder = $list.RootFolder
$wikiPage = $rootFolder.Files.Add("/"+$rootFolder+"/Home.aspx", [Microsoft.SharePoint.SPTemplateFileType]::WikiPage)
$wikiItem = $wikiPage.Item
$wikiItem[[Microsoft.SharePoint.SPBuiltInFieldId]::WikiField] = "Template Preview"
$wikiItem.UpdateOverwriteVersion()
-
This is incorrect since wiki page will be created "non-editable", besides you could utilize SSOM SPUtility.CreateNewWikiPage method Aug 21, 2014 at 9:49
-
1Exception calling "Add" with "2" argument(s): "" At C:\sandeeptest\sales\Testwikipage.ps1:7 char:36 + $wikiPage = $rootFolder.Files.Add <<<< ("/"+$rootFolder+"/Home.aspx", [Micr osoft.SharePoint.SPTemplateFileType]::WikiPage) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException– JonAug 22, 2014 at 8:10
-
2