Dec 15, 2009

Create a sharpoint webpart in Visual Studio 2010

Creating a web part becomes much easier with Visual Studio 2010 (beta 2). The structure is a dummy web part which simple load a user control:


this.page.LoadControl(_ascxPath);


In theory, a control can be added through

  • dynamically in CreateChildControls() of web part file

  • declarative in ascx file of user control

  • dynamically in Page_load() of user control's code behind

adding a custom property is very easy now, all need to do is add the following 2 attributes to any property: [WebBrowsable(true), Personalizable(true) ]

in .webpart, you can add default velues just as any other OOB properties

As custom properties are open to injection of user script (good or bad), SharePoint 2010 introduce 2 new flags to enforce security at both webpart level and property level:

SafeAgainstScript attribute in

[RequiresDesignerPermission] attribute for web part's property

see here for more details