0
votes

I have created a simple custom page layout in SharePoint Designer, added a couple of web part zones, and a custom web part on each zone.

These web parts have a custom provider-consumer set of interfaces. I configured the connection between the web parts in SharePoint Designer (using Add Connection wizard), and saved the page layout.

After that, I checked in the layout and approved it in SP's web interface.

Finally, I created a Publishing Page based on my layout, but the connection between the web parts was not set. I can set the connection manually and it works fine, but I would like the connection to be automatically set on each page I create based on the layout.

Is this the expected behavior? If so, is there a way to configure the connection automatically either programmatically or declaratively?

Is there a step I'm missing?

1

1 Answers

0
votes

It is possible to set connections in SharePoint, but not so easy. Below is the code from my project. At first we need some method to create transformer for consumers field and provider field:

private TransformableFilterValuesToParametersTransformer CreateTransformer(String consumerFieldName, String providerFieldName)
        {
            TransformableFilterValuesToParametersTransformer filterToParameterTransformer =
                new TransformableFilterValuesToParametersTransformer();
            // The private fields '_consumerFieldNames' and '_providerFieldNames' must be set for the web part connection to work.
            // Having reflected the TransformableFilterValuesToParametersTransformer it seems that the only place this is set is within a
            // nested wizard class (this seems to be the wizard that runs when the connection is created via the SharePoint UI) so although
            // this is not pretty it looks to be the only way...

            try
            {
                string[] _consumerFieldNames = new string[1];
                _consumerFieldNames[0] = consumerFieldName;
                FieldInfo conFld = typeof(TransformableFilterValuesToParametersTransformer).GetField("_consumerFieldNames", BindingFlags.Instance | BindingFlags.NonPublic);
                conFld.SetValue(filterToParameterTransformer, _consumerFieldNames);
                string[] _providerFieldNames = new string[1];
                _providerFieldNames[0] = providerFieldName;
                FieldInfo prvFld = typeof(TransformableFilterValuesToParametersTransformer).GetField("_providerFieldNames", BindingFlags.Instance | BindingFlags.NonPublic);
                prvFld.SetValue(filterToParameterTransformer, _providerFieldNames);
            }
            catch
            {
                throw new Exception("Unexpected internal definition for 'TransformableFilterValuesToParametersTransformer'. Please amend private member access code.");
            }
            return filterToParameterTransformer;
        }

Now create connection:

TransformableFilterValuesToParametersTransformer transformer = CreateTransformer("ConsumerFieldInternalName", "ProviderFieldInternalName");

            ProviderConnectionPointCollection pcpc = mgr.GetProviderConnectionPoints(providerWebPart);
            ConsumerConnectionPointCollection ccpc = mgr.GetConsumerConnectionPoints(consumerWebPart);
            mgr.SPConnectWebParts(
               providerWebPart, pcpc[0],
                consumerWebPart,
                ccpc[0],
                transformer
                );

mgr is SPLimitedWebPartManager instance of particular view page.

You can find more examples here:

http://social.msdn.microsoft.com/Forums/en/sharepointdevelopment/thread/3f960d66-af45-42ca-82d7-114de773213f

http://jsiegmund.wordpress.com/2010/05/28/sp2010-programmatically-creating-a-web-part-page-with-connected-webparts/