Wednesday, June 25, 2014

How to Create a Web Part With Custom Properties



Create Custom Web Part

1. Click 'Start,' 'All Programs,' 'Microsoft Visual Studio 2010,' and then'Microsoft Visual Studio 2010.' Select 'New' from the 'File' menu, and then 'Project.'

2. Select 'Visual C# Projects' or 'Visual Basic Project,' and then 'Web Part Library.' Enter a name for your project and specify the location. Click 'OK.'

3. Copy 'Microsoft.SharePoint.dll' from 'c:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\60\ISAPI' to the folder where you are developing your Web Part.

4. Select 'Add Reference' from the 'Project' menu. Click 'Browse' on the '.NET' tab and navigate to the folder you copied the dll file. Select the dll file and click 'Open.' Click 'OK.'

5. Select 'Add Reference' from the 'Project' menu. Double-click 'System.Xml.dll' on the '.NET' tab. Click 'OK.'

6. Add the following directive near the top of your code:using Microsoft.SharePoint.WebPartPages;using System.Xml.Serialization;using System.Web.UI.HtmlControls;

7. Replace '[ToolboxData('

')]' with '[ToolboxData('

')].'Also replace 'public class WebCustomControl1 : System.Web.UI.WebControls.WebControl' with 'public class SimpleWebPart : WebPart.'Add '[XmlRoot(Namespace='MyWebParts')]' above the declaration for your WebPart class.

8. Replace 'protected override void Render(HtmlTextWriter output)' with 'protected override void RenderWebPart(HtmlTextWriter output).' If necessary, replace the name space for your Web Part and XML Root.

9. Set the attributes of your Web Part in the property pane. Select 'Build Solution' from the 'Build' menu.

Deploy Web Part

10. Copy your Web Part's assembly to the bin folder of your SharePoint server, usually -- c:\inetpub\wwwroot\bin.

11. Open 'c:\inetpub\wwwroot\web.config' in Visual Studio .NET or Notepad. Add the following lines of code into the '

' block:

12. Copy the Web Part Definition (.dwp) file created for you by Visual Studio into the SharePoint server's bin folder.

13. Open a Web Part Page on your SharePoint server. Click 'Modify My Page' or 'Modify Shared Page' as appropriate and select 'Add Web Parts.' Click 'Import' and browse to the .dwp file and click 'Upload.'

14. Refresh the page if necessary. Your browser should display your custom Web Page. You may type some text into the text box and click 'Set Web Part Title' to test your Web Part.

No comments:

Post a Comment