Wednesday, July 30, 2008

Custom WebPart connections and Custom WebPart Properties

Custom Properties does not seem to work any longer the way described in article below. Seems like new patches and SP's from Microsoft makes certain changes. Try inheriting webpart from Microsoft.SharePoint.WebPartPages.WebPart.
Note: Do not forget to include System.Web.UI.WebControls.WebParts in your using statements.
Updated properties are as follows:
[WebBrowsable(true)]
[Personalizable(false)]
[WebPartStorage(Storage.Shared)]
[WebDisplayName("User Name(Domain\\username)")]
[WebDescription("User to display in the WebPart.")]
[SPWebCategoryName("Options")]
==================================
I want to share some of my finding on Custom WebPart connections and Custom WebPart Properties. Attached image, shows 2 custom web parts, communicating via a custom web part connection. Additionally, it shows grouping of custom controls under "Custom Properties" label.

Creating a custom WebPart connection is quiet easy. Simply follow the steps below:

a) Create an interface IStringConnection

public interface IStringConnection
{
string ProvidedString { get; }
}

b) Implement System.Web.UI.WebControls.WebParts.WebPart and IStringConnection in connection provider webpart

[ConnectionProvider("String Provider")]
public IStringConnection ConnectionInterface()
{
return this;
}

public string ProvidedString
{
get { return m_string; }
}

c) On connection receiver webpart add following code

IStringConnection m_providerPart = null;

[ConnectionConsumer("String Consumer")]
public void GetConnectionInterface(IStringConnection providerPart)
{
m_providerPart = providerPart;
}

=> However, this technique will not allow you to connect to default/out-of-box lists and libraries. For that, one needs to implement IwebPartField, IWebPartRow, and IWebPartTable on receiver webpart.

That's about it.

Snippet below adds a textbox to Web Part propery view.

// Assign the default value.
[DefaultValue(c_MyStringDefault)]

// Property is available in both Personalization
// and Customization mode.
[WebPartStorage(Storage.Personal)]

// The caption that appears in the property sheet.
[FriendlyNameAttribute("Custom String")]

// The tool tip that appears when pausing the mouse pointer over the friendly name in the property pane.
[Description("Type a string value.")]

// Display the property in the property pane.
[Browsable(true)]

[XmlElement(ElementName="MyString")]
// The accessor for this property.
public string MyString
{
get
{
return _myString;
}
set
{
_myString = value;
}
}

Hope this helps!!






Cheers


3 comments:

Anonymous said...

Good work. Lots of useful information. I have a question regarding data view/form web parts. Like you described in your posts that if you change guid to name then the data webpart do not break when transporting. I am using custom connection string for data view web parts. The cutsom connection string refers to connection in web.config file. It works fine but when i check the page out and press save, it reverts the connection string back to hard coded one instead of custom one which refers to web congig. Is there any way to retain custom connection even after check out and save. thanks

Unknown said...

Hello,
Greate article. Could you please forward a code sample of a connectable webpart to me please. My email is bdvaal@yahoo.com
Many thanks in advance,
Ben

Anonymous said...

I've got a question about this. I'm trying to write two custom web parts where one passes a string to the other.

As I'm trying to use the WSS extensions and this keeps each web part in it's own assembly, the interface "IStringConnection" has to be in a separate assembly and signed too.

So I've got a solution set up in VS2008 (and I'm developing on the machine hosting WSS).

My solution is structured so that the two web parts (projects A & B) know nothing about each other (as in one does not reference the other). And the use the "IStringConnection" interface to communicate as explained ahead.

To allow the two parts to be connected I've created project C and referenced this from projects A & B. Project C is a class library and contains the custom interface "IStringConnection" I have defined for passing data from project A to project B.

This all builds fine, but when I use the VS2008 deploy option it falls over with this error.

" Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information. "

My question is there any thing that I m doing wrong?

I tried signing and installing the "IStringConnection" interface assemble in GAC too but it didn't helped much.

Thanks for any help.