0

我在 SharePoint Designer 中创建了一个简单的自定义页面布局,添加了几个 Web 部件区域,并在每个区域上添加了一个自定义 Web 部件。

这些 Web 部件具有一组自定义的提供者-消费者界面。我在 SharePoint Designer 中配置了 Web 部件之间的连接(使用添加连接向导),并保存了页面布局。

之后,我检查了布局并在 SP 的 Web 界面中批准了它。

最后,我根据我的布局创建了一个发布页面,但未设置 Web 部件之间的连接。我可以手动设置连接,它工作正常,但我希望根据布局在我创建的每个页面上自动设置连接。

这是预期的行为吗?如果是这样,有没有办法以编程方式或声明方式自动配置连接?

有没有我错过的步骤?

4

1 回答 1

0

可以在 SharePoint 中设置连接,但不是那么容易。以下是我项目中的代码。首先,我们需要一些方法来为消费者字段和提供者字段创建转换器:

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

现在创建连接:

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 是特定视图页面的 SPLimitedWebPartManager 实例。

您可以在此处找到更多示例:

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/

于 2011-05-10T12:49:29.860 回答