我有一个网页部件,我想添加一个自定义图标。我在Web部件架构中使用CatalogIconImageUrl属性(假设值为_catalogs / masterpage / MyFolder / MyWebPartResources / MyWebPart_16x16.png)。
如果我将Web部件部署到我的开发站点,它会正确找到路径。
但是,如果我将Web部件部署到多层站点,则它只是找不到URL。例如,如果我将Web部件部署到http://mysitecollection/SitePages/等网站集或个人SP网站,例如http://mysitecollection/my/personal/UserName/
有解决方法吗?
答案 0 :(得分:3)
chris_walker向我提供了这个帮助。
我已在该功能中添加了一个事件接收器。之后,我在Feature Activated方法中检索了Web应用程序根目录。最后,我将webpart架构打开为txt文件,并用真实网站集URL替换了一个关键字(~sitecollection)。
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));
}
}
}