Skip to main content

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
3.Override the Render Content to render the Html in the Visual Web part


















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

Popular posts from this blog

Asp.net- Encrypt and Decrypt connection strings in web.config file

In this article I will explain how to encrypt or decrypt connectionStrings in web.config file using asp.net. If we are using applications in our internal servers with security then it’s ok if we deploy our applications in shared host environment then we have chance to arise security problems to avoid these problems asp.net 2.0 provided built in protected configuration model functionality to encrypt or decrypt few sections of web.config file those are RSAProtectedConfigurationProvider :   This is default provider and uses the RSA public key encryption algorithm to encrypt and decrypt data. DataProtectionConfgurationProvider : This provider uses windows data protection application programming interface to encrypt and decrypt the data. The encrypting and decrypting of connection strings in web.config file will do by using aspnet_regiis.exe  command line tool and  code behind . First Method : First we will do encryption and decryption using  aspnet...

CRM Automate build Solution using Powershell commands

In CRM if there is any solution movement from other enviornment like DEV,PROD,STG we use to export the solution and import the solution to the respective environment by logging into MS Dynamics CRM. This will tends to extra effort to do manually for developers or release managers. So what if release user or test user can build and deploy the Solution deployment without CRM intervention(CRM loggin in) We need to do some automate build and deployment. In this article i write about how Import/Export solution automate to the various environment without intervention of logging into MS Dynamics CRM. XRM CI Framework,this is one tool use to automate the build in CRM       Below link is for download the XRM CI Framework https://xrmciframework.codeplex.com/releases/view/125516 Download and extract the ZIP file . First HelloWorld example “WhoAmIRequest” 1.Open "Powershell" and "Run as a Administrator" 2.Navigate to Extrac...

Auto number generation using C# and SQL

Auto number generation using C# and SQL ------------------------------------------------------ Req :  Create a Autonumber for Particular Application form(Eg:Order or Quote) Approach : ------------- 1.Create a Separate autonumber configuration table which looks like below AutoNumConfig table --------------------------   AutoNumberId  AutoNumber   FormNameorFormId   -------------------------------------------    1             ORD-0002     Order or 1066    2             QUO-0002     Quote or 1067 2.Create Transaction table for Application  Form(Eg:Order/Quote) which looks like below OrderTable -------------- OrderId   OrderName  OrderAutoNum -------------------------------------------   1       Spartan      ORD-0001   2       Xamarin      ORD-0002 QuoteT...