SharePoint / WSS3.0:在运行时创建静态webpart连接

时间:2010-05-24 11:51:14

标签: c# asp.net sharepoint wss wss-3.0

我创建了几个需要连接的主/详细webpart。我们要求webparts自我发现并连接到页面上的其他可连接的webpart。我在标准的ASP.NET页面中使用以下代码实现了这一点:

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版本会导致一般的共享点错误:

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 个答案:

答案 0 :(得分: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;
}