Web Part connections in site definitions

1.9k Views Asked by At

I have requirement of specifying web part connections in onet.xml. So when site is created using this site definition the said web parts are already connected and ready to use. Which properties I need to specify for that particular web parts in onet.xml.

3

There are 3 best solutions below

0
On BEST ANSWER

I have also hit the wall on this one sometime last year! It looks like connections can no longer be specified on Web Parts in the new .webpart format as they could in the old .dwp format. I ended up including a custom feature in the site definition like kpinhack also suggests. My code for connecting the Web Parts is listed below. The method is just designed for connecting two Web Parts of different types - it does not support multiple Web Parts of the same type on the same page. But I am sure you'll catch the general idea.

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
On

I would configure the WebParts in the SiteProvisioning-Feature, by implementing the 'OnActivated'-Eventhandler. That way the code will run when the website is created, and you can handle errors the way you like it (i.e. if the WebParts are not available when the website is created - for whatever reason)

I hope this helps!

0
On

you would need to use the < AllUsersWebPart > tag to declare your web parts, and then declare your connections within the enclosed < WebPart > elements.

example