Wednesday, May 21, 2014

Connecting Two ListView Webpart Programmatically

Introduction

 I am going to share my knowledge about connecting two SharePoint webparts programmatically.

Lists

 First, we need to create a lists before going to connect them. We need to have an common field in both list to connect. We'll code the connection after creating lists.

Connection between WebParts

This is the code to connect the webparts,


private void ConnectWebpart()
        {
            var currentWeb = SPContext.Current.Web;
            var currentAllowUnsafeUpdatesValue = currentWeb.AllowUnsafeUpdates;
            
            try
            {
                currentWeb.AllowUnsafeUpdates = true;
                SPLimitedWebPartManager wpManager = SPContext.Current.Web.GetLimitedWebPartManager("PageURL", 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("ProviderTitle", StringComparison.CurrentCultureIgnoreCase))
                    {

                        providerPart = wp as XsltListViewWebPart;
                        providerPart.TitleUrl = "TitleURL";
            

                    }
                    else if (wp.Title.Equals("ConsumerTitle", StringComparison.CurrentCultureIgnoreCase))
                    {
                        consumerPart = wp as XsltListViewWebPart;
                        consumerPart.TitleUrl = "TitleURL";
                       
                       
                    }

                // get connectionpoints 
                System.Web.UI.WebControls.WebParts.ProviderConnectionPointCollection providerConnections = wpManager.GetProviderConnectionPoints(providerPart);
                System.Web.UI.WebControls.WebParts.ProviderConnectionPoint pCP = null;

                if (providerConnections != null)
                {
                    pCP = providerConnections["DFWP Row Provider ID"];
                }
                // Consumer webpart
                System.Web.UI.WebControls.WebParts.ConsumerConnectionPointCollection consumerConnections = wpManager.GetConsumerConnectionPoints(consumerPart);
                System.Web.UI.WebControls.WebParts.ConsumerConnectionPoint cCP = null;
                if (consumerConnections != null)
                {
                    cCP = consumerConnections["DFWP Filter Consumer ID"];
                }

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

                // connecting  Webpart
                SPWebPartConnection filterConn = wpManager.SPConnectWebParts(providerPart, pCP, consumerPart, cCP, transformer);
                if (!wpManager.SPWebPartConnections.Contains(filterConn))
                {
                    wpManager.SPWebPartConnections.Remove(filterConn);
                    wpManager.SPWebPartConnections.Add(filterConn);
                }

                
            }
            catch (Exception ex)
            {
                            }
            finally
            {
                currentWeb.AllowUnsafeUpdates = currentAllowUnsafeUpdatesValue;
            }
        }


That's all.  Happy Coding.. !!!!

No comments:

Post a Comment