Geeks With Blogs
Keep It Simple Simple Solutions for Complex Scenarios

imageSharepoint 2010 comes with huge enhancements in the development experience, exists a feature that i am sure a lot of programmers were waiting, this is the capability to create Webparts using a visual interface, in Sharepoint 2010 this is done and is called Visual WebParts

To create  a Visual Webpart, first we should create a Visual Studio Project, i selected the Empty Project template for Sharepoint 2010, when the project is created the Sharepoint Customization Wizard is launched, the first innovation are the 2 kinds of solutions that we can create and deploy to our Sharepoint Site: Sandboxed Solutions and Farm Solutions (the complete explanation about the differences need a new post) but a quick explanation is that the Sandboxed Solutions running on an isolated environment with limited resource access of this way if exists any exception in the solution, the entire farm is not affected just an inferior level, for this sample choose the second option.

 

I have to say that i LOVE the new Sharepoint development tools in VS2010, the Solution explorer is completely descriptive and useful to manage features of the solution. Add a new item to the solution, choose the Visual Webpart Template from the dialog  and edit the .webpart file to customize the name and description of our WebPart.

   1: private const string _ascxPath = @"~/_CONTROLTEMPLATES/Gotchas.Webparts/ListSummary/ListSummaryUserControl.ascx";
   2:  
   3:         public ListSummary()
   4:         {
   5:         }
   6:  
   7:         protected override void CreateChildControls()
   8:         {
   9:             Control control = this.Page.LoadControl(_ascxPath);
  10:             Controls.Add(control);
  11:             base.CreateChildControls();
  12:         }

image

What’s a Visual Webpart?, such as you can see in the webpart code file exists a reference to an ASP.Net UserControl, this control is loaded dynamically in the call to CreateChildControls method.

This approach allows edit visually the Webpart interface using the ASP.Net Control, but brings new challenges, first let me create the UI for the webpart, this is a really simple interface for this webpart, the goal for this webpart is show the total number of items from any list, for this i need create a custom property that allows set the input list, the name for this property is InputList, this property is decorated with various attributes that defines the behavior of the property in Browse Time.

 

The custom property is defined into the Settings category in the webpart properties window, this property stores the name of the list to get the total of items. The most easy way to set this value to be used in the UserControl is creating a public property in the UserControl and overwriting the code to load the UserControl, in the call to CreateChildControls method, in my case i have created the public property InputList in the user control to store the value from the webpart custom property, our code should like to this

   1: protected override void CreateChildControls()
   2:         {
   3:             SurveyBoardUserControl control = (SurveyBoardUserControl)this.Page.LoadControl(_ascxPath);
   4:             control.InputList = InputList;
   5:             Controls.Add(control);
   6:             base.CreateChildControls();
   7:         }

 

 

 

 

 

The code in the UserControl looks like

   1: public partial class SurveyBoardUserControl : UserControl
   2:     {
   3:         protected void Page_Load(object sender, EventArgs e)
   4:         {
   5:  
   6:             if (!String.IsNullOrEmpty(InputList))
   7:             {
   8:                 using (SPWeb currentWeb = SPContext.Current.Web)
   9:                 {
  10:                     SPList inputList = currentWeb.Lists.TryGetList(InputList);
  11:                     if (inputList != null)
  12:                     {
  13:                         totalItemsLabel.Text = inputList.ItemCount.ToString();
  14:                         listNameLabel.Text = InputList;
  15:                     }
  16:                     else
  17:                     {
  18:                         listNameLabel.Text = "Not found list";
  19:                     }
  20:                 }
  21:             }
  22:  
  23:  
  24:         }
  25:  
  26:         public String InputList { get; set; }
  27:     }

Finally only need deploy our solution, add our webpart to home page and set the Input List property, to see the results

image

See u

Posted on Monday, December 28, 2009 12:32 AM Sharepoint Server | Back to top


Comments on this post: Sharepoint 2010: Creating a Visual WebPart

# re: Sharepoint 2010: Creating a Visual WebPart
Requesting Gravatar...
It's really great program بلوتوث
Left by بلوتوث on May 17, 2010 11:21 AM

# re: Sharepoint 2010: Creating a Visual WebPart
Requesting Gravatar...
Thank you for your efforts to create this website. But I cannot follow your example, I just don't understand it. I guess I would be able to follow if I could read a step-by-step example. Yours is too confusing to me, sorry.
Left by Niguel Schumacher on Jul 14, 2010 4:48 AM

# re: Sharepoint 2010: Creating a Visual WebPart
Requesting Gravatar...
thanks this was a great help!
Left by joe on Sep 21, 2010 3:55 AM

# re: Sharepoint 2010: Creating a Visual WebPart
Requesting Gravatar...
Its Great......
Left by Deepika on Dec 26, 2010 11:25 PM

# re: Sharepoint 2010: Creating a Visual WebPart
Requesting Gravatar...
Its Great..
Left by Deepika on Dec 26, 2010 11:27 PM

# re: Sharepoint 2010: Creating a Visual WebPart
Requesting Gravatar...
Is it best practise to use the html table instead auf stackpanels or something like that?

I can't find any example how to use the designer with many controls for example to build an register form or something like that.
Left by devcs on Jul 28, 2011 6:16 PM

# re: Sharepoint 2010: Creating a Visual WebPart
Requesting Gravatar...
Hi @devcs,

U are right, exists best techniques for the page layout instead of tables, but for the sample a table works fine

When u says designer are u referring to Sharepoint designer or the designer view on VS??, well both works in a similar way if you need put many controls in a single view for me is better the code view

Juan
Left by Juan Mestas on Jul 28, 2011 10:58 PM

# re: Sharepoint 2010: Creating a Visual WebPart
Requesting Gravatar...
It's Nice,Really !
Left by raja on Nov 02, 2011 6:15 AM

# re: Sharepoint 2010: Creating a Visual WebPart
Requesting Gravatar...
I have some problem in visual webpart communicate.
there are two webpart in a feature,provider and consumer webpart,both are visual webpart.
the provider implement the interface a IProduct,the consumer has a reference of IProduct.now,how can i pick controls'value from providerUserControl.ascx and send it to consumer.

if it is a asp.net webpart,i know how to implement this,but when use visual webpart,i don't kown how to implement the two visual web part communicate problems.
Left by Leo on Jan 12, 2012 4:45 AM

Your comment:
 (will show your gravatar)


Copyright © Juan Mestas J. (aka gotchas) | Powered by: GeeksWithBlogs.net