Expression Evaluation Time: ms / Round trip: ms
How to Implement a Consumer / Producer WebPart
20. Mar
2012
by Dario Category:
Technical
3

In MatchPoint we introduced a more sophisticated mechanism to consume data in one Web Part from another. Compared to the OOB SharePoint solution, with MatchPoint the data can be shared without a post back of the page, and individual controls and Web Parts can be refreshed using an Ajax callback.

In order to benefit from the Connection Framework you can extend the MatchPoint BaseWebPart class. This example shows a Web Part with a text field input that provides its input to other MatchPoint WebParts such as a Composite Web Part.

Addtionally to show how data can be consumed from other controls, the given sample has a configurable label (PatternString) which can contain references to "ConnectionData". The label (or the control) should be updated whenever the referenced data is changed.

[Serializable]
public class ConnectableWebPartConfiguration: IWebPartConfiguration
{
    // the name of under which this WebPart instance provides the connection data
    public string Name;
    // a configurable label text that can contain placeholder pointing to other 
    // connection data such as {ConnectionData.OtherGrid.SelectedRow.Title}
    public PatternString Label;
}

public class ConnectableWebPart : BaseWebPart<ConnectableWebPartConfiguration>
{
    ...
}

As we have some JS code we have to register it. MatchPoint then will include the JS and auotmatically provide a proxy for Ajax callbacks.

If you want to provide some data to other controls and Web Parts you have to register the control as a producer (data source). This is done using the ConnectionManager.RegisterDataSource method.

If you want to consume data you have to register the control as a consumer. This is done calling ConnectionManager.RegisterConsumer. As you don't want to react on all ConnectionData changes but only on the one that affect your control, you additionally pass in a DepedencyCollection. The DepedencyCollection itself provides methods to analyze PatternStrings. If "ConnectionData.xxx" references are found in the placeholders the fields are added to the collection of dependencies.

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);         

    // register the corresponding JS file 
    // (has the same name as the class and is located)
    JavaScriptBehaviorManager.RegisterBehavior(this, typeof(ConnectableWebPart));

    // register as data source
    ConnectionManager.RegisterDataSource(this, Configuration.Name, typeof(string));

    // extract all ConnectionData field access from the pattern and register
    // as consumer as well
    DependencyCollection dependencies = new DependencyCollection();
    dependencies.AddFromPattern(Configuration.Label);

    ConnectionManager.RegisterConsumer(this, dependencies);
}

On the client side we have to send a notification to the consumer controls if the text input changes.

The MP.ConnectionManager.NotifyConsumers method calls the Refresh method on all controls that are registered as consumer.

We self implement the Refresh method as well and render the control including the label again doing an Ajax callback. As we registered the Web Part as a consumer for all ConnectionData references in the Label string the framework automatically calls the Refresh method on changes that affect the Web Part.

MP.Samples.ConnectableWebPart = function()
{
    this.Setup = function()
    {
        this.BindEventHandlers();
    };

    this.TextBox_Change = function()
    {
        MP.ConnectionManager.SetData(this.ConnectionName, this.$textBox.val());
        MP.ConnectionManager.NotifyConsumers(this);
    };

    this.BindEventHandlers = function()
    {
        this.$textBox = $(this.Control).find("input");
        this.$textBox.blur($$.Delegate.Create(this, this.TextBox_Change));
    };

    this.Refresh = function()
    {
        $(this.Control).html(this.Callback.GetHtml());
        this.BindEventHandlers();
    };
}

In order that MatchPoint includes the JS automatically and knows where to look it up you have to register your assembly as MatchPoint extension in the MatchPoint Instance Configuration and define the script location using the JavaScriptBaseAttribute in the AssemblyInfo.cs:

[assembly: JavaScriptBase("MP.Samples", "/_layouts/Colygon.MatchPoint.Samples/scripts")]

ConnectableWebPart.js

ConnectableWebPart.cs

Comments
mikko koskinen
22.03.2012 07:49
Could you give more information on even send an full example solution so that I could test this. The thing that isn't clear to me is do I have to call the js file inside my for example master page if I wan't to use this web part?
Dario
23.03.2012 03:24
Thanks for your feedback. Indeed I was too unspecific regarding the JS registration. I updated the post. Hope this helps.
leomarquezd
19.10.2012 12:35
Hello,

I tried to implement this example and I'm not sure what the result should be.
I registered my assembly as external in MatchPoint configuration just putting the url of my .dll in the ExternalAssemblies field. Here is the resulting xml:

<ExternalAssemblies>
<Assembly>/bin/ActivityCriticalAreas.dll</Assembly>
</ExternalAssemblies>

Is it well configured? How can I check if MatchPoint is loading my assembly?

After this, I expected to see the new webpart under the MatchPoint webpart category, but I just see the out of the box ones.

I used a VS SP Visual Web Part Project, should I use another type of project?

Thank you and best regards,

Leo
ABOUT

This blog is about technical and non-technical aspects of the product MatchPoint and other SharePoint topics.

If you would like to post an article or if you have an idea for a post, please contact us.

ARCHIVE
COMMENTS
Matthias Weibel
09.04.2018 01:12
Link is updated and works now. | Goto Post
Dhanabalan
09.04.2018 12:21
Link doesn't work. Could anyone explain what does... | Goto Post
matthiaszbrun
14.03.2018 02:05
Hi Markus
We I use the config for SiteCollectionSe... | Goto Post
Reto Jeger
04.10.2017 09:15
Hello Reiner,
Thanks for pointing out the missing ... | Goto Post
rganser
29.09.2017 09:56
Hi, I downloaded the ZIP-file for MatchPoint Versi... | Goto Post