Programmatically add web parts to the SharePoint Web Part Gallery

It’s a simple matter of uploading the files to the Web Part Gallery list, as shown here:

http://www.customware.net/repository/display/SharePoint/SharePoint+-+Pro…

string url_Site = sSite;

using (Microsoft.SharePoint.SPSite site = new Microsoft.SharePoint.SPSite(url_Site))
{
Microsoft.SharePoint.SPList ctg_WebParts = site.GetCatalog(Microsoft.SharePoint.SPListTemplateType.WebPartCatalog);

FileInfo file = new FileInfo(@”c:\webpartfile.webpart”);

FileStream stream = file.Open(FileMode.Open, FileAccess.Read);

SPFolder root = ctg_WebParts.RootFolder;

SPFile dwpFile = root.Files.Add(“webpartfile.webpart”, stream);

dwpFile.Update();

stream.Close();
stream.Dispose();
}

Advertisements

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