Creating a Basic Web Part
1. Start Microsoft Visual Studio 2010.
2. On the File menu, point to New, and then click Project.
3. In the New Project dialog box, in the Installed Templates pane, expand the SharePoint node under the programming language that you want to use, and then select 2010.
4. In the Templates pane, select Empty SharePoint Project.
5. Type FirstWebpart as the project name, and then click OK.
6. In the SharePoint Customization Wizard, select Deploy as a sandboxed solution, and then click Finish.
7. In Solution Explorer, click the FirstWebpart project.
8. On the Project menu, click Add New Item.
9. In the Add New Item dialog box, select the Web Part template, type DisplayMessageWebPart as the Name, and then click Add.
To create the Web Part property
1. In the DisplayMessageWebPart file, copy and paste the following code to create a basic customizable property.
private string customMessage = "Hello, world!";
public string DisplayMessage
{
get { return customMessage; }
set { customMessage = value; }
}
2. Add the following tags above the public declaration to allow changes on a per-user basis.
[WebBrowsable(true),
WebDescription("Displays a custom message"),
WebDisplayName("Display Message"),
Personalizable(PersonalizationScope.User)]
Now you have created a personalizable Web Part property.
• The WebBrowsableAttribute attribute ensures that your custom property renders in the editing tool pane in SharePoint Foundation.
• The WebDescriptionAttribute attribute displays a tooltip to help guide users when they are editing your custom property.
• The WebDisplayNameAttribute attribute shows a display name for your custom property.
• The PersonalizableAttribute attribute determines whether changes to your custom property affect all users or only individual users.
Overriding the CreateChildControls Method
By overriding the CreateChildControls method, you can tell the Web Part what operations to perform when the page is visited. In this example, the Web Part renders the user-defined text.
To override the CreateChildControls method
protected override void CreateChildControls()
{
base.CreateChildControls();
LiteralControl message = new LiteralControl();
message.Text = DisplayMessage;
Controls.Add(message);
}
Deploying and Testing the Web Part
Press F5 to deploy the Web Part.
1. Click Site Actions, and then click More Options.
2. On the Create page, click Web Part Page, and then click Create.
3. On the New Web Part Page page, for the Name, type DisplayMessageWebPartPage, and then click Create.
4. Click Add a Web Part in the Header zone.
5. On the ribbon, click the Insert tab, and then click Web Part.
6. In the Categories pane, click Custom.
7. In the Web Parts pane, click DisplayMessageWebPart, and then click Add.
8. On the ribbon, click the Page tab, and then click Stop Editing
0 comments:
Post a Comment