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

发布于 2024-09-02 12:06:33 字数 2065 浏览 3 评论 0原文

我创建了几个需要连接的主/详细 Web 部件。我们需要 Web 部件自我发现并连接到页面上的其他可连接的 Web 部件。我已使用以下代码在标准 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 { }
    }
}

I've created several master / detail webparts that need to be connected. We have a requirement the the webparts self discover and connect to other connectable webparts on the page. I've acheived this in a standard ASP.NET page with the following code:

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

This approach, however, does not work in SharePoint. Using the SharePoint version of these objects results in a generic sharepoint error:

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

The following approach works, but creates the connection as part of the user's personalization:

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 { }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老娘不死你永远是小三 2024-09-09 12:06:33

日志中隐藏着一条错误,指出无法在 SharePoint/WSS 环境中使用 StaticConnections 属性。相反,必须使用 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;
}

Hidden in the logs was an error stating that the StaticConnections property cannot be used in SharePoint/WSS environments. Instead, the SPWebPartConnections property must be used. Moreover, connections must be added prior to the load event (eg. OnInit).

Working code:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文