SOLVED

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

%3CLINGO-SUB%20id%3D%22lingo-sub-1220804%22%20slang%3D%22en-US%22%3EHow%20to%20update%2Fedit%20SharePoint%20classic%20pages%20content%2Faspx%20using%20CSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1220804%22%20slang%3D%22en-US%22%3E%3CP%3EWe%20did%20successfully%20migrate%20the%20content%20from%20MediaWiki%20into%20the%20SharePoint%20classic%20page.%20However%2C%20some%20of%20the%20content%20that%20came%20from%20the%20MediaWiki%20has%20extra%20information%20that%20we%20don't%20need.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIs%20there%20a%20way%20through%20CSOM%20to%20update%20all%20the%20ASPX%20page%20we%20generated%20when%20migrated%20content%20from%20MediaWiki%20to%20SharePoint%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAll%20we%20need%20is%20to%20remove%20some%20footers%20from%20all%20the%20pages%20that%20we%20generated.%3C%2FP%3E%3CP%3EThank%20you%20in%20advance%20for%20everyone's%20help%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1220804%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESharePoint%20Online%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESites%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1221468%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20update%2Fedit%20SharePoint%20classic%20pages%20content%2Faspx%20using%20CSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1221468%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F482737%22%20target%3D%22_blank%22%3E%40MikhailPodolski%3C%2FA%3E%26nbsp%3BYou%20can%20access%20SharePoint%20Wiki%20pages%20with%20CSOM.%20SharePoint%20stores%20the%20content%20in%20a%20List%20which%20means%20you%20could%20also%20use%20the%20RestAPI.%3C%2FP%3E%0A%3CP%3EThis%20article%20might%20help%20with%20CSOM%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fsharepoint.stackexchange.com%2Fquestions%2F221767%2Fextract-html-from-wikipage-in-sharepoint-online-using-csom%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fsharepoint.stackexchange.com%2Fquestions%2F221767%2Fextract-html-from-wikipage-in-sharepoint-online-using-csom%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1252608%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20update%2Fedit%20SharePoint%20classic%20pages%20content%2Faspx%20using%20CSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1252608%22%20slang%3D%22en-US%22%3E%3CP%3EHere%20is%20the%20answer%20that%20worked%20for%20me%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Estatic%20void%20Main(string%5B%5D%20args)%3CBR%20%2F%3E%7B%3CBR%20%2F%3Etry%3CBR%20%2F%3E%7B%3CBR%20%2F%3EClientContext%20ctx%20%3D%20GetClientContext(%22%3CA%20href%3D%22https%3A%2F%2Ftenant.sharepoint.com%2Fsites%2Fsitename%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Ftenant.sharepoint.com%2Fsites%2Fsitename%3C%2FA%3E%22%2C%3CBR%20%2F%3E%22usernaname%40tenant.onmicrosoft.com%22%2C%20%22password%22)%3B%3CBR%20%2F%3Evar%20listTitle%20%3D%20%22Site%20Pages%22%3B%3CBR%20%2F%3Evar%20list%20%3D%20ctx.Web.Lists.GetByTitle(listTitle)%3B%3CBR%20%2F%3Evar%20items%20%3D%20list.GetItems(CamlQuery.CreateAllItemsQuery())%3B%3CBR%20%2F%3Ectx.Load(items%2C%20icol%20%3D%26gt%3B%20icol.Include(i%20%3D%26gt%3B%20i%5B%22WikiField%22%5D%2C%20i%20%3D%26gt%3B%20i%5B%22CanvasContent1%22%5D%2C%20i%20%3D%26gt%3B%20i%5B%22FileRef%22%5D%2C%20i%20%3D%26gt%3B%20i.ContentType))%3B%3CBR%20%2F%3Ectx.ExecuteQuery()%3B%3CBR%20%2F%3Eforeach%20(var%20item%20in%20items)%3CBR%20%2F%3E%7B%3C%2FP%3E%3CP%3Eswitch%20(item.ContentType.Name)%3CBR%20%2F%3E%7B%3CBR%20%2F%3Ecase%20%22Site%20Page%22%3A%3CBR%20%2F%3Eif%20(item%5B%22CanvasContent1%22%5D%20!%3D%20null)%3CBR%20%2F%3E%7B%3CBR%20%2F%3Eif%20(item%5B%22CanvasContent1%22%5D.ToString().Contains(%22certain%20pattern%22))%3CBR%20%2F%3E%7B%3CBR%20%2F%3EProcessSiteHTML(item%5B%22CanvasContent1%22%5D.ToString()%2C%20item%5B%22FileRef%22%5D.ToString()%2C%20item%2C%20ctx%2C%20%22CanvasContent1%22)%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3Ebreak%3B%3CBR%20%2F%3Ecase%20%22Wiki%20Page%22%3A%3CBR%20%2F%3Eif%20(item%5B%22WikiField%22%5D%20!%3D%20null)%3CBR%20%2F%3E%7B%3CBR%20%2F%3Eif%20(item%5B%22WikiField%22%5D.ToString().Contains(%22certain%20pattern%22))%3CBR%20%2F%3E%7B%3CBR%20%2F%3EProcessSiteHTML(item%5B%22WikiField%22%5D.ToString()%2C%20item%5B%22FileRef%22%5D.ToString()%2C%20item%2C%20ctx%2C%20%22WikiField%22)%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3Ebreak%3B%3C%2FP%3E%3CP%3E%7D%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%7D%3CBR%20%2F%3Ecatch%20(Exception%20e)%3CBR%20%2F%3E%7B%3CBR%20%2F%3EConsole.ForegroundColor%20%3D%20ConsoleColor.Red%3B%3CBR%20%2F%3EConsole.WriteLine(%22Main%20%22%20%2B%20e.Message)%3B%3CBR%20%2F%3EWriteLog(fileName%2C%20%22Main%20%22%20%2B%20e.Message)%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3Efinally%3CBR%20%2F%3E%7B%3CBR%20%2F%3EConsole.ForegroundColor%20%3D%20ConsoleColor.White%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3Eprivate%20static%20void%20ProcessSiteHTML(string%20page%2C%20string%20pageName%2C%20ListItem%20item%2C%20ClientContext%20ctx%2C%20string%20pageType)%3CBR%20%2F%3E%7B%3CBR%20%2F%3Estring%20pattern%20%3D%20%22Regular%20expression%20pattern%22%3B%3CBR%20%2F%3ESystem.Text.RegularExpressions.Regex%20rg%20%3D%20new%20System.Text.RegularExpressions.Regex(pattern%2C%20System.Text.RegularExpressions.RegexOptions.Multiline)%3B%3CBR%20%2F%3ESystem.Text.RegularExpressions.MatchCollection%20matched%20%3D%20rg.Matches(page.ToLower())%3B%3C%2FP%3E%3CP%3Eif%20(matched.Count%20%26gt%3B%200)%3CBR%20%2F%3E%7B%3CBR%20%2F%3ESystem.Text.RegularExpressions.Match%20m%20%3D%3CBR%20%2F%3ESystem.Text.RegularExpressions.Regex.Match(page%2C%20pattern%2C%20System.Text.RegularExpressions.RegexOptions.IgnoreCase)%3B%3C%2FP%3E%3CP%3Estring%20updatedField%20%3D%20System.Text.RegularExpressions.Regex.Replace(page%2C%3CBR%20%2F%3Em.Value%2C%20string.Empty%2C%20System.Text.RegularExpressions.RegexOptions.Multiline%20%7C%20System.Text.RegularExpressions.RegexOptions.IgnoreCase)%3B%3CBR%20%2F%3Eitem%5BpageType%5D%20%3D%20updatedField%3B%3CBR%20%2F%3Eitem.Update()%3B%3CBR%20%2F%3Ectx.ExecuteQuery()%3B%3CBR%20%2F%3EConsole.WriteLine(pageName%20%2B%20%22%20has%20been%20updated%22)%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional 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...

 

 

 

 

Highlighted
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");
}
}