I would like to add a couple of libraries as web parts in the wiki page "SitesPages/Home.aspx" using CSOM in SharePoint Online.

Can anyone tell me how can I do that? An example will be very helpful.

I found this blog, and it inserts a web part to a web part page. I would like to insert it in a wiki page. And another thing with this code is using xml as web part.

up vote 1 down vote accepted

Check this https://stackoverflow.com/questions/27268492/how-to-add-a-web-part-into-a-sitepages-home-aspx-using-csom

public static void AddWebPartIntoWikiPage(ClientContext context, string pageUrl, string webPartXml)
{
        var page = context.Web.GetFileByServerRelativeUrl(pageUrl);
        var webPartManager = page.GetLimitedWebPartManager(PersonalizationScope.Shared);

        var importedWebPart = webPartManager.ImportWebPart(webPartXml);
        var webPart = webPartManager.AddWebPart(importedWebPart.WebPart, "wpz", 0);
        context.Load(webPart);
        context.ExecuteQuery();

        string marker = String.Format(CultureInfo.InvariantCulture, "<div class=\"ms-rtestate-read ms-rte-wpbox\" contentEditable=\"false\"><div class=\"ms-rtestate-read {0}\" id=\"div_{0}\"></div><div style='display:none' id=\"vid_{0}\"></div></div>", webPart.Id);
        ListItem item = page.ListItemAllFields;
        context.Load(item);
        context.ExecuteQuery();
        item["PublishingPageContent"] = marker; 
        item.Update();
        context.ExecuteQuery();
}

Usage

var webPartXml = System.IO.File.ReadAllText(filePath);
using (var ctx = new ClientContext(webUri))
{
    AddWebPartIntoWikiPage(ctx, wikiPageUrl,webPartXml);
}

Wiki pages do not have zones; web parts are placed inline with the text content. could programmatically reference the tags in your Wiki Page content.

  • Thanks Tarun. I will give it a try and get back to you in a tick. – Burre Ifort Jul 5 '17 at 11:53

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.