Wednesday, February 5, 2014

Visual Web Part in sharepoint 2010

 Visual Web Part Project Creation
In this task, you create a Visual Web Part project in Microsoft Visual Studio 2010.

1. Start Visual Studio 2010, click File, point to New, and then click Project.
2. Navigate to the Visual C# node in the Installed Templates section, click SharePoint, and then click 2010.
3. Select the Visual Web Part project template, provide a name, a location for your project, and then click OK.


Figure 1. Select Visual Web Part project type

  1. In the What local site do you want to use for debugging dropdown, select the site to use (such as http://localhost/sites/SampleWebSite). Also select the Deploy as a farm solution option and then click  Finish.
After the project is created, Solution Explorer contains the default Visual Web Part named VisualWebPart1 (see Figure 2). Also see in Solution Explorer the presence of the Features and Package nodes. A feature organizes your application in a way that SharePoint Foundation understands. Features can be deployed to SharePoint Foundation at the site or Web level. The package contains features and other assets used when you deploy solutions to SharePoint Foundation.
[note: better remove the default visual webpart and a new visual wbpart with a good name, otherwise all your visual webpart have the same name ,that is VisualWebPart1 ]

Figure 2. The SampleWebPart project in the Solution Explorer 

Add the following code:


Button btn =null;
Label lb=null;
protected override void  CreateChildControls (....)
{
lb=new Label();
 btn=new Button{ text="Hello"};
btnClick+=new EventHandler(btn_Click);

this.controls.add(lb);
this.controls.add(btn);

}
void btn_Click()
{
lb.Text="We are fine";
}
/* usually we get a flow layout , we dont 've any control over the position of controls.for that we use RenderContents method   */

 Protected override  void RenderContents(..)
{
writer .RenderBeginTag(HtmlTextWriterTag.Table);
writer .RenderBeginTag(HtmlTextWriterTag.Tr);
writer .RenderBeginTag(HtmlTextWriterTag.Td);
lb.RenderControl(writer);
writer.RenderEndTag();
writer .RenderBeginTag(HtmlTextWriterTag.Td);
btn.RenderControl(writer);
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderEndTag();

}


Build and Deploy the Web Part

In this task, you build and deploy the Web Part project.
Build and deploy the project by using one of the following options:
When debugging the SharePoint solution, use the F5 key to build and deploy your solution. By doing this, the debug experience includes steps such as help for creating a Web Part Page and resetting Internet Information Services (IIS).
Alternately, you can build and deploy your solution by clicking the Build menu, selecting Build Solution, verifying that the solution builds without any errors, and then selecting Deploy Solution.

Create a Webpart Page
In this task, you create a Web Parts page to contain the Web Part, unless one has already been created for you.

Here is a sample webpart page

  1. If you clicked F5 to debug your application, by default, the page where you create a Web part page is displayed. Otherwise, open the SharePoint site, click Site Actions, click View All Site Content, click Create, scroll and select the Web Part Page option.
  2. In the Web Part Page screen, provide the information requested about that particular Web Parts page. For example, provide a name (SampleWebPartPage) and layout template for the page.
  3. In the Document Library dropdown, select Site Pages, and then click Create. SharePoint creates and displays your Web Parts page.

Add the Web Part to the Web Parts Page
In this task, you add the Web Part to the Web Parts page and test the solution.
On the Web Parts page, click into the Add a Web Part text in the zone where you want the Web Part displayed.
In the Categories list, click Custom. In the Web Parts box, click your visual webpart name.
In the About the Web Part box at the top of the page, click Add. The Web Part is added to the zone that you selected as shown in Figure 4. Note that the lists and SubWebs are displayed in a hierarchical view.





0 comments:

Post a Comment