2010-05-24 52 views
1

我已經創建了多個需要連接的主/細節Web部件。我們要求Web部件自己發現並連接到頁面上的其他可連接Web部件。我在一個標準的ASP.NET頁面下面的代碼來達到的這樣:SharePoint/WSS3.0:在運行時創建靜態Web部件連接

protected override void OnLoad(EventArgs e) 
{  
    WebPartManager manager = WebPartManager.GetCurrentWebPartManager(Page); 
    manager.StaticConnections.Add(new WebPartConnection() 
    { 
     ID = string.Format("WebPartConnection{0}{1}", this.ID, provider.ID), 
     ConsumerID = this.ID, 
     ConsumerConnectionPointID = "WebPartConnectableConsumer", 
     ProviderID = provider.ID, 
     ProviderConnectionPointID = "WebPartConnectableProvider" 
    }); 
} 

這種方法,然而,這並不在SharePoint工作。在一個通用的SharePoint錯誤使用這些對象結果的SharePoint版本:

protected override void OnLoad(EventArgs e) 
{  
    SPWebPartManager spManager = SPWebPartManager.GetCurrentWebPartManager(Page) as SPWebPartManager; 
    spManager.StaticConnections.Add(new WebPartConnection() 
    { 
     ID = string.Format("WebPartConnection{0}{1}", this.ID, provider.ID), 
     ConsumerID = this.ID, 
     ConsumerConnectionPointID = "WebPartConnectableConsumer", 
     ProviderID = provider.ID, 
     ProviderConnectionPointID = "WebPartConnectableProvider" 
    }); 
} 

以下方法工作,但創建連接,用戶的個性化的一部分:

protected override void OnLoad(EventArgs e) 
{ 
    SPWebPartConnection connection = (from SPWebPartConnection c in spManager.SPWebPartConnections where c != null && c.Consumer == this && c.ConsumerConnectionPointID == "WebPartConnectableConsumer" && c.Provider == provider select c).FirstOrDefault(); 
    if (connection == null) 
    { 
     try 
     { 
      ProviderConnectionPointCollection providerCollections = spManager.GetProviderConnectionPoints(provider); 
      ConsumerConnectionPointCollection consumerConnections = spManager.GetConsumerConnectionPoints(this); 
      connection = spManager.SPConnectWebParts(provider, providerCollections["WebPartConnectableProvider"], this, consumerConnections["WebPartConnectableConsumer"]); 
     } 
     catch { } 
    } 
} 
+1

,您可以檢查的SharePoint日誌文件或事件查看器來查看是否有更詳細的錯誤信息,可以在故障排除問題幫助嗎? – 2010-05-24 18:59:07

+0

感謝您的日誌建議。我對SharePoint開發稍微陌生,但仍然習慣於開發模式。我真的忘了打開詳細的日誌記錄。 – etc 2010-05-25 10:58:26

回答

0

在日誌中隱藏的是一個錯誤,指出StaticConnections屬性無法在SharePoint/WSS環境中使用。相反,必須使用SPWebPartConnections屬性。而且,必須在加載事件之前添加連接(例如,OnInit)。

工作代碼:

protected override void OnInit(EventArgs e) 
{ 
    base.OnInit(e); 
    SetUpProviderConnection(); 
} 

private bool SetUpProviderConnection() 
{ 
    bool connectionCreated = false; 

    WebPartManager manager = WebPartManager.GetCurrentWebPartManager(Page); 
    foreach (System.Web.UI.WebControls.WebParts.WebPart webPart in manager.WebParts) 
    { 
     BaseWebPart provider = webPart as BaseWebPart; 
     if (provider != null && (provider != this)) 
     { 
      if (manager is Microsoft.SharePoint.WebPartPages.SPWebPartManager) 
      { 
       SPWebPartManager spManager = SPWebPartManager.GetCurrentWebPartManager(Page) as SPWebPartManager; 

       spManager.SPWebPartConnections.Add(new SPWebPartConnection() 
       { 
        ID = string.Format("WebPartConnection{0}{1}", this.ID, provider.ID), 
        ConsumerID = this.ID, 
        ConsumerConnectionPointID = "WebPartConnectableConsumer", 
        ProviderID = provider.ID, 
        ProviderConnectionPointID = "WebPartConnectableProvider" 
       }); 
      } 
      else 
      { 
       manager.StaticConnections.Add(new WebPartConnection() 
       { 
        ID = string.Format("WebPartConnection{0}{1}", this.ID, provider.ID), 
        ConsumerID = this.ID, 
        ConsumerConnectionPointID = "WebPartConnectableConsumer", 
        ProviderID = provider.ID, 
        ProviderConnectionPointID = "WebPartConnectableProvider" 
       }); 
      } 
      connectionCreated = true; 
     } 
    } 
    return connectionCreated; 
}