我需要在onet.xml中指定Web部件连接。因此,当使用此站点定义创建站点时,所述Web部件已经连接并且可以使用。我需要在onet.xml中为特定Web部件指定哪些属性。
答案 0 :(得分:3)
private void ConnectWebParts(SPWeb web, string pageName, Type providerType, Type consumerType)
{
SPFile file = web.GetFile(pageName);
SPList list = null;
if (file.InDocumentLibrary)
{
list = file.Item.ParentList;
if (list.ForceCheckout) file.CheckOut();
}
SPLimitedWebPartManager webPartManager =
web.GetLimitedWebPartManager(
pageName,
System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
WebPart provider = null;
foreach (WebPart wp in webPartManager.WebParts)
{
if (wp.GetType() == providerType)
{
provider = wp;
break;
}
}
foreach (WebPart consumer in webPartManager.WebParts)
{
if (consumer.GetType() != consumerType) continue;
ProviderConnectionPointCollection providerConnections = webPartManager.GetProviderConnectionPoints(provider);
ProviderConnectionPoint providerConnection = providerConnections[0];
ConsumerConnectionPointCollection consumerConnections = webPartManager.GetConsumerConnectionPoints(consumer);
ConsumerConnectionPoint consumerConnection = consumerConnections[0];
SPWebPartConnection con = webPartManager.SPConnectWebParts(provider, providerConnection, consumer, consumerConnection);
webPartManager.SPWebPartConnections.Add(con);
}
if (list != null)
{
if (list.ForceCheckout)
{
file.CheckIn("Added Web Part Connections");
}
if (list.EnableVersioning && list.EnableMinorVersions)
{
file.Publish("Added Web Part Connections");
}
}
}
答案 1 :(得分:0)
我将通过实现'OnActivated'-Eventhandler来配置SiteProvisioning-Feature中的WebParts。这样代码将在创建网站时运行,并且您可以按照自己喜欢的方式处理错误(即,如果在创建网站时WebParts不可用 - 无论出于何种原因)
我希望这有帮助!
答案 2 :(得分:0)
你需要使用< AllUsersWebPart>标记以声明您的Web部件,然后在随附的<中声明您的连接。 WebPart>元件。