Implementation of IWebPartParameters web part

A client was asking for a connectable web part, sending a parameter to data form web part (dfwp) build by SharePointDesigner.

The first you will read on Microsoft msdn for SharePoint 2010 about this requirement should be: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webpartpages.communication.iparametersoutprovider.aspx.

Uups, there is a bold NOTE: This API is now obsolete (IParametersOutProvider) and where is the link to the new one?  .. i was  adapting the example for my test solution.

If i copied the complete code to my own web part class and compile this code first time, i got a message on my classname: ‘Microsoft.SharePoint.WebPartPages.Communication.IParametersOutProvider’ is obsolete: ‘Use System.Web.UI.WebControls.WebParts.IWebPartParameters instead’, why it’s not written in the note?

Trying this code, i was running in an unhandled exception on Step #6. The PartCommunicationConnect was called, the given connectedPart was Null! The exception occurs if you connect a dfwp with parameter binding to my wp implementing IParametersOutProvider. So i realized, this should not my solution.. searching for a fully description on IWebPartParameters.. there are not so many complete stories on the net yet..

Now i will share working c# code like in the above link.

In my example, a hidden web part extracts a sequence from current web URI and send the ‘data’ parameter to a dfwp connected by parameter binding.

   1:  // Common .NET required namespaces
   2:  using System;
   3:  using System.ComponentModel;
   4:   
   5:  // WebPart required namespaces
   6:  using System.Web;
   7:  using System.Web.UI;
   8:  using System.Web.UI.WebControls;
   9:  using System.Web.UI.WebControls.WebParts;
  10:   
  11:  //Step #1: Reference the SharePoint namespace.
  12:  using Microsoft.SharePoint;
  13:   
  14:  namespace ConnectionCodeSamples.WebPartParametersProvider
  15:  {
  16:      // Step #2: Inherit from the WebPart base class and implement the 
  17:      // IWebPartParameters interface.
  18:      [ToolboxItemAttribute(false)]
  19:      public class WebPartParametersProvider : WebPart, IWebPartParameters
  20:      {
  21:          // Step #3: Declare private variables for configuration , handling 
  22:          // and a value passed as parameter.
  23:          private int _strFirst;
  24:          private int _strLength;
  25:          private string _myError = string.Empty;
  26:   
  27:          private string _urlData = "WebPartParametersProvider url 'data' value";
  28:   
  29:          // Step #4: the provider must implement a list of property descriptors 
  30:          // that the consumer is interested in 
  31:          private PropertyDescriptorCollection _objParameters;
  32:          public PropertyDescriptorCollection Parameters
  33:          {
  34:              get { return _objParameters; }
  35:              set { _objParameters = value; }
  36:          }
  37:   
  38:          // Step #5: Constructor
  39:          public WebPartParametersProvider()
  40:          {
  41:              this.ExportMode = WebPartExportMode.All;
  42:              base.Hidden = true;
  43:          }
  44:   
  45:          // Step #6: implement ConnectionInterface
  46:          [ConnectionProvider("my parameters provider")]
  47:          public IWebPartParameters ConnectionInterface()
  48:          {
  49:              return this;
  50:          }
  51:   
  52:          // Step #7: implement a schema for the data returned by the provider
  53:          // you need this for the IWebPart* interfaces
  54:          public System.ComponentModel.PropertyDescriptorCollection Schema
  55:          {
  56:              get
  57:              {
  58:                  PropertyDescriptorCollection objProperties;
  59:                  PropertyDescriptor[] arrProperties =
  60:                      new PropertyDescriptor[Parameters.Count];
  61:                  TypeDescriptor.GetProperties(this);
  62:   
  63:                  objProperties = TypeDescriptor.GetProperties(this);
  64:                  int intParameterCount = 0;
  65:                  foreach (PropertyDescriptor objProperty in Parameters)
  66:                  {
  67:                      if (Parameters[objProperty.Name] != null)
  68:                      {
  69:                          intParameterCount++;
  70:                          arrProperties[intParameterCount] = objProperty;
  71:                      }
  72:                  }
  73:   
  74:                  objProperties = new PropertyDescriptorCollection(arrProperties);
  75:                  return objProperties;
  76:              }
  77:          }
  78:   
  79:          // Step #8: implement a method called SetConsumerSchema(), allows a consumer
  80:          // to express its interest in a set of parameters provided by provider
  81:          public void SetConsumerSchema(System.ComponentModel.PropertyDescriptorCollection schema)
  82:          {
  83:              Parameters = schema;
  84:          }
  85:   
  86:          // Step #9: implement the GetParametersData(), this will hold a reference
  87:          // to the parameters consumer web part after a connection is established
  88:          public void GetParametersData(ParametersCallback callback)
  89:          {
  90:              StateBag objParameters = new StateBag();
  91:   
  92:              foreach (PropertyDescriptor objProperty in Parameters)
  93:              {
  94:                  switch (objProperty.Name)
  95:                  {
  96:                      case "data":
  97:                          objParameters.Add("data", UrlData);
  98:                          break;
  99:                      default:
 100:                          throw new Exception("WebPartParametersProvider: Unknown parameter name");
 101:                  }
 102:              }
 103:   
 104:              callback.Invoke(objParameters);
 105:          }
 106:   
 107:          // Step #10: implement properties for web part configuration 
 108:          // and to hold our data extracted from current url
 109:          [WebBrowsable(true),
 110:          Personalizable(PersonalizationScope.Shared),
 111:          WebDescription("The first char in web url to search for (0 based).")]
 112:          public int UrlFirst
 113:          {
 114:              get
 115:              {
 116:                  return this._strFirst;
 117:              }
 118:              set
 119:              {
 120:                  this._strFirst = value > 0 ? value : 0;
 121:              }
 122:          }
 123:   
 124:          [WebBrowsable(true),
 125:          Personalizable(PersonalizationScope.Shared),
 126:          WebDescription("The length of the string to extract after first char.")]
 127:          public int StrLength
 128:          {
 129:              get
 130:              {
 131:                  return this._strLength;
 132:              }
 133:              set
 134:              {
 135:                  this._strLength = value > 0 ? value : 0;
 136:              }
 137:          }
 138:   
 139:          public string UrlData
 140:          {
 141:              get { return _urlData; }
 142:              set { _urlData = value; }
 143:          }
 144:   
 145:          // Step #11: init the all data
 146:          protected override void OnInit(EventArgs e)
 147:          {
 148:              base.OnInit(e);
 149:   
 150:              if (UrlFirst != 0 && StrLength > 0)
 151:              {
 152:                  try
 153:                  {
 154:                      string url = SPContext.Current.Web.Url;
 155:                      if (url.Length >= UrlFirst + StrLength)
 156:                      {
 157:                          _urlData = url.Substring(UrlFirst, StrLength);
 158:                      }
 159:                  }
 160:                  catch (Exception se)
 161:                  {
 162:                      _myError = se.Message + ", " + se.InnerException;
 163:                  }
 164:              }
 165:          }
 166:   
 167:          // Step #12: create controls to show properies, 
 168:          // data and web part configuration hints in page edit mode
 169:          protected override void CreateChildControls()
 170:          {
 171:              Controls.Clear();
 172:   
 173:              if ((UrlFirst == 0 || StrLength == 0))
 174:              {
 175:                  //.. show a how to configure web part
 176:              }
 177:              else
 178:              {
 179:                  //.. show your properties and the assigned data
 180:              }
 181:          }
 182:      }
 183:  }

for more description, please study also on msdn the IWebPartParameters interface: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webparts.iwebpartparameters.aspx

Leave a Reply

  

  

  


2 + = vier

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>