Tuesday 2 July 2013

Programmatically Establish the connection between existing List View Webparts on page

        /// <summary>
        /// This method is used to establish a connection between  two XsltListViewWebParts in the same page
        /// </summary>
        /// <param name="web"></param>
        /// <param name="pagename"></param>
        /// <returns></returns>
        private void ConnectableWebParts(SPWeb web, string pagename)
        {
            try
            {
                // Get the WebParts that need to get connected
                string providerWebPartTitle = "PROVIDER WEBPART TITLE";
                string consumerWebPartTitle = "CONSUMER WEBPART TITLE";
                //ListViewWp(web);
                string url = web.Url.Trim('/');

                // get the web part manage which we will use to interact
                SPLimitedWebPartManager wpManager = web.GetLimitedWebPartManager(url + "/SitePages/" + pagename, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
                XsltListViewWebPart providerPart = null;
                XsltListViewWebPart consumerPart = null;

                foreach (System.Web.UI.WebControls.WebParts.WebPart wp in wpManager.WebParts)
                {
                    if (wp.Title.Equals(providerWebPartTitle, StringComparison.CurrentCultureIgnoreCase))
                    {
                        providerPart = wp as XsltListViewWebPart;
                    }
                    else if (wp.Title.Equals(consumerWebPartTitle, StringComparison.CurrentCultureIgnoreCase))
                    {
                        consumerPart = wp as XsltListViewWebPart;
                    }
                }

                // get connectionpoints
                ProviderConnectionPointCollection providerConnections = wpManager.GetProviderConnectionPoints(providerPart);
                ProviderConnectionPoint pCP = null;
                if (providerConnections != null)
                {
                    pCP = providerConnections["DFWP Row Provider ID"];
                }

                ConsumerConnectionPointCollection consumerConnections = wpManager.GetConsumerConnectionPoints(consumerPart);
                ConsumerConnectionPoint cCP = null;
                if (consumerConnections != null)
                {
                    cCP = consumerConnections["DFWP Filter Consumer ID"];
                }

                SPRowToParametersTransformer transformer = new SPRowToParametersTransformer();
                transformer.ProviderFieldNames = new string[] { "@Title" };
                transformer.ConsumerFieldNames = new string[] { "Consumer Field Name(i.e Lookup column name)" };

                // connect the webparts
                SPWebPartConnection filterConn = wpManager.SPConnectWebParts(providerPart, pCP, consumerPart, cCP, transformer);
                wpManager.SPWebPartConnections.Add(filterConn);

            }
            catch (Exception ex)
            {
            
            }
        }