Udostępnij za pośrednictwem


How To: Build a Custom Filter Provider Web Part

If you have been following the recent How To posts, here you'll recognize a common thread. At this point in my demo, I have a workflow that constructs a site collection and stores a JobID in the property bag of the root web site in the collection. I have a site definition that contains a BDC Item web part on it that I want to display the entity whose key is that value.

The next step in this process is that I needed to create a custom filter web part. This web part would read the value from the property bag and using a connection, configure the BDC Item part. I'll add another post in a bit on how I made the connection happen in the provisioning of the site, but for now here is the web part.

It has just one property which is the name of the property to retreive out of the property bag. Notice the implementation of the ITransformableFilterValues interface which enables this part to provide the value to the BDC web part. Some of the code could probably be streamlined here, but hey it is a demo and this works. I also setup the web part to support exporting all of its properties so I could easily get the XML I needed to put in the ONET.XML file of my site definition.

namespace SitePropertyFilter
{
    [Guid("3f6b0d1c-42ba-440b-9ae5-ccdcdb5f3799")]
    public class SitePropertyFilterPart : System.Web.UI.WebControls.WebParts.WebPart,ITransformableFilterValues
    {
        string propertyname = string.Empty;
        string propertyvalue = string.Empty;

        [WebBrowsable(),Personalizable(PersonalizationScope.Shared)]
        public string PropertyName
        {
            get
            {
                return propertyname;
            }
            set
            {
                propertyname = value;
                if (propertyname != String.Empty && this.Context != null)
                {
                    SPWeb web = SPControl.GetContextWeb(this.Context);
                    if (web.Properties[propertyname] != null)
                    {
                        propertyvalue = web.Properties[propertyname];
                    }
                }
            }
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            // add custom rendering code here.
            writer.Write("Set the SiteProperty setting of this web part to retrieve value from the web's property bag");
            if (propertyname != string.Empty)
            {
                writer.Write("Property " + propertyname + ":");
                writer.Write(propertyvalue);

            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.ExportMode = WebPartExportMode.All;
            if (propertyname != String.Empty && this.Context != null)
            {
                SPWeb web = SPControl.GetContextWeb(this.Context);
                if (web.Properties[propertyname] != null)
                {
                    propertyvalue = web.Properties[propertyname];
                }
            }
        }

        #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 "SiteProperty"; }
        }

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

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

        #endregion
    }
}

Comments

  • Anonymous
    March 28, 2007
    Live Search Web Service and Data View Web Part Live Search Web Part for MOSS White Paper Migrating a