using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Search.Query;
using Newtonsoft.Json.Linq;
using OfficeDevPnP.Core.Pages;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security;
using System.Text;
using System.Threading.Tasks;
namespace ConfluenceToSharePointMigrator
{
///
/// Migrate Confluence space to SharePoint site.
///
class Migration
{
public static Guid webId = Guid.Empty;
public static Guid siteId = Guid.Empty;
public static NavigationNode parentNode = null;
///
/// Migration Confluence to Sharepoint
///
/// Local location of exported Confluence
/// Client context of SharePoint site
public void MigrateConfluenceToSharePoint(string sharePointSiteURL, string exportedLocation, ClientContext clientContext)
{
Web web1 = clientContext.Web;
clientContext.Load(web1);
clientContext.ExecuteQueryRetry();
HTMLFileProcess htmlFileProcess = new HTMLFileProcess();
string[] htmlFiles = Directory.GetFiles(exportedLocation, "*.html");
Web web = clientContext.Web;
clientContext.Load(web.Lists);
clientContext.Load(web, w => w.Title);
clientContext.Load(web);
clientContext.Load(clientContext.Site);
clientContext.ExecuteQueryRetry();
webId = clientContext.Web.Id;
siteId = clientContext.Site.Id;
if (clientContext.WebExistsFullUrl(sharePointSiteURL))
{
foreach (string htmlFile in htmlFiles)
{
//set page name from html file.
string pageName = htmlFileProcess.SharePointPageNameFromHtml(new FileInfo(htmlFile).Name, htmlFile);
Console.WriteLine(pageName);
string htmlContent = System.IO.File.ReadAllText(htmlFile);
//Create the Page
if (!pageName.EndsWith("Home") && !pageName.EndsWith("index"))
{
var page = clientContext.Web.AddClientSidePage(pageName + ".aspx", true);
page.AddSection(CanvasSectionTemplate.OneColumn, 0);
page.Save();
CanvasSection section = page.Sections[0];
//Remove unwanted content.
htmlContent = htmlFileProcess.FrameHTMLContentForSharePointPage(htmlContent);
//Upload the files and replace the link.
htmlContent = htmlFileProcess.UploadImageURLToHtml(htmlContent, exportedLocation, sharePointSiteURL, clientContext);
ClientSideText pageConent = new ClientSideText() { Text = htmlContent };
page.AddControl(pageConent, section.Columns[0], 0);
page.PageTitle = pageName;
page.Save();
page.Publish();
Console.WriteLine(" - Page published");
}
}
}
}
///
/// Upload image or file.
///
/// image or file local location
/// ClientContext of SharePoint site
public void SharePointFileUpload(string source_file, ClientContext ctx)
{
var folder = ctx.Web.GetFolderByServerRelativeUrl("Shared Documents");
System.IO.FileInfo file = new FileInfo(source_file);
if (!System.IO.File.Exists(source_file))
Console.WriteLine("File not fount: " + source_file);
else
{
FileStream fs = System.IO.File.OpenRead(source_file);
string source_filename = Path.GetFileName(source_file);
FileCreationInformation fci = new FileCreationInformation();
fci.Overwrite = true;
fci.ContentStream = fs;
fci.Url = source_filename;
var file_upload = folder.Files.Add(fci);
ctx.Load(file_upload);
ctx.ExecuteQuery();
fs.Close();
}
}
}
}