SharePoint: Custom provider web part

Got a requirement to read a querystring and pass it to some of the web parts on a page. I thought that this would be easy since I can use the OOTB query string url filter web part to read and then pass the value after setting up the connection.

However, apparently some of the consumer web parts need the value to be in different format. My first thought was to append another querystring with the same value (just different format) but that would not be very nice (especially when the url length is restricted). This is where ITransformableFilterValues comes in handy. The interface is used for web part to web part connections.

Below are an example on how this interface can be implemented.


public class CustomQueryStringFilterWebPart : System.Web.UI.WebControls.WebParts.WebPart, ITransformableFilterValues
    {        
        string _queryStringKey = "category";
		string _filterValue = "";
		
        [WebPartStorage(Storage.Shared)]
        [WebBrowsable(true)]
        [WebDisplayName("Query String Key")]
        [Personalizable(PersonalizationScope.Shared)]
        [Category("Settings")]
        public string QueryStringKey
        {
            get { return _queryStringKey; }
            set { _queryStringKey = value; }
        }      

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            _filterValue = HttpContext.Current.Request.QueryString[_queryStringKey];
        }

        public bool AllowAllValue
        {
            get { return true; }
        }

        public bool AllowEmptyValue
        {
            get { return false; }
        }

        public bool AllowMultipleValues
        {
            get { return false; }
        }

        public string ParameterName
        {
            get { return "Query String Filter"; }
        }

        public System.Collections.ObjectModel.ReadOnlyCollection<string> ParameterValues
        {
            get
            {
                string value = string.Format("[{0}]", _filterValue.ToUpper());
                string[] values = new string[] { value };
                return new ReadOnlyCollection<string>(values);
            }
        }

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

The code above is a custom web part that implements ITransformableFilterValues. It gets the querystring value and convert it to uppercase.

Add the above web part to a page and to pass the transformed value, connections to the consumer web parts need to be created. Once the connection is setup, everything should just work 🙂

3 Responses to SharePoint: Custom provider web part

  1. Can you please explain in more detail how to implement this solution? I have created a basic web part with the above code. I have added it to a page, however, when I attempt to make a connection to or from this web part I receive an error. The “connections” menu option states “the Web Part you are connecting from does not allow authoring of contents.” How do I go about making this work? What are my next steps? To attempt to pull the querystring I appended the variable “&category=dog” to the end of my querystring. Was this correct? I am assuming your web part will convert my “dog” text into “DOG”, right?

    • akurniaga says:

      Hi pivotalsolution,

      Can you post your code so I can get a better understanding the cause of the problem please? The web part example works as a provider(source) which gets the query string and do some conversion with the query string value before passes the value to consumer web parts.

      One thing to check is whether you have set the query string for the web part to read. You can do this by going to edit web part -> settings. You should have Query String Key property and in your case you have to set it to ‘category’.

      Let me know how it goes 🙂

  2. Amitt says:

    Thanks for this post. I spend days on finding this but finally found this post when I gave up my seraching 🙂 . I really thankful about this post.

    On the 48th line of code string value = string.Format(“[{0}]”, _filterValue.ToUpper());
    I had to remove the square brackets to make it working, so that correct value is passed.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: