2

I would like to create a wiki page using powershell and would like set "Two column with Header" as the page layout instead of the default "One Column" layout. Able to create the page however dont know how to update the page layout. Kindly help.

note: Publishing feature is not enabled on the web.

## Main Sharepoint Site
$site = "http://Webapplication";

## Sharepoint Wiki sub-site
$web = "/subsite";

## Library of Wiki Pages       
$Library = "Site Pages";

## Name of the Wiki Pages       
$pageName = "NewHomePage.aspx";

## Setup Basic sites and pages
$spSite = Get-SPSite $site;
$spWeb  = Get-SPWeb ($site+$web);
$spLibrary = $spWeb.Lists[$Library];
$pageFolder = $spLibrary.RootFolder;
$spWIKIPage = $pageFolder.Files.Add($pageFolder.ServerRelativeUrl +"/" + $pageName, [Microsoft.SharePoint.SPTemplateFileType]::WikiPage);

$spWIKIItem = $spWIKIPage.Item;
$spWIKIItem[[Microsoft.SharePoint.SPBuiltInFieldId]::WikiField] = "Template Preview" ;
$spWIKIItem.UpdateOverwriteVersion();

# Dispose the Objects
$spWeb.Dispose();
$spSite.Dispose();

It works if html contents are directly added. However does it make any performance impact?

### Variables ###
## Main Sharepoint Site
$site = "http://webapplication";

## Sharepoint Wiki sub-site
$web = "/subsite";

## Library of Wiki Pages       
$Library = "Site Pages";

## Name of the Wiki Pages       
$pageName = "NewHomePage.aspx";

## Layout of the Wiki Pages 
$pageLayout = '<div class="ExternalClassCFBA8CDC81B34264A81647E2260499B4"><table id="layoutsTable" style="width:100%"><tbody><tr style="vertical-align:top"><td colspan="2"><div class="ms-rte-layoutszone-outer" style="width:100%"><div class="ms-rte-layoutszone-inner"><div class="ExternalClass84D2F659CA274C02B313B2039A7DCEF6">Template Preview</div></div></div></td></tr><tr style="vertical-align:top"><td style="width:49.95%"><div class="ms-rte-layoutszone-outer" style="width:100%"><div class="ms-rte-layoutszone-inner"><div class="ExternalClass84D2F659CA274C02B313B2039A7DCEF6"> </div></div> </div></td><td style="width:49.95%"><div class="ms-rte-layoutszone-outer" style="width:100%"><div class="ms-rte-layoutszone-inner"></div> </div></td></tr></tbody></table><span id="layoutsData" style="display:none">true,false,2</span></div>';

## Setup Basic sites and pages
$spSite = Get-SPSite $site;
$spWeb  = Get-SPWeb ($site+$web);
$spLibrary = $spWeb.Lists[$Library];
$pageFolder = $spLibrary.RootFolder;
$spWIKIPage = $pageFolder.Files.Add($pageFolder.ServerRelativeUrl +"/" + $pageName, [Microsoft.SharePoint.SPTemplateFileType]::WikiPage);
$spWIKIPage.Title = $web.Title + " Home Page";

$spWIKIItem = $spWIKIPage.Item;
$spWIKIItem[[Microsoft.SharePoint.SPBuiltInFieldId]::WikiField] = $pageLayout;
$spWIKIItem.UpdateOverwriteVersion();

# Dispose the Objects
$spWeb.Dispose();
$spSite.Dispose();
  • Would like to minimize any kind of DB operations, so there a better way of doing it? – Karthikeyan Dec 2 '13 at 19:08
2

To specify Page Layout when creating Wiki page use OOTB SPField PublishingPageLayout, for example:

$wikiPage["PublishingPageLayout"] = "/_catalogs/masterpage/EnterpriseWiki.aspx, Basic Page"

How to create Wiki page in SharePoint 2010

Below is provided the code for creating wiki pages in SharePoint 2010:

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")
            $load = [Microsoft.SharePoint.Client.ClientContext].GetMethod("Load") 
            $listLoad = $load.MakeGenericMethod([Microsoft.SharePoint.Client.List]) 
            $listLoad.Invoke($ctx,@($wikiPages,$null)) 
            $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)
            $fileLoad = $load.MakeGenericMethod([Microsoft.SharePoint.Client.File]) 
            $fileLoad.Invoke($ctx,@($wikiFile,$null))

            $wikiPage = $wikiFile.ListItemAllFields
            $wikiPage["PublishingPageContent"] = $pageContent
            $wikiPage["PublishingPageLayout"] = "/_catalogs/masterpage/EnterpriseWiki.aspx, Basic Page"
            $wikiPage.Update()
            $ctx.ExecuteQuery();

}


$credentials = New-Object System.Net.NetworkCredential('username', 'password','domain')
$webUrl = 'http://intranet.contoso.net/kb/'
$pageName = 'MyFirstWikiPage.aspx'
$pageContent  = '<h1>Welcome to the Knowledge Base!</h1>'
CreateWikiPage $webUrl $credentials $pageName $pageContent

References


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.