Today i was trying create to custom webpart preview page as test canvas for site admins. For this i wanted to add webpart on fly. We need few properties like web part type name and web part assembly name for this.
string webPartTitle = "
Content Editor
Web Part";
string webPartTypeName = string.Empty;
string webPartAssemblyName = string.Empty;
SPList wpGallery = SPContext.Current.Site.GetCatalog(SPListTemplateType.WebPartCatalog);
SPQuery wpQuery = new SPQuery();
wpQuery.Query
= @"<Where><Eq><FieldRef
Name='Title' /><Value Type='Text'>" +
webPartTitle + "</Value></Eq></Where>";
SPListItemCollection items = wpGallery.GetItems(wpQuery);
if (items != null && items.Count > 0)
{
webPartTypeName = items[0]["WebPartTypeName"].ToString();
webPartAssemblyName = items[0]["WebPartAssembly"].ToString();
}Some other useful properties are below
- WebPartDescription
- WebPartPartImageLarge
- LinkWebPart
- WebPartIcon
- Group
- QuickAddGroups