Reusable web Part in Sharepoint 2013 using Visual studio
Creation of Webparts and
Resuse the User control in Sharepoint 2013
There is small difference
in Visual WebPart Sharepoint 2013 and Sharepoint 2010
Overview:
SharePoint 2010, there
would be a WebPart class and a UserControl Class in a Visual WebPart.
The associated UserControl (.ascx) file will be loaded during
execution in WebPart class through CreateChildControl Method. Since
two different classes are involved, the properties created in WebPart
class has to be passed to UserControl Class to make use of those
values during execution. But in SharePoint 2013 Visual WebPart, no
separate UserControl class is created for the .ascx file and only
WebPart class is available. The controls that are placed in ascx file
are automatically converted in to c# code and placed in a file with a
name [Name of Web Part].ascx.g.cs. This is a partial
class of the original WebPart class that is inherited from
“System.Web.UI.WebControls.WebParts.WebPart”
class and created in the name of Visual WebPart
From the above we can use
Visual Webpart as a Resuable componets any where in the page,but the
use of user control in webpart is important as we can resuse the
Controls any where in the webpart as well as in pages in sharepoint
with webpart.
Step 1:
Create Visual Webpart from
sharepoint 2013 project
once u created the folder
structure as follow as
Step
2:
Create User Control by
adding new item choose(User Control-Farm Only)
Note: The user
control will work only for farm only not suitable for sandbox deploy
Once User control created in
the project the user control creates under Folder "Control
Templates" shown as below.
Step 3:
Once
after create the user control, we can use sharepoint List to pull all
the data and bind into simple Gridview(control) to res use in the
webpart.
The
below simple code is to pull the data from sharepoint list and
bindin the simple gridview control of asp.net.
Step 4:
Deploy
user control to use in the Webpart
Right
Click the Project->Deploy.
All
user control is deployed in the Path "C:\Program Files\Common
Files\Microsoft Shared\Web
ServerExtensions\15\TEMPLATE\CONTROLTEMPLATES\WebPartProject\SiteUserControl.ascx"
Step
5 :
Register
User control in Webpart
Below
code to register the User control in web part
1.Mention the path of user control
reside(It will Reside on
@"~/_CONTROLTEMPLATES/15/WebPartProject/SiteUserControl.ascx)
Note:Sharepoint 2010 user control reside on 14 hive folder
Sharepoint 2013 user control reside on 15 hive
folder
2.Override
the CreateChildControls method to load the user control on the
webpart page
Step
6:
Use Webpart in the Page in Sharepoint 2013
Once after registering the user control in the Webpart build the
Project and deploy the same.
In
below screen explain sthe how to add the webpart in the page
1.
Go to Page tab
navigation and click edit
2.
Click on Insert tab
3.
Click Web Part
4.
In Categories Choose
Custom folder(all customize webpart deployed in this folder)
5.
Choose the Visual webpart on the right under Parts Caption
6.
Click add to View Your webpart with Resusable user control in the
Page
7.
Similar way u can create the Page and resuse this webpart any where
Resusable control with Visual Webparts
Comments
Post a Comment