Last active
July 19, 2018 15:08
-
-
Save vgrem/7551435 to your computer and use it in GitHub Desktop.
Create Wiki page via CSOM in SharePoint 2010
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
$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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
when i tried to add file to folder "$wikiPages.RootFolder.Files.Add($file)" its showing "Access Denied" error.