Connect webparts programmatically using SPConnectWebParts

If you wish to connect two webpart where you have webpart consumer and webpart provider object then you can use following idea to implement functionality.

        /// <summary>
        /// It connect web parts each others, if any, using consumer and provider objects
        /// siteWebParts object have all information like consumer,provider connection and mapping.
        /// You can pass these details in your own way to this function.
        /// </summary>
 private void ConnectWebPartOnPage(SPWeb oWeb, List<SiteWebPart> siteWebParts)
        {
            List<SiteWebPart> siteFilterWebParts = siteWebParts.FindAll(e => e.Settings.MappingId != null);
            SPFile file = null;
            SPLimitedWebPartManager manager = null;
            file = oWeb.GetFile(strPageUrl);

            try
            {
                if (file.CheckOutStatus == SPFile.SPCheckOutStatus.None)
                {
                    file.CheckOut();
                    manager = file.GetLimitedWebPartManager(PersonalizationScope.Shared);

                    foreach (SiteWebPart siteWebPart in siteFilterWebParts)
                    {
                        string strConsumerWebPartName = siteWebPart.Settings.ConsumerWebPart;
                        string strProviderWebPartName = siteWebPart.Settings.ProviderWebPart;

                        System.Web.UI.WebControls.WebParts.WebPart webPartConsumer = null;
                        System.Web.UI.WebControls.WebParts.WebPart webPartProvider = null;

                        var mgConsumerWebPart =
                          manager.WebParts.Cast<System.Web.UI.WebControls.WebParts.WebPart>().Where(e => e.Title ==
                                                                                                          strConsumerWebPartName);
                        var mgProviderWebPart =
                          manager.WebParts.Cast<System.Web.UI.WebControls.WebParts.WebPart>().Where(e => e.Title ==
                                                                                                           strProviderWebPartName);

                        if (mgConsumerWebPart.Count() > 0)
                            webPartConsumer = mgConsumerWebPart.ElementAt(0);
                        if (mgProviderWebPart.Count() > 0)
                            webPartProvider = mgProviderWebPart.ElementAt(0);

                        ConsumerConnectionPoint consumerConnection = null;
                        foreach (ConsumerConnectionPoint point in manager.GetConsumerConnectionPoints(webPartConsumer))
                        {
                            if (point.InterfaceType == typeof(IFilterValues))
                            {
                                consumerConnection = point; break;
                            }
                        }
                        ProviderConnectionPoint providerConnection = null;
                        foreach (ProviderConnectionPoint point in manager.GetProviderConnectionPoints(webPartProvider))
                        {
                            if (point.InterfaceType == typeof(Microsoft.SharePoint.WebPartPages.ITransformableFilterValues))
                            {
                                providerConnection = point; break;
                            }
                        }

                        TransformableFilterValuesToFilterValuesTransformer transformMapping =
                          new TransformableFilterValuesToFilterValuesTransformer();
                        transformMapping.MappedConsumerParameterName = siteWebPart.Settings.MappingId;
                        manager.SPConnectWebParts(webPartProvider, providerConnection,
                                                 webPartConsumer, consumerConnection, transformMapping);
                        //manager.SPWebPartConnections.Add(conn);

                        oWeb.Update();
                    }

                }
            }
            catch (Exception ex)
            {
                //Exception
            }
            finally
            {
                if (manager != null)
                    manager.Dispose();

                if (file != null)
                {
                    file.CheckIn("Connected web parts, if any");
                    file.Publish("Connected web parts, if any");
                }
            }
        }

I have got some question on my another blog http://avinash-moss-expert.blogspot.com/2010/08/connect-webparts-programmatically-using.html
which says what is MappingID?

My answer is:-
I export webparts from existing site where i have existing connection between webparts. That means i have mapping ID too. I store these details and re-sue while import these webparts on other site. for detail about Export and Import please visit here Export and import webparts programmatically

However if you trying to import new webparts where you don’t have mapping details then you may try like this:-

SPRowToParametersTransformer transformer = new SPRowToParametersTransformer();
transformer.ProviderFieldNames = new string[] { "LinkTitleNoMenu" };
transformer.ConsumerFieldNames = new string[] { "ArticleEditionTitle" }

 

Hope it helps.
Thanks!
Avinash

calendarMarch 13, 2012 · cardInfoyen · commentsNo Comments
tagTags: , , , , , , , ,  · Posted in: MOSS, SharePoint

Leave a Reply

Spam Protection: , required

myworldmaps infoyen