可以在 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/