Loading data into a Sharepoint page from a webpart file

/*

using System;

using System.Xml;

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebPartPages;

*/

 

            try

            {

 

                using (SPSite site = new SPSite(“http://mysite:123456/“))

                {

                    using (SPWeb web = site.OpenWeb(“MySubWeb”))

                    {

                        SPFile targetPage = web.GetFile(“default.aspx”);

 

                        SPLimitedWebPartManager webpartManager = targetPage.GetLimitedWebPartManager(

                            System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

 

                        string errMsg;

 

                        // This is the file that can be exported from the webpart

                        XmlTextReader reader = new XmlTextReader(“summary.webpart”);

                        System.Web.UI.WebControls.WebParts.WebPart webPart = webpartManager.ImportWebPart(reader, out errMsg);

 

                        webpartManager.AddWebPart(webPart, “TopWebPartZone”, 1);

                        targetPage.Update();

 

                    }

                }

 

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex);

            }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s