Search code examples
c#sharepointdeploymentweb-partscsom

Adding SharePoint list webpart to a Specific Page after Initial deployment with CSOM


During the deployment of my sharepoint site, I created a few lists that I'd like to programmatically add to the home page, but I cannot find anything that works.

Right now, I'm getting the following error:

The property or field 'SchemaXml' has not been initialized. 
It has not been requested or the request has not been executed.
It may need to be explicitly requested.'

Here the code that creates the lists during the deployment:

ArrayList arrFields = new ArrayList();
        arrFields.Add(fieldBody);
        arrFields.Add(fieldPublishStartDate);
        arrFields.Add(fieldPublishStopDate);
        arrFields.Add(fieldReadMore);

        //create the home page content lists

        List FeaturedStories = CreateList(ctx, currentWeb, "Featured Stories", ListTemplateType.PictureLibrary, arrFields, true);
        CreateList(ctx, currentWeb, "Team Spotlight", ListTemplateType.PictureLibrary, arrFields, true);
        List quickLinks = CreateList(ctx, currentWeb, "Quick Links", ListTemplateType.Links, null, true);
        List resourceLinks = CreateList(ctx, currentWeb, "Resource Links", ListTemplateType.Links, null, true);

        arrFields.Remove(fieldBody);
        List announcementsList = CreateList(ctx, currentWeb, "News and Announcements", ListTemplateType.Announcements, arrFields, true);
        List eventsCalendarList = CreateList(ctx, currentWeb, "Events Calendar", ListTemplateType.Events, arrFields, true);

        ctx.Load(currentWeb, w => w.Lists);
        ctx.ExecuteQuery();

Here is the code where I'm trying to add one of the lists to the home page:

var pageUrl = "/sites/practice3/SitePages/Home.aspx";
var xml = ctx.Web.Lists.GetByTitle("News and Announcements").SchemaXml;
var web = ctx.Web;
var lists = web.Lists;
ctx.Load(lists);
ctx.ExecuteQuery();
AddWebPart(ctx, pageUrl, xml);

This is the method AddWebPart, but I have not been able to test whether this will work either since, I cannot reach the code:

public static void AddWebPart(ClientContext context, string pageUrl, string xml)
    {
        var page = context.Web.GetFileByServerRelativeUrl(pageUrl);
        var webpartManager = page.GetLimitedWebPartManager(PersonalizationScope.Shared);
        context.Load(webpartManager);
        context.Load(webpartManager.WebParts);
        context.ExecuteQuery();
        WebPartDefinition webpartDef = webpartManager.ImportWebPart(xml);
        WebPartDefinition webpart = webpartManager.AddWebPart(webpartDef.WebPart, "mainContent", 0);
        context.Load(webpartDef);
        context.ExecuteQuery();
        //webpartManager.ImportWebPart(xml);
        //context.ExecuteQuery();
        Console.WriteLine("Hit any key to continue...");
        Console.ReadKey();
        Console.ReadKey();
    }

Thank you for any help!


Solution

  • You are loading all lists information (with basic fileds) thus having an NonInitializedProperty exception when trying to access XML data. If you would like to get your Schema, you need to load it explicitly.

    var pageUrl = "/sites/practice3/SitePages/Home.aspx";
    // mark object you would like to access
    var list = ctx.Web.Lists.GetByTitle("News and Announcements");
    // prepare load query with SchemaXml property
    ctx.Load(list, l=>l.SchemaXml);
    ctx.ExecuteQuery(); // request the data
    
    AddWebPart(ctx, pageUrl, list.SchemaXml);
    

    Hope that helps.