2010-05-24 6 views
1

J'ai créé plusieurs parties Web maître/détail qui doivent être connectées. Nous avons une exigence que les webparts découvrent et connectent à d'autres webparts connectables sur la page. J'ai acheived cela dans une page ASP.NET standard avec le code suivant:SharePoint/WSS3.0: Création de connexions WebPart statiques au moment de l'exécution

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" 
    }); 
} 

Cette approche, cependant, ne fonctionne pas dans SharePoint. En utilisant la version SharePoint de ces objets provoque une erreur sharepoint générique:

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" 
    }); 
} 

L'approche suivante fonctionne, mais crée la connexion dans le cadre de la personnalisation de l'utilisateur:

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

Pouvez-vous vérifier les fichiers journaux de SharePoint ou l'Observateur d'événements pour voir s'il y a des informations d'erreur plus détaillées qui peuvent aider à la résolution du problème? –

+0

Merci pour la suggestion de log. Je suis un peu nouveau dans le développement de SharePoint et je m'habitue toujours au modèle de développement. J'avais très honnêtement oublié d'activer la journalisation détaillée. – etc

Répondre

0

Caché dans les journaux était une erreur indiquant que la propriété StaticConnections ne peut pas être utilisée dans les environnements SharePoint/WSS. Au lieu de cela, la propriété SPWebPartConnections doit être utilisée. De plus, les connexions doivent être ajoutées avant l'événement de chargement (par exemple OnInit).

Code de travail:

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; 
} 
Questions connexes