Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 25064

Webpart page content not being overwritten CSOM SharePoint 2013

$
0
0

I am using the code below to create a WebPart page. The content is an exported WebPart page that does not have any content (ie webparts) added to the page. This creates the page fine the first time but if I were to run the code multiple times the page is recreated (time stamp changes), but the content seems to just be appended, the same web part is being added to the page. Example being first run page created, web part added, second run page is created(time stamp change), web part from first run still shows up and a second web part is added to the page.

Is there something that I am missing while creating the file? The overwrite flag is set to true so I would assume that this would overwrite the file completely and the content of the page would no longer exist.

ClientContext clientContext = new ClientContext(siteUrl);
        Web site = clientContext.Web;
        List list = site.Lists.GetByTitle("New Document Library");

        clientContext.Load(list);
        clientContext.ExecuteQuery();


        // create page
        var objFileInfo = new FileCreationInformation();
        objFileInfo.Url = "MyPage.aspx";
        objFileInfo.Overwrite = true;
        objFileInfo.Content = System.Text.Encoding.ASCII.GetBytes(content);
        Microsoft.SharePoint.Client.File file = list.RootFolder.Files.Add(objFileInfo);
        clientContext.Load(file);
        clientContext.ExecuteQuery();

        Microsoft.SharePoint.Client.File page = clientContext.Web.GetFileByServerRelativeUrl("/New Document Library/MyPage.aspx");
        clientContext.Load(page);
        clientContext.ExecuteQuery();

        Microsoft.SharePoint.Client.File webPartXMl = clientContext.Web.GetFileByServerRelativeUrl("/_catalogs/wp/CustomWebPart.webpart");
        clientContext.Load(webPartXMl);
        clientContext.ExecuteQuery();

        LimitedWebPartManager limitedWebPartManager = page.GetLimitedWebPartManager(PersonalizationScope.Shared);

        var webpart = webPartXMl.OpenBinaryStream();
        clientContext.ExecuteQuery();

        string wp;
        using (StreamReader xml = new StreamReader(webpart.Value))
        {
            wp = xml.ReadToEnd();
        }

        WebPartDefinition webPartDef = limitedWebPartManager.ImportWebPart(wp);

        limitedWebPartManager.AddWebPart(webPartDef.WebPart, "FullPage", 0);
        clientContext.ExecuteQuery();


Viewing all articles
Browse latest Browse all 25064

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>