Moving from MediaWiki to SharePoint O365 - part 6

Now that we have our scripts in place to emulate MediaWiki functionalities, copied over the images and files, and prepared html-pages for all our relevant articles, we can send the pages to SharePoint.

With this script, you prepare the articles one by one, create files for it, and call a PowerShell script which does the actual uploading (see below). Don't forget to update the rootPath to whatever you're using.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
ini_set('display_errors', 1);
$rootPath = "C:/workspace/PortableSoft/UniServerZ/www/wikimigration/articles/";

//get files from 
$files = scandir($rootPath.'articlesToSend');
$i = 0;
$filename = "";

//check if there are files to process
if(sizeof($files)>2){
 $filename = $files[$i];
 
 //skip the "this" and "one up" files
 while(($filename == "." or $filename == "..") and $i < sizeof($files)){
  $i++;
  $filename = $files[$i];
 }
 
 //get the contents of the file, split out title and content
 $page = file_get_contents($rootPath."articlesToSend/$filename");
 //$title = substr($page,strlen("<h1>"),strpos($page,"</h1>")-strlen("<h1>"));
 //$content = substr($page, strlen($title)+strlen("<h1></h1>"));
 $title = substr($filename,0,strlen($filename)-5);
 $content = $page;
 
 //write data to files
 if(file_put_contents($rootPath."powershell/pageName.txt", $title) === false)
  echo "issue writing title to file<br>";
 if(file_put_contents($rootPath."powershell/pageContent.txt", $content) === false)
  echo "issue writing contents to file<br>";
 
 //call the powershell script to upload the article to SharePoint
 $command = 'powershell "'.$rootPath.'powershell/JV.ps1"';
 $output = shell_exec($command);
 //show the output produced by powershell
 echo "<div><pre>$output</pre></div>";
 
 //move the file from articlesToSend to articlesSent
 if(rename($rootPath."articlesToSend/$filename",$rootPath."articlesSent/$filename") === false){
  //if unsuccessfull: give feedback
  echo "<div>issue moving $filename to articlesSent</div>";
 }
 else{
  //if successfull: continue with next file
  echo '<meta http-equiv="refresh" content="3;URL=http://localhost:8080/wikimigration/articles/uploadToSharepoint.php" />';
  echo "<div>file $filename sent and moved to 'articlesSent'</div>";
 }
}
else {
 echo '<meta http-equiv="refresh" content="3;URL=http://localhost:8080/wikimigration/articles/uploadToSharepoint.php" />';
 echo "no files present";
}
?>

Now for the PowerShell script. Put it in a subdirectory powershell in the articles directory. Don't forget to change the siteURL, your username and your password on line 78. And of course the script directory on line 4.
You'll notice the script is calling a dll. You actually need a bunch of them. Create a subdirectory dll in your powershell folder, and put these dlls there: client.dll, microsoft.office.client.policy.dll, microsoft.office.sharepoint.tools.dll, microsoft.sharepoint.client.dll, microsoft.sharepoint.client.publishing.dll, microsoft.sharepoint.client.publishing.silverlight.dll, microsoft.sharepoint.client.runtime.dll, microsoft.sharepoint.client.silverlight.dll, microsoft.sharepoint.client.silverlight.runtime.dll, microsoft.sharepoint.client.taxonomy.dll, microsoft.sharepoint.client.taxonomy.silverlight.dll, microsoft.sharepoint.client.userprofiles.dll, microsoft.sharepoint.client.userprofiles.phone.dll, microsoft.sharepoint.client.userprofiles.silverlight.dll, microsoft.sharepoint.dll. I'm not sure if they're all needed, but hey, this bunch made it work for me. I'll upload a zip of them somewhere later, if somebody needs them and can't find them anywhere else.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# global vars
$clientContext
$rootSiteUrl
$scriptdir = "C:\workspace\PortableSoft\UniServerZ\www\wikimigration\articles\powershell"

function Initialize-SPPS
{
 [CmdletBinding()]
 param
 (
     [Parameter(Mandatory=$true, Position=1)]
     [string]$siteURL,
  
  [Parameter(Mandatory=$false, Position=2)]
  [bool]$online,

     [Parameter(Mandatory=$false, Position=3)]
     [string]$username,

     [Parameter(Mandatory=$false, Position=4)]
     [string]$password
 )
 Write-Host "Loading the CSOM library" -foregroundcolor black -backgroundcolor yellow
 [Reflection.Assembly]::LoadFrom("$scriptdir\dll\Microsoft.SharePoint.Client.dll")
 Write-Host "Succesfully loaded the CSOM library" -foregroundcolor black -backgroundcolor green

 Write-Host "Create client context for site $siteUrl" -foregroundcolor black -backgroundcolor yellow
 $context = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
 
 $context.RequestTimeOut = 1000 * 60 * 10;

 if ($online)
 {
  Write-Host "Setting SharePoint Online credentials" -foregroundcolor black -backgroundcolor yellow
  
  $context.AuthenticationMode = [Microsoft.SharePoint.Client.ClientAuthenticationMode]::Default
  $securePassword = ConvertTo-SecureString $password -AsPlainText -Force

  $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
  $context.Credentials = $credentials
 }

 Write-Host "Check connection" -foregroundcolor black -backgroundcolor yellow
 $web = $context.Web
 $site = $context.Site
 $context.Load($web)
 $context.Load($site)
 $context.ExecuteQuery()
 
 Set-Variable -Name "clientContext" -Value $context -Scope Global
    Set-Variable -Name "rootSiteUrl" -Value $siteURL -Scope Global
 
 Write-Host "Succesfully connected" -foregroundcolor black -backgroundcolor green
}

Function Create-WikiPage([Microsoft.SharePoint.Client.ClientContext]$Context,[string]$WikiLibraryTitle,[string]$PageName,[string]$PageContent)
{
    $wikiLibrary = $Context.Web.Lists.GetByTitle($wikiLibraryTitle)
    $Context.Load($wikiLibrary.RootFolder)
    $Context.ExecuteQuery()

    $wikiPageInfo = New-Object Microsoft.SharePoint.Client.Utilities.WikiPageCreationInformation
    $wikiPageInfo.WikiHtmlContent = $PageContent
    $wikiPageInfo.ServerRelativeUrl = [String]::Format("{0}/{1}", $wikiLibrary.RootFolder.ServerRelativeUrl, $PageName)
    $wikiFile = [Microsoft.SharePoint.Client.Utilities.Utility]::CreateWikiPageInContextWeb($Context, $wikiPageInfo)
    $context.ExecuteQuery()   
}


# get files for variables
    # get file for pagename
    $pageName = [IO.File]::ReadAllText("$scriptdir\pageName.txt")
    # get file for page contents
    $pageContent = [IO.File]::ReadAllText("$scriptdir\pageContent.txt")


# connect
Initialize-SPPS -siteURL "https://xxxxx.sharepoint.com/sites/yyyyy/" -online $true -username "user@domain.com" -password "yourPass" 

# put page
$fullPageName = "$pageName.aspx"
Write-Host "Adding page" -foregroundcolor black -backgroundcolor yellow
Create-WikiPage -Context $clientContext -WikiLibraryTitle "Site Pages" -PageName $fullPageName -PageContent $pageContent
Write-Host "New page added" -foregroundcolor black -backgroundcolor green


$clientContext.Dispose()


No comments:

Post a Comment