2011-11-02 29 views
0

我有一個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/

是否有解決此問題的方法?

回答

3

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)); 
      } 
     } 
    } 
相關問題