Showing posts with label provider webpart. Show all posts
Showing posts with label provider webpart. Show all posts

Wednesday, October 16, 2013

Connect custom webpart to List View Webpart in SharePoint 2013 .Connected Webparts send parameter from Custom webpart

I had a requirement to sending a parameter from custom webpart and sending it to a report view webpart .This code can also be used to connect any custom webpart with List view webpart.

All you need to do is
1)Open visual studio
2)Create a project
3)Using the template "Webpart" which is grouped under Sharepoint section in VS create a item
named "FilterWebpart".

Use the below code in FilterWebpart.cs



using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Collections.ObjectModel;

namespace ProviderWebpartVS.FilterWebpart
{
    [ToolboxItemAttribute(false)]
    public class FilterWebpart : System.Web.UI.WebControls.WebParts.WebPart, ITransformableFilterValues
    {

        string propertyvalue = "BSW3";     

        #region ITransformableFilterValues Members

        public bool AllowAllValue
        {
            get { return true; }
        }

        public bool AllowEmptyValue
        {
            get { return true; }
        }

        public bool AllowMultipleValues
        {
            get { return false; }
        }

        public string ParameterName
        {
            get { return "ConnectionProperty"; }
        }

        public System.Collections.ObjectModel.ReadOnlyCollection<string> ParameterValues
        {
            get
            {
                string[] values = new string[] { propertyvalue };
                return new ReadOnlyCollection<string>(values);
            }
        }

        [ConnectionProvider("Connection Property Filter", "ITransformableFilterValues", AllowsMultipleConnections = true)]
        public ITransformableFilterValues SetConnectionInterface()
        {
            return this;
        }

        #endregion
        protected override void CreateChildControls()
        {
        }
    }
}

4)Now deploy the solution into a sharepoint site.
5)Open the site in browser 
6)Add this custom webpart .
7) Also add report view webpart which has parameters or any other list view webpart
8)Edit the webpart -->click -->Connection -->Connect to any webpart on the page.A configuartion window popups on the page where you can configure which parameter is connected to which webpart.


9)Here in this example propertyvalue will be sent as parameter to other webpart.You can change it to take arrary of string in "string[] values"  as shown in code.You your custom logic here to fill the data into string[] values.



Thanks to links below which helped in achieving this

http://blogs.msdn.com/b/edhild/archive/2007/03/28/how-to-build-a-custom-filter-provider-web-part.aspx