如何 update/edit SharePoint 经典页面 content/aspx 使用 CSOM
How to update/edit SharePoint classic pages content/aspx using CSOM
我们确实成功地将内容从 MediaWiki 迁移到 SharePoint 经典页面。然而,一些来自 MediaWiki 的内容包含我们不需要的额外信息
有没有办法通过 CSOM 更新我们在将内容从 MediaWiki 迁移到 SharePoint 时生成的所有 ASPX 页面?
我们只需要从我们生成的所有页面中删除一些页脚。
先谢谢大家的帮助
这是对我有用的代码
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");
}
}
我们确实成功地将内容从 MediaWiki 迁移到 SharePoint 经典页面。然而,一些来自 MediaWiki 的内容包含我们不需要的额外信息
有没有办法通过 CSOM 更新我们在将内容从 MediaWiki 迁移到 SharePoint 时生成的所有 ASPX 页面?
我们只需要从我们生成的所有页面中删除一些页脚。
先谢谢大家的帮助
这是对我有用的代码
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");
}
}