0

SharePoint Online has this awesome functionality where you click on the Page>Edit>Insert and are able to add an existing list as a web part to the home page.

enter image description here

I am trying to implement the same functionality using CSOM, but there seems to be no such option OOTB. What I have so far:

File page = ctx.Web.GetFileByServerRelativeUrl("/sites/powie64/SitePages/Pageee.aspx");
        LimitedWebPartManager wpm = page.GetLimitedWebPartManager(PersonalizationScope.Shared);
        ctx.Load(wpm);
        ctx.Load(wpm.WebParts);
        ctx.ExecuteQuery();
        string xml = ctx.Web.Lists.GetByTitle("Sample List").SchemaXml;
        wpm.ImportWebPart(xml);
        ctx.ExecuteQuery();
        Console.ReadKey();
  1. LimitedWebPartManager does not seem to have any function "AddFromList." That leaves me with AddWebPart() or ImportWebPart().
  2. Adding WebPart requires adding a ready web part. Lists, even though in UI they are visible in the Web Part Tab, are not listed under site webparts:

code

foreach (WebPartDefinition wp in wpm.WebParts)
        {
            ctx.Load(wp.WebPart);
            ctx.ExecuteQuery();
            Console.WriteLine(wp.Id+" "+wp.WebPart.Title+"   "+wp.WebPart.TitleUrl+" "+wp.WebPart.ZoneIndex+" ");
        }
  1. For importing a webpart, we need XML, but List SchemaXML is different from Xml required for a webpart, e.g. here, so direct copy paste is not possible.

Could you please advise how to tackle it? The lists are pretty simple and I do not need to convey almost any settings, just the overview, but it's a lot of lists per web, and 70 subsites per site collection.

EDIT

The edit is additional information to the comment on Rob Windsor's answer:

enter image description here

This is the XML I am using:

static string xemelek = "<?xml version='1.0' encoding=\"utf-8\"?><webParts><webPart xmlns='http://schemas.microsoft.com/WebPart/v3'><metaData><type name='Microsoft.SharePoint.WebPartPages.XsltListViewWebPart, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' /><importErrorMessage>Cannot import this Web Part.</importErrorMessage></metaData><data><properties><property name='ListUrl' type='string' /><property name='ListId' type='System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'>757981FB-184A-441B-952D-9434BE33DC85</property><property name='ListName' type='string'>{757981FB-184A-441B-952D-9434BE33DC85}</property></properties></data></webPart></webParts>";


EDIT 2
Adding working code to close the question:

WebPartDefinition newWebiie= wpm.ImportWebPart(xemelek);
        WebPartDefinition webpartie = wpm.AddWebPart(newWebiie.WebPart, "mainContent", 0);
        ctx.Load(newWebiie);
        ctx.ExecuteQuery();
3
  • The second parameter to the AddWebPart method is the name of the Web Part Zone that will contain the web part. Do you have a Web Part Zone named Left on the page? Dec 3, 2015 at 13:24
  • The zone is ok. I can add existing web parts to this zone without issues.
    – grisha
    Dec 7, 2015 at 14:51
  • Yes we can add CEWP WebPart, but could you please write steps to add default webparts. means existing webparts, (Shared documents, links, caleder etc..) because we did hard coded XML (vai importedXML), but i am not sure how to add (links, Shared docu, calender or lists)webpart to page.
    – user78325
    Sep 2, 2018 at 2:57

1 Answer 1

1

You want to add a XsltListViewWebPart to your page. The basic steps are:

  1. Build the XML defining the web part
  2. Get the LimitedWebPartManager for the page
  3. Create an instance of the web part using the XML (via ImportWebPart)
  4. Add the instance of the web part to the page (via AddWebPart)

For more information see: Add List WebPart To Page CSOM

3
  • Thank you. I am using copy-pasted xml string with my list GUID. I can Import it without issues, but at Adding it wants the output variable as WebPartDefinition, not WebPart, and if I use WPD or var, it throws an error: Specified argument was out of the range of valid values. The webpart is also not added to webpartdefinitionscollection of limitedwebpartmanager. Could you please help?
    – grisha
    Dec 3, 2015 at 11:04
  • I suspect it has never been imported properly (even though there is no error at .ExecuteQuery()) and that's why it cannot be added (value eq null, out-of-range), but no idea how to handle this
    – grisha
    Dec 3, 2015 at 11:19
  • Unbelievable as it seems, reordering the .AddWebPart() method before .ExecuteQuery() did the trick. Thank you for your suggestions, Rob, and for the link. They were very useful.
    – grisha
    Dec 7, 2015 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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