Thursday, December 24, 2009

How to: Create a Web Part using using user control in SharePoint 2007 - Part 1

Hi all, 

I attended a Microsoft event last weekend (19 Dec 2009) and Randy Williams demonstrated new development techniques pertaining to SharePoint 2010. There are lot of new innovations such as: 
  • Developer dashboard 
  • Client model (Object Model - OM)
  • SilverLight 
  • Sandboxed Solutions  
  • REST (Representational State Transfer
  • Language Integrated (LINQ) for SharePoint
  • Visual Web Part
Apparently the concept of "Visual Web Part" is not an entirely new in SP 2007. The SmartPart has been used which has drag-drop capabilities. But it has few drawbacks such as it has to be on farm level and after we can use it. 

There is one more technique which I am showing in this post. As we know that writing a web part in the code is ghastly task and particularity when we have to add controls such as drop down, calendar, lists etc. by hand. Adding these controls are succumb to errors and developers have to really troubleshoot on the controls instead on the business logic.     

Following on steps where we have to: 

i) Create a new ASP.NET project
ii) Design the User Control (.aspx) using Visual Studio 2005/2008. 
iii) Copy the User control to the WSS file system. 
iv) Create a WSPBuilder project and wrap the user control. 
v) Deploy Web Part, Activate the feature and add web part on the SharePoint 2007 page. 


Create an ASP.NET project.

Step 1) Fire up Visual Studio, 
File >> New >> Web Site >>
















Step 2)  Right click on project >> Add new item >> Web User Control 














NOTE: We should uncheck "Place code in separate file" as the demo we are doing is simple. It's possible to add c sharp as well for complex solutions.

Step 3) Put some text and drag a calendar control. 


















Step 4) Build the site. 

Step 5) Copy this WebUserControl.ascx to the "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\CONTROLTEMPLATES"





Create a WSPBuider project

Step 6) Create a WSPBuidler project name: WSPWebPart


Step 7) From the Categories: WSPBuilder; Templates: Web Part Feature














Step 8) Modify this simple code:

protected override void CreateChildControls()
        {
            if (!_error)
            {
                try
                {
                    UserControl userControl;
                    base.CreateChildControls();

                    // Your code here...
                    this.Controls.Add(new LiteralControl(this.MyProperty));
                    this.Controls.Clear();
                    userControl = (UserControl)this.Page.LoadControl(@"/_controltemplates/WebUserControl.ascx");
                    this.Controls.Add(userControl);

                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

Step 9) Use WSPBuider menu >> Build WSP >> Deploy

Step 10) Activate the feature 



Step 11) Site Actions >> Edit Page >> 












Step 12)  Add that web part



















But, there are few minor issues here: 


i) We have to copy the ASP.NET user control to the WSS file system. If we forget this step, we will surely get an error such as "The file '/_controltemplates/WebUserControl.ascx does not exist."


ii) At times we have to "web.config" trust level from "WSS_Mininal" to "Full". The issue becomes more serious if we have to deploy on the production where we do not have on the production enviorment.

iii) Web Part stays in WSS file system and therefore can not used in a SandBoxed solutions. (concept of SP 2010)

SP 2010 has definitely eased the overall designing the web part and provides a "Visual Web Part"template  which is a part of Visual Studio 2010. 


Cheers, 
--aaroh


Download the source


No comments:

How to display Sharegate administrative dynamic reports via Power BI on SharePoint Online

After working in the SharePoint domain for a couple of years as SharePoint consultant , I am fortunate enough to help customers from man...