简体   繁体   中英

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

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 pages 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

Here is the code 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");
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM