Showing posts with label SharePoint Object Model. Show all posts
Showing posts with label SharePoint Object Model. Show all posts

Wednesday, November 5, 2014

Get web part assembly name and type name from SharePoint web part gallery


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

Tuesday, January 10, 2012

Find all ContentByQueryWebParts from the page programatically

Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager manager = web.GetLimitedWebPartManager("Pages/default.aspx", PersonalizationScope.Shared);

foreach (Microsoft.SharePoint.WebPartPages.WebPart webPart in manager.WebParts)
{
 string type name="Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart, Microsoft.SharePoint.Publishing, Version=14.0.0.0,   Culture=neutral,    PublicKeyToken=71e9bce111e9429c";

 Type type = webPart.GetType();
 if (type.FullName.Contains("ContentByQueryWebPart")
 {
  //then it is ContentByQueryWebPart
 }
}