CatalogIconImageUrl - 自定义图标无法与个人SP网站正常工作
问题描述:
我有一个Web部件,我想添加一个自定义图标。我在Web部件模式中使用了CatalogIconImageUrl属性(假设值为_catalogs/masterpage/MyFolder/MyWebPartResources/MyWebPart_16x16.png)。CatalogIconImageUrl - 自定义图标无法与个人SP网站正常工作
如果我将Web部件部署到我的开发站点,它会正确找到路径。
但是,如果我将Web部件部署到多层站点,它只是找不到URL。例如,如果我将Web部件部署到网站集,如http://mysitecollection/SitePages/或个人SP网站,例如http://mysitecollection/my/personal/UserName/
是否有解决此问题的方法?
答
chris_walker为我提供了这个帮助。
我已将事件接收器添加到该功能。之后,我已经在Feature Activated方法中检索了Web应用程序根目录。最后,我将webpart模式作为txt文件打开,并将一个关键字(〜sitecollection)替换为真实的网站集URL。
public class WebPartEventReceiver : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties) {
SPSite site = properties.Feature.Parent as SPSite;
if (site != null) {
SPList webPartsGallery = site.GetCatalog(SPListTemplateType.WebPartCatalog);
SPListItemCollection allWebParts = webPartsGallery.Items;
SPListItem webPart = (from SPListItem wp in allWebParts where wp.File.Name == "your.webpart" select wp).SingleOrDefault();
if (webPart != null) {
string siteCollectionUrl = site.ServerRelativeUrl;
if (!siteCollectionUrl.EndsWith("/")) { siteCollectionUrl += "/"; }
string fileContents = Encoding.UTF8.GetString(webPart.File.OpenBinary());
fileContents = fileContents.Replace("~sitecollection/", siteCollectionUrl);
webPart.File.SaveBinary(Encoding.UTF8.GetBytes(fileContents));
}
}
}