Custom Web Part Property Visual webpart sharepoint 2010

Here is detail step by step post of how to create a custom visual webpart with custom webpart property.

To create a custom Web part property, follow these steps:
1. Open Visual Studio 2010, and click File ➪ New ➪ Project. Select the SharePoint node and then select Empty SharePoint Project. Provide a name for the project (for example, CustomWPProperties) and click OK. When prompted, select “Deploy as farm solution” and click Finish.

2. Right-click the project, select Add ➪ New Item, and select Web Part in the SharePoint node.
Provide a name for the Web part (for example, WPPropertyExample) and click Add.

3. Right-click the main Web part class file (for example, WPPropertyExample.cs) and click View Code.

4. In the main Web part class file, add the following bolded code:

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Xml.Serialization;
namespace CustomWPProperties.WPPropertyExample
{
public class WPPropertyExample : WebPart
{
Label lblTitle = new Label();
public enum SharePointLists
{
Stats,
Players,
Customers,
Sales,
Budget
};
protected SharePointLists listOfList;
[Personalizable(PersonalizationScope.User),
WebBrowsable,
WebDisplayName("Available Lists"),
WebDescription("Available Lists in SharePoint Site.")]

public SharePointLists MySticks
{
get { return listOfList; }
set { listOfList = value; }
}

protected override void CreateChildControls()
{
lblTitle.Text = "Web Part with Property";
this.Controls.Add(lblTitle);
}
}
}
5. When you have finished, select Build ➪ Deploy Solution to deploy the Web part class to SharePoint.

6. When the Web part deploys successfully, navigate to the top-level site and click Site Actions ➪ Edit Page. Click on the wiki page.Click the Insert tab and then select Web Part.

7. Add the custom Web part.

8. After you’ve added the Web part, click the down arrow to expose the Web part menu. Select Edit Web Part.

9. Navigate to the Miscellaneous section in the Property Editor, and you’ll see the custom property you added earlier

0 comments:

Post a Comment

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer or anyone else. Should you have any questions or concerns please e-mail me at sharepointprogrammingblogger@gmail.com .

Copyright (c) 2010 @ myshaepointwork.blogspot.com. All rights are reserved.Do Not Copy.

@ Learning SharePoint.com