Saturday, October 20, 2007

No Web parts Found.. while adding the webparts

Problem Definition:

After creating a '.stp' file out of a site, and adding this .stp template to a farm using stsadm, when you try to add webparts to the new site collection that has been created with the custom site template, you will get the error ''No Web parts Found.."



Fix:

  1. You can manually copy the webparts from the tempalte site webpart gallery and paste into the destination webpart gallery. But web parts are not grouped in sections(Content Rollup, dashboard, mislaneous, all webparts etc)
  2. You can also go to the new site webpart gallery and populate the webparts manually.
    (Site Actions -> Site Settings -> Galleries -> Webpart Gallery -> New -> select the webpart you desire -> populate)
  3. Run the following code using a console application if you wish to add the webparts only once. If you are running like this, then uncomment the line 'SPSite ProvisionedSite = new SPSite(provisionedSiteUrl);' and comment the line 'SPSite ProvisionedSite = SPContext.Current.Site;'

    If you wish to have the webparts copied automatically, each time you create a site, then go for a feature and embed this code in the 'Feature_Acticated' method of the feature.
//code for copying webpart files from the template site to provisioned site
public static void CopyWebParts(string templateSiteUrl, string provisionedSiteUrl)
{
// Get the context of the template site
SPSite templateSite = new SPSite(templateSiteUrl);
// Access the Template site Webparts folder under _catalogs
SPFileCollection templateWebParts = templateSite.RootWeb.Folders["_catalogs"].SubFolders["wp"].Files;
// Below line is commented as this code is meant for automatic execution.Un comment this line, if you want to run this code as a console application.
//SPSite ProvisionedSite = new SPSite(provisionedSiteUrl);
// Load the properties of the existing file
// Get the current site context
SPSite ProvisionedSite = SPContext.Current.Site;
// Access the Provisioned site Webparts folder under _catalogs
SPFileCollection ProvisionedWebParts = ProvisionedSite.RootWeb.Folders["_catalogs"].SubFolders["wp"].Files;
foreach (SPFile templateWebPart in templateWebParts)
{
// Load the properties of the existing file
byte[] binaryData = templateWebPart.OpenBinary();
// Copy the file
SPFile copiedFile = ProvisionedWebParts.Add(templateWebPart.Name, binaryData, true);
copiedFile.Update();
// Update the properties of the copied file

copiedFile.Item["Group"] = templateWebPart.Item["Group"];
copiedFile.Item["Quick Add Groups"] = templateWebPart.Item["Quick Add Groups"];
copiedFile.Item.Update();
copiedFile.Update();
}
}
 
Note:Always activate features through the browser.
Do not activate this feature through command prompt stsadm.exe.
Coz, stsadm doesn’t know the current context of the site.

No comments:

Post a Comment