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:…

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);




Leave a Reply

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

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

Facebook photo

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

Connecting to %s