Someone who can point me into the right direction? Code snippets?


Get the file attribute from the page and then get the LimitedWebPartManager.

You add the webpart definition to the page. You can donwload it from the web part gallery but incase of a listview webpart I think you need to generate the correct xml yourself due to the references to lists, views, fields, etc....

public Guid AddWebPartToPage( ClientContext ctx, File file, string webpartXml, string zone, int zoneIndex)
LimitedWebPartManager lwp = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
            ctx.Load(lwp.WebParts, wps => wps.Include(wp1 => wp1.WebPart.Title, wp1 => wp1.Id));

            WebPart wp = lwp.ImportWebPart(webpartXml).WebPart;
            WebPartDefinition wpdNew = lwp.AddWebPart(wp, zone, zoneIndex);
            ctx.Load(wpdNew, d => d.Id);

I can't say I have done this with CSOM as yet, I have added my webparts to the wiki page which is deployed with a stapled feature. This is an easy option (can be made easier by saving the site as template and extracting the page definition) and may suit your needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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