Sharepoint 2010 Web 部件通信 - 如何让消费者等待提供者

发布于 2024-11-17 01:07:24 字数 4520 浏览 3 评论 0原文

我需要在 SharePoint 2010 中实现一系列 Web 部件。数据提供程序 Web 部件使用 UpdatePanel 并异步进行 Web 服务调用,该调用可能会很慢。为了简单起见,我在页面(图表)上放置了一个消费者 Web 部件,它将使用消费者作为其数据提供者。

我的问题是我无法让消费者等待提供者 - 我收到各种错误,但基本上都回到“没有可用数据”。这可能是因为它是一个图表 Web 部件,但问题也适用于我将开发的其他自定义部件,因为它们将提取相同的数据。

问题是:当我的提供者准备就绪时,我如何将数据推送给我的消费者,或者以某种方式让他们等待我的提供者获得数据(通过轮询或其他方式)。

注意:这只是一个原型,我还没有添加错误处理等。

代码如下:

[ToolboxItem(true)]
public partial class ClarityProjectGeneral : System.Web.UI.WebControls.WebParts.WebPart , IWebPartTable
{

    public DataTable ProjectVitals = new DataTable(); For web part communication

    // bunch of properties

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

        // For web part communication
        // Initialize our datatable so the chart doesn't barf
        DataColumn col = new DataColumn();
        col.DataType = typeof(string);
        col.ColumnName = "Name";
        this.ProjectVitals.Columns.Add(col);

        col = new DataColumn();
        col.DataType = typeof(DateTime);
        col.ColumnName = "Start";
        this.ProjectVitals.Columns.Add(col);

        col = new DataColumn();
        col.DataType = typeof(DateTime);
        col.ColumnName = "End";
        this.ProjectVitals.Columns.Add(col);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        loading.Visible = true;
        content.Visible = false;            
    }

    public ClarityObjectClasses.Projects GetProject(string projectID)
    {
        Clarity.ClarityAbstractorProject ca = new Clarity.ClarityAbstractorProject(this.Username, this.Password);
        Dictionary<string, string> queryParams = new Dictionary<string, string>();
        queryParams.Add("projectID", projectID);
        // Class for making web service call
        ClarityObjectClasses.Projects response = new ClarityObjectClasses.Projects();
        response = ca.GetProject(queryParams);
        return response;
    }

    protected void Timer1_Tick(object sender, EventArgs e)
    {
        if (this.ProjectID == null || this.Username == null || this.Password == null)
        {
            lblConfigError.Visible = true;
            lblConfigError.Text = "One or more required configuration values are not set.  Please check the web part configuration.";
            panelProjectDetails.Visible = false;
        }
        else
        {
            loading.Visible = true;
            content.Visible = false;

            panelProjectDetails.Visible = true;
            ClarityObjectClasses.Projects projects = GetProject(this.ProjectID);
            //Assign a bunch of values

            // For web part communication
            LoadTable(projects.Project[0]);

            Timer1.Enabled = false;
            loading.Visible = false;
            content.Visible = true;
        }
    }


    /* Interface functions for Graph Chart communication */
    For web part communication
    protected void LoadTable(ClarityObjectClasses.Project project)
    {
        DataRow row = ProjectVitals.NewRow();
        row["Name"] = project.name;
        row["Start"] = project.start;
        row["End"] = project.finish;
        this.ProjectVitals.Rows.Add(row);
    }

    public PropertyDescriptorCollection Schema
    {
        get
        {
            return TypeDescriptor.GetProperties(ProjectVitals.DefaultView[0]);
        }
    }

    public void GetTableData(TableCallback callback)
    {
        callback(ProjectVitals.Rows);
    }

    public bool ConnectionPointEnabled
    {
        get
        {
            object o = ViewState["ConnectionPointEnabled"];
            return (o != null) ? (bool)o : true;
        }
        set
        {
            ViewState["ConnectionPointEnabled"] = value;
        }
    }

    [ConnectionProvider("Table", typeof(TableProviderConnectionPoint), AllowsMultipleConnections = true)]
    public IWebPartTable GetConnectionInterface()
    {
        return this;
    }

    public class TableProviderConnectionPoint : ProviderConnectionPoint
    {
        public TableProviderConnectionPoint(MethodInfo callbackMethod, Type interfaceType, Type controlType, string name, string id, bool allowsMultipleConnections)
            : base(callbackMethod, interfaceType, controlType, name, id, allowsMultipleConnections)
        {
        }

        public override bool GetEnabled(Control control)
        {
            return ((ClarityProjectGeneral)control).ConnectionPointEnabled;
        }

    }
}

I have a series of web parts I need to implement in SharePoint 2010. The data provider web part uses an UpdatePanel and asynchronously makes a web service call which can potentially be slow. To keep it simple, I've put a single consumer web part on the page (Chart) which will use the consumer as its data provider.

My problem is that I can't get the consumer to wait for the provider - I get a variety of errors but all basically come back to "There is no data available". This may be because it is a Chart web part but the question also applies to the other custom parts I will be developing as they will pull the same data.

The question is: how do I either push data to my consumers when my provider is ready or somehow let them wait for my provider to have data (via polling or whatever).

Note: this is just a prototype, I haven't added error handling, etc yet.

Code is below:

[ToolboxItem(true)]
public partial class ClarityProjectGeneral : System.Web.UI.WebControls.WebParts.WebPart , IWebPartTable
{

    public DataTable ProjectVitals = new DataTable(); For web part communication

    // bunch of properties

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

        // For web part communication
        // Initialize our datatable so the chart doesn't barf
        DataColumn col = new DataColumn();
        col.DataType = typeof(string);
        col.ColumnName = "Name";
        this.ProjectVitals.Columns.Add(col);

        col = new DataColumn();
        col.DataType = typeof(DateTime);
        col.ColumnName = "Start";
        this.ProjectVitals.Columns.Add(col);

        col = new DataColumn();
        col.DataType = typeof(DateTime);
        col.ColumnName = "End";
        this.ProjectVitals.Columns.Add(col);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        loading.Visible = true;
        content.Visible = false;            
    }

    public ClarityObjectClasses.Projects GetProject(string projectID)
    {
        Clarity.ClarityAbstractorProject ca = new Clarity.ClarityAbstractorProject(this.Username, this.Password);
        Dictionary<string, string> queryParams = new Dictionary<string, string>();
        queryParams.Add("projectID", projectID);
        // Class for making web service call
        ClarityObjectClasses.Projects response = new ClarityObjectClasses.Projects();
        response = ca.GetProject(queryParams);
        return response;
    }

    protected void Timer1_Tick(object sender, EventArgs e)
    {
        if (this.ProjectID == null || this.Username == null || this.Password == null)
        {
            lblConfigError.Visible = true;
            lblConfigError.Text = "One or more required configuration values are not set.  Please check the web part configuration.";
            panelProjectDetails.Visible = false;
        }
        else
        {
            loading.Visible = true;
            content.Visible = false;

            panelProjectDetails.Visible = true;
            ClarityObjectClasses.Projects projects = GetProject(this.ProjectID);
            //Assign a bunch of values

            // For web part communication
            LoadTable(projects.Project[0]);

            Timer1.Enabled = false;
            loading.Visible = false;
            content.Visible = true;
        }
    }


    /* Interface functions for Graph Chart communication */
    For web part communication
    protected void LoadTable(ClarityObjectClasses.Project project)
    {
        DataRow row = ProjectVitals.NewRow();
        row["Name"] = project.name;
        row["Start"] = project.start;
        row["End"] = project.finish;
        this.ProjectVitals.Rows.Add(row);
    }

    public PropertyDescriptorCollection Schema
    {
        get
        {
            return TypeDescriptor.GetProperties(ProjectVitals.DefaultView[0]);
        }
    }

    public void GetTableData(TableCallback callback)
    {
        callback(ProjectVitals.Rows);
    }

    public bool ConnectionPointEnabled
    {
        get
        {
            object o = ViewState["ConnectionPointEnabled"];
            return (o != null) ? (bool)o : true;
        }
        set
        {
            ViewState["ConnectionPointEnabled"] = value;
        }
    }

    [ConnectionProvider("Table", typeof(TableProviderConnectionPoint), AllowsMultipleConnections = true)]
    public IWebPartTable GetConnectionInterface()
    {
        return this;
    }

    public class TableProviderConnectionPoint : ProviderConnectionPoint
    {
        public TableProviderConnectionPoint(MethodInfo callbackMethod, Type interfaceType, Type controlType, string name, string id, bool allowsMultipleConnections)
            : base(callbackMethod, interfaceType, controlType, name, id, allowsMultipleConnections)
        {
        }

        public override bool GetEnabled(Control control)
        {
            return ((ClarityProjectGeneral)control).ConnectionPointEnabled;
        }

    }
}

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

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

发布评论

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

评论(2

一杯敬自由 2024-11-24 01:07:24

不太明白,但如果有帮助的话
您不得在 UpdatePanel 内使用“可连接”Web 部件,
因为缺乏相应的事件来绑定异步回调上的数据。

Do not quite understand, but if it helps
You may not use "connectable" web-parts inside UpdatePanel,
because of lack of corresponding events to bind data on asynchronous callback.

他是夢罘是命 2024-11-24 01:07:24

我刚刚偶然发现了这一点。我在尝试实现自定义 Web 部件时遇到了完全相同的问题,只是为了向自己证明。我将过滤器应用于我的 Web 部件和列表,然后让图表使用它们。我发现我的 Web 部件发送了错误的数据,但列表 Web 部件按预期工作。

所以我反映了 XsltListViewWebPart (或无论它的确切名称是什么),我发现有一个 IConnectionData 接口。这允许您指定依赖项并获得所需的正确延迟绑定。 GetRequiresData 表示在请求数据之前还需要消耗更多连接。

I just stumbled across this. I had exactly the same problem trying to implement a custom webpart just as a proof to myself. I applied filters to both my webpart and a list, and then let a chart consume them. What I found was that my webpart sent the wrong data, but the list webpart worked as expected.

So I reflected the XsltListViewWebPart (or whatever it's exact name is) and I discovered that there is an IConnectionData interface. This allows you to specify the dependencies and get the correct delay binding you need. GetRequiresData indicates that there are still more connections to be consumed before the data can be requested.

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