Friday, January 14, 2011

Hello World Webpart

Select New Project… from the Startup page to display the New Project dialog box. Expand the SharePoint node and select 2010 to display the available project templates, as shown in Figure 1.
Figure 1: Available project templates


Visual Studio 2010 offers templates to support the most common SharePoint development projects. Select the Visual Web Part template and click OK to start the SharePoint Customization Wizard. The wizard prompts you for the URL of the SharePoint site to be used for deployment and debugging and lets you choose between farm and sandboxed solution types.
Farm solutions deploy to the SharePoint farm and have full access to the SharePoint API and all SharePoint resources. In contrast, sandboxed solutions deploy to the site collection, have limited access to the SharePoint API, and can only access data within the site collection. This lets farm administrators allow sandboxed solutions, which won't impact the entire farm, to be deployed to the server. Visual Web Parts can be deployed only in farm solutions, so the sandboxed option isn't available in this case.
After you create the project, the Visual Web Part opens in the default Source view. Click the Design tab to switch to a WYSIWYG view. Regardless of which view you prefer, you can drag and drop controls from the toolbox onto the designer.
Now I'll build a typical "Hello World" Web Part. Click the designer near the top and type "My Hello World Web Part." You'll notice you have all the text tools, such as font size and color, in the menu. Under the text, drag a label and button control onto the designer. Figure 2 shows what the designer will look like so far.
Figure 2: The designer so far

Double-click the button to display the code behind the form. Add a using directive to access the SharePoint object model (a reference to the Microsoft.SharePoint.dll assembly is included in all SharePoint projects).
using Microsoft.SharePoint;

Next, add the following code to the button event handler Button1_Click. This code gets the current user for the SharePoint site and sets the label to the LoginName.

SPUser user = SPContext.Current.Web.CurrentUser;
Label1.Text = user.LoginName;

With the code now complete, set a breakpoint in the first line of the event handler. It's time to deploy the Web Part and check whether it works as expected. Press F5 to instruct the project system to build the assembly, package the feature, and deploy the solution to the SharePoint site. In addition, the debugger will be attached and the browser will launch to display the SharePoint site.
When the browser launches, you're taken to the home page of the site you specified during project creation. Click on the Site Actions menu at the top left of the page, and choose More Options. In the dialog page that appears, select the item Web Part Page and click Create. Enter HelloWorld as the Name, and leave the Layout Template selected as the default. Change the Document Library to Shared Documents and click the Create button. In the header section of the new page, click Add a Web Part to display the Web Part picker. In the Categories section, select Custom, and in the Web Parts section select the Web Part VisualWebPart1. Click the Add button to add the Web Part to the page, and then click the Stop Editing button on the SharePoint ribbon. Figure 3 shows the Web Part displayed on the HelloWorld page in the browser.
Figure 3: The HelloWorld page

Issue(can be encountered)

Error : 
image

Solution:
For example, you may consider custom document library to store the images for your solution, etc.
However, sometimes we need to get rid from sandboxed solution. Then we need to change the project type into non-sandboxed solution. To change project type to non-sandboxed solution,
1. Click on project property
2. Set Sandboxed Solution = False.
image