viernes, 17 de febrero de 2012

Creating a Web Part with Custom Properties

Create a Visual Web Part project in Visual Studio. In the file VisualWebPart1.cs, add your custom properties:

SharePoint_Blog_Creating_a_Web_Part_with_Custom_Properties

The Personalizable attribute is what tells is the Web Part framework to store this property in the database. The WebBrowsable attribute is what tells the Web Part framework to make this property visible in the Edit Web Part properties pane. The WebDisplayName attribute changes the display name visible within the Edit Web Part properties pane. In this case, if we did not alter the display name, it would appear as CustomProperty. Since we have now altered the display name to include a space, these two words will not run together. The Category attribute will change what section of the Edit Web Part properties pane this setting will appear in. If not specified, the custom property will appear in a Miscellaneous section.

In the file VisualWebPart1UserControl.ascx.cs, add a property to store our custom web part.

SharePoint_Blog_Creating_a_Web_Part_with_Custom_Properties

Now, back in the VisualWebPart1.cs file, alter the CreateChildControls() method to pass this web part into our user control.

SharePoint_Blog_Creating_a_Web_Part_with_Custom_Properties

Now, in the VisualWebPart1UserControl.ascx.cs file, update the code to display the value entered for our custom property in a label we have added to our VisualWebPart1UserControl.ascx file.

SharePoint_Blog_Creating_a_Web_Part_with_Custom_Properties

Now deploy your web part and add it to your page. When we go to edit our web part, we see there is a section called Custom Settings which contains our property.

SharePoint_Blog_Creating_a_Web_Part_with_Custom_Properties

Upon entering text into this field, hitting OK, and saving the page, we will now see our text appear in the label for our web part.

SharePoint_Blog_Creating_a_Web_Part_with_Custom_Properties

 

Fuente:

http://www.thesharepointblog.net/Lists/Posts/Post.aspx?List=815f255a-d0ef-4258-be2a-28487dc9975c&ID=105

Más información:

http://blog.concurrency.com/sharepoint/create-a-custom-web-part-for-sharepoint-2010/

http://allaboutmoss.com/2010/04/05/webpart-development-part-4-creating-custom-web-part-properties/

No hay comentarios:

Publicar un comentario