Before you create a Visual Web Part in SharePoint 2010, it is important that you understand the files that are included in a Visual Studio 2010 Web Part project.
- Elements.xml: This file contains deployment instructions and the files that are needed to deploy the web part to the /_ControlTemplates folder of the SharePoint hive.
- VisualWebPart1.cs: This file simply hosts the ASP.Net user control used in the web part.
- VisualWebPart1.webpart: This XML file consists of the properties that describe the web part. You can add a Title, Description and an Error Message too. This file also make the web part visible in the web part gallery.
- VisualWebPart1UserControl.ascx: This file is the actual ASP.Net User Control that you edit in the Design mode of the Visual Studio 2010.
Some other important tips to keep in mind when developing new web parts:
- It is important that you use the System.Web.UI.WebControls.WebParts.WebPart namespace instead of the older Microsoft.SharePoint.WebPartPages.WebPart;
- Visual Web Parts can only be deployed as Farm Solutions, this is because a visual web part is deployed to the /_ControlTemplates folder and the web part needs farm permissions to access this folder;
- The CreateChildControls function adds the ASP.Net User Control to the Controls collection of the web part class.
- The VisualWebPart1.cs file consists of a private constant “_ascxPath” which consists of the path where the visual web part is installed in the SharePoint hive. Do not modify this path or rename anything, Visual Studio 2010 will do that for you;
- A SharePoint 2010 web part page uses it’s own classes called SPWebPartManager and SPWebPartZone.
If you have any more tips you would like to share with our readers please feel free to leave a comment below.Contact Me link in the top menu.