SOLVED

How to update/edit SharePoint classic pages content/aspx using CSOM

Brass Contributor

We did successfully migrate the content from MediaWiki into the SharePoint classic page. However, some of the content that came from the MediaWiki has extra information that we don't need.

 

Is there a way through CSOM to update all the ASPX page we generated when migrated content from MediaWiki to SharePoint?

 

All we need is to remove some footers from all the pages that we generated.

Thank you in advance for everyone's help

2 Replies

@MikhailPodolski You can access SharePoint Wiki pages with CSOM. SharePoint stores the content in a List which means you could also use the RestAPI.

This article might help with CSOM 

https://sharepoint.stackexchange.com/questions/221767/extract-html-from-wikipage-in-sharepoint-onlin...

 

 

 

 

best response confirmed by MikhailPodolski (Brass Contributor)
Solution

Here is the answer that worked for me

 

static void Main(string[] args)
{
try
{
ClientContext ctx = GetClientContext("https://tenant.sharepoint.com/sites/sitename",
"usernaname@tenant.onmicrosoft.com", "password");
var listTitle = "Site Pages";
var list = ctx.Web.Lists.GetByTitle(listTitle);
var items = list.GetItems(CamlQuery.CreateAllItemsQuery());
ctx.Load(items, icol => icol.Include(i => i["WikiField"], i => i["CanvasContent1"], i => i["FileRef"], i => i.ContentType));
ctx.ExecuteQuery();
foreach (var item in items)
{

switch (item.ContentType.Name)
{
case "Site Page":
if (item["CanvasContent1"] != null)
{
if (item["CanvasContent1"].ToString().Contains("certain pattern"))
{
ProcessSiteHTML(item["CanvasContent1"].ToString(), item["FileRef"].ToString(), item, ctx, "CanvasContent1");
}
}
break;
case "Wiki Page":
if (item["WikiField"] != null)
{
if (item["WikiField"].ToString().Contains("certain pattern"))
{
ProcessSiteHTML(item["WikiField"].ToString(), item["FileRef"].ToString(), item, ctx, "WikiField");
}
}
break;

}
}

}
catch (Exception e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Main " + e.Message);
WriteLog(fileName, "Main " + e.Message);
}
finally
{
Console.ForegroundColor = ConsoleColor.White;
}
}

private static void ProcessSiteHTML(string page, string pageName, ListItem item, ClientContext ctx, string pageType)
{
string pattern = "Regular expression pattern";
System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(pattern, System.Text.RegularExpressions.RegexOptions.Multiline);
System.Text.RegularExpressions.MatchCollection matched = rg.Matches(page.ToLower());

if (matched.Count > 0)
{
System.Text.RegularExpressions.Match m =
System.Text.RegularExpressions.Regex.Match(page, pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);

string updatedField = System.Text.RegularExpressions.Regex.Replace(page,
m.Value, string.Empty, System.Text.RegularExpressions.RegexOptions.Multiline | System.Text.RegularExpressions.RegexOptions.IgnoreCase);
item[pageType] = updatedField;
item.Update();
ctx.ExecuteQuery();
Console.WriteLine(pageName + " has been updated");
}
}

1 best response

Accepted Solutions
best response confirmed by MikhailPodolski (Brass Contributor)
Solution

Here is the answer that worked for me

 

static void Main(string[] args)
{
try
{
ClientContext ctx = GetClientContext("https://tenant.sharepoint.com/sites/sitename",
"usernaname@tenant.onmicrosoft.com", "password");
var listTitle = "Site Pages";
var list = ctx.Web.Lists.GetByTitle(listTitle);
var items = list.GetItems(CamlQuery.CreateAllItemsQuery());
ctx.Load(items, icol => icol.Include(i => i["WikiField"], i => i["CanvasContent1"], i => i["FileRef"], i => i.ContentType));
ctx.ExecuteQuery();
foreach (var item in items)
{

switch (item.ContentType.Name)
{
case "Site Page":
if (item["CanvasContent1"] != null)
{
if (item["CanvasContent1"].ToString().Contains("certain pattern"))
{
ProcessSiteHTML(item["CanvasContent1"].ToString(), item["FileRef"].ToString(), item, ctx, "CanvasContent1");
}
}
break;
case "Wiki Page":
if (item["WikiField"] != null)
{
if (item["WikiField"].ToString().Contains("certain pattern"))
{
ProcessSiteHTML(item["WikiField"].ToString(), item["FileRef"].ToString(), item, ctx, "WikiField");
}
}
break;

}
}

}
catch (Exception e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Main " + e.Message);
WriteLog(fileName, "Main " + e.Message);
}
finally
{
Console.ForegroundColor = ConsoleColor.White;
}
}

private static void ProcessSiteHTML(string page, string pageName, ListItem item, ClientContext ctx, string pageType)
{
string pattern = "Regular expression pattern";
System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(pattern, System.Text.RegularExpressions.RegexOptions.Multiline);
System.Text.RegularExpressions.MatchCollection matched = rg.Matches(page.ToLower());

if (matched.Count > 0)
{
System.Text.RegularExpressions.Match m =
System.Text.RegularExpressions.Regex.Match(page, pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);

string updatedField = System.Text.RegularExpressions.Regex.Replace(page,
m.Value, string.Empty, System.Text.RegularExpressions.RegexOptions.Multiline | System.Text.RegularExpressions.RegexOptions.IgnoreCase);
item[pageType] = updatedField;
item.Update();
ctx.ExecuteQuery();
Console.WriteLine(pageName + " has been updated");
}
}

View solution in original post