Web Part connections in site definitions
Asked Answered
D

3

6

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.

Declarant answered 26/6, 2009 at 11:58 Comment(0)
H
3

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");
    }
  }
}
Hardwood answered 26/6, 2009 at 20:46 Comment(0)
B
0

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!

Bagnio answered 26/6, 2009 at 12:33 Comment(1)
Is this the only way to provide web part connection in site definition? Can't i just specify webpart connection properties in onet.xml. I think in sharepoint 2003 two web part proeprties were there "connectionid" and "connections".Can't I use the same? if yes how?Declarant
N
0

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

example

Naominaor answered 26/6, 2009 at 21:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.