donderdag 26 november 2009

Creating a visual web part

One of the new Visual Studio 2010 project types is the 'Visual Web Part' project. Because the Microsoft guys claim that by using this project it is now much easier to create a web part, a gave it a try.

This 'guide' is meant to inform beginners so don't expect any fancy stuff. I just want to give an overview on how web parts can be created using Visual Studio 2010.

1. When starting up Visual Studio, I started by creating a Visual Web Part project.



2. Visual Studio will ask you which SharePoint site it has to use for deployment.



3. Visual Studio will then create the project and by default, the user control (.ascx file) will be opened. Switch to design view to start the work.



4. To do a first test, I added a label and a button. I then dubbel clicked the button and put some code for the event. As you can see, all the controls you can use are in the toolbox on the left. Double clicking a control will take you to its event listener. This is pretty much the same as creating WinForms.







5. Something pretty new in Visual Studio 2010 is the control you have over your features and the solution package (WSP). On the right, in the 'Solution Explorer', you see 'Package' and 'Features' elements. Opening them and double clicking their content will give you a lot of options. You have control over what files will be included in the feature, the scope, its XML and for the package you can control which features will be included. Using the 'Manifest' and 'Advanced' buttons you will get access to even more options like which external assemblies to add to your package.
It's clear that Microsoft included almost all WSP builder functionalities and has gone even further making it much more easier and straightforward to create SharePoint solutions.






6. You can now press F5 and Visual Studio will create the WSP and deploy your web part. It will ask you to go directly into debug mode. Select this option. You will see I put an error in the code and the debugger will automatically pick it up when clicking the button.
To be able to click on the button, you must place the web part on a page. Go to 'Edit page', insert, insert a web part and you will find your web part in the 'Custom' category.









7. To actually make this web part work, I deleted the label, placed a textbox instead and adjusted the code in the event listener. I then redeployed and repositioned the web part. You can press the button to see the result.





Geen opmerkingen:

Een reactie posten