Logo 
Search:

MOSS Forum

Ask Question   UnAnswered
Home » Forum » MOSS       RSS Feeds

Copy a webpart programmatically

  Asked By: Raven    Date: Feb 16    Category: MOSS    Views: 1934

I have a client that wants to copy an existing webpart from an
existing MOSS 07 page and add that webpart to the "MySite" page.

I know I can use something like this to add a webpart to a page as
long as I have access to the *.webpart or *.dwp file:
public void AddWebPart2()
{
string err = string.Empty;
string chromeType = "Default";
string zoneIndex = "Left";
string Error = string.Empty;

using (SPSite site = new SPSite("http://ford/test"))
{
using (SPWeb web = site.OpenWeb())
{
try
{
SPFile file = web.GetFile("default.aspx");

SPLimitedWebPartManager manager1 =
file.GetLimitedWebPartManager(PersonalizationScope.Shared);
XmlReader reader1 = GetWebPartDef
(@"D:\SampleWebParts\ThisWeekInPictures.dwp");
System.Web.UI.WebControls.WebParts.WebPart
part1 = manager1.ImportWebPart(reader1, out err);
part1.ChromeType = (PartChromeType)Enum.Parse
(typeof(PartChromeType), chromeType);
manager1.AddWebPart(part1, zoneIndex, 1);
}
catch (Exception ex)
{
Error = "AddWebPart2 Error: " + ex.Message;
throw new Exception(Error);
}

} // End of using web

} // End of using site
}

So my question is ... how can I get the *.webpart or *.dwp file from
the webpart gallery? Or do I get it from the webpart gallery? If
not where can I gain access to these/this file from?

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Tyrell Nash     Answered On: Feb 16

Can you go to the page  where the webpart  is used and then export that
webpart via "edit page" ->"edit shared  webpart"?

 
Didn't find what you were looking for? Find more on Copy a webpart programmatically Or get search suggestion and latest updates.




Tagged: