Today I relearned an important lesson on the Page_Load and OnLoad event handlers for ASP.Net user controls. We cannot assume that they are one and the same in the page lifecycle. By default Visual Studio will create an event handler for Page_Load when you doubleclick on the designer panel for a user control.
This default behavior of Visual Studio set up a disaster in conjunction with my SharePoint development framework.
In my framework I've created a Framework.Base.BaseUserControl class that inherits from the System.Web.UI.UserControl class. The purpose of this BaseUserControl class is to serve as an ancestor class for all my user controls that get deployed with SharePoint solutions. In addition, the web part wrapper class (i.e. Framework.Base.BaseWebPart) assumes that the wrapped user control descends from this class.
Here's the OnLoad event handler in BaseUserControl.
protected override void OnLoad(EventArgs e)
{
this.MyBaseControlLoadLogic();
base.OnLoad(e);
}
Here's the result in Visual Studio for my user control.
protected override void Page_Load(EventArgs e)
{
base.OnLoad(e);
this.MyControlLoadLogic();
}
Everything compiles. But then all I got was a blank screen. I did some debugging and then discovered my mistake. The base.OnLoad(e) call im my Page_Load was causing a stack overflow that crashed my development web server. All I had to do was rename Page_Load as OnLoad.
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.MyControlLoadLogic();
}
It worked :)
Monday, August 2, 2010
Thursday, July 29, 2010
Referencing the current user in SharePoint Code
In this example I'm making the assumption that you have a web page or web part hosted in SharePoint that is calling a user control with a C# codebehind (i.e. a .ascx.cs file). The code here is called in the partial class.
There are 2 object references available to use in gathering current user information:
System.Web.HttpContext.Current.User.Identity has a type definition of the System.Security.Principal.IIdentity interface.
Microsoft.SharePoint.SPContext.Current.Web.CurrentUser has a type definition of the
Microsoft.SharePoint.SPUser class.
In my code if my Active Directory account is adegaston on the domain called MyDomain then the output of the following function would be:
"Login:MyDomain\adegaston; Display:Alex Degaston."
private string SampleCode()
{
string loginName = System.Web.HttpContext.Current.User.Identity.Name;
string displayName = Microsoft.SharePoint.SPContext.Current.Web.CurrentUser.Name;
string kFormat = "Login:{0}; Display:{1}.";
return String.Format(kFormat, loginName, displayName);
}
There are 2 object references available to use in gathering current user information:
System.Web.HttpContext.Current.User.Identity has a type definition of the System.Security.Principal.IIdentity interface.
Microsoft.SharePoint.SPContext.Current.Web.CurrentUser has a type definition of the
Microsoft.SharePoint.SPUser class.
In my code if my Active Directory account is adegaston on the domain called MyDomain then the output of the following function would be:
"Login:MyDomain\adegaston; Display:Alex Degaston."
private string SampleCode()
{
string loginName = System.Web.HttpContext.Current.User.Identity.Name;
string displayName = Microsoft.SharePoint.SPContext.Current.Web.CurrentUser.Name;
string kFormat = "Login:{0}; Display:{1}.";
return String.Format(kFormat, loginName, displayName);
}
Colors are the Key to Asthetic Appeal
I found a great color chart at http://cloford.com/resources/colours/500col.htm to help me with the look/feel of websites.
Monday, July 19, 2010
Upgrading an Existing SharePoint Solution Package
If I already have a solution installed on SharePoint 2007 but I need to quickly upgrade it then I run the following script.
stsadm -o upgradesolution -filename mySolution.wsp -name mySolution.wsp -immediate -allowgacdeployment
stsadm -o execadmsvcjobs
stsadm.exe -o copyappbincontent
stsadm -o execadmsvcjobs
Note: replace "mySolution" with the solution's actual name.
stsadm -o upgradesolution -filename mySolution.wsp -name mySolution.wsp -immediate -allowgacdeployment
stsadm -o execadmsvcjobs
stsadm.exe -o copyappbincontent
stsadm -o execadmsvcjobs
Note: replace "mySolution" with the solution's actual name.
Thursday, July 8, 2010
Post-Build Event Script in Visual Studio
When doing SharePoint development its good to choose on-the-fly whether I want to compile for the sake of making sure my code is compilable or to verify that my code deploys/activates successfully to a website. To accomplish this I've created a Post-Build Event script for my project as follows.
if $(ConfigurationName) == Debug GOTO DoDebugSkip
DEL "$(TargetDir)$(ProjectName).wsp"
"C:\Program Files\WSPTools\WSPBuilderExtensions\WspBuilder" -SolutionPath "$(ProjectDir)" -OutputPath "$(TargetDir)"
ECHO ON
call "c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat"
gacutil /u $(ProjectName).dll
gacutil /i $(TargetDir)$(ProjectName).dll
@set PATH=C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN;%PATH%
stsadm -o deactivatefeature -filename $(ProjectName)\Feature.xml -url http://localhost/
stsadm -o execadmsvcjobs
stsadm -o retractsolution -name "$(ProjectName).wsp" -allcontenturls -immediate
stsadm -o execadmsvcjobs
stsadm -o deletesolution -name "$(ProjectName).wsp" -override
stsadm -o execadmsvcjobs
stsadm -o addsolution -filename "$(ProjectName).wsp"
stsadm -o execadmsvcjobs
stsadm -o deploysolution -name "$(ProjectName).wsp" -url http://localhost -immediate -allowGacDeployment -allowCasPolicies -force
stsadm -o execadmsvcjobs
stsadm -o activatefeature -filename $(ProjectName)\Feature.xml -url http://localhost/
stsadm -o execadmsvcjobs
stsadm –o copyappbincontent
stsadm -o execadmsvcjobs
iisreset
:DoDebugSkip
if $(ConfigurationName) == Debug GOTO DoDebugSkip
DEL "$(TargetDir)$(ProjectName).wsp"
"C:\Program Files\WSPTools\WSPBuilderExtensions\WspBuilder" -SolutionPath "$(ProjectDir)" -OutputPath "$(TargetDir)"
ECHO ON
call "c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat"
gacutil /u $(ProjectName).dll
gacutil /i $(TargetDir)$(ProjectName).dll
@set PATH=C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN;%PATH%
stsadm -o deactivatefeature -filename $(ProjectName)\Feature.xml -url http://localhost/
stsadm -o execadmsvcjobs
stsadm -o retractsolution -name "$(ProjectName).wsp" -allcontenturls -immediate
stsadm -o execadmsvcjobs
stsadm -o deletesolution -name "$(ProjectName).wsp" -override
stsadm -o execadmsvcjobs
stsadm -o addsolution -filename "$(ProjectName).wsp"
stsadm -o execadmsvcjobs
stsadm -o deploysolution -name "$(ProjectName).wsp" -url http://localhost -immediate -allowGacDeployment -allowCasPolicies -force
stsadm -o execadmsvcjobs
stsadm -o activatefeature -filename $(ProjectName)\Feature.xml -url http://localhost/
stsadm -o execadmsvcjobs
stsadm –o copyappbincontent
stsadm -o execadmsvcjobs
iisreset
:DoDebugSkip
Monday, June 28, 2010
HRESULT: 0x80040E14
When attempting to add content to a List we got the following error "Exception from HRESULT: 0x80040E14". This error may occur when the file system on the data tier has run out of space. I used mstsc.exe to login to the server running SQL Server for this SharePoint farm and discovered there was only 500K left on the 40 gig C: drive. In SQL Server Management Studio I found the chief culprits were two log files hogging up approx. 23 gig and confirmed the files' location/sizes on the C: drive. I truncated both of them, this C: drive now has over 23 gig of free space, and this resolved the issue. Here's the command I used for truncating one of these log files.
USE SharePoint_Config
GO
ALTER DATABASE SharePoint_Config Set Recovery Simple
GO
ALTER DATABASE SharePoint_Config Set Recovery Full
GO
DBCC SHRINKFILE('SharePoint_Config_log', 2)
GO
USE SharePoint_Config
GO
ALTER DATABASE SharePoint_Config Set Recovery Simple
GO
ALTER DATABASE SharePoint_Config Set Recovery Full
GO
DBCC SHRINKFILE('SharePoint_Config_log', 2)
GO
Wednesday, November 18, 2009
Workflow for Sharepoint 2010
TFS 2010 and Sharepoint 2010 are quickly headed towards purchase and adoption by IT organizations of enterprises all over the globe. I'm determined to get up-to-speed on any changes to the Sharepoint Workflow functionality. I found a collection of videos at http://msdn.microsoft.com/en-us/sharepoint/ee513154.aspx to help techies get up-to-speed. However it takes 22+ minutes to watch all 5 videos so I planned an approach that should work in grasping all this new info. Here are my steps.
(1) See http://msdn.microsoft.com/en-us/library/ee537015(office.14).aspx for the latest in Workflow features for Sharepoint 2010.
(2) Go to http://msdn.microsoft.com/en-us/sharepoint/ee513154.aspx and click on the "Test your skills" button to really learn the main ideas to grasp.
(3) look at the code snippets at http://blogs.msdn.com/pandrew/pages/GettingStarted2010Snippets7.aspx,
(4) speed read through Paul Andrew's http://msdn.microsoft.com/en-us/magazine/ee335710.aspx article.
(5) Plan to return to http://msdn.microsoft.com/en-us/sharepoint/ee513154.aspx with your earphones during your lunch break to watch these videos.
And finally ....
(6) repeat steps 1-4 more carefully in order that you master the material.
A similar approach could be taken with the other 9 modules at http://msdn.microsoft.com/en-us/sharepoint/ee513147.aspx for getting up-to-speed on the Sharepoint 2010 platform.
(1) See http://msdn.microsoft.com/en-us/library/ee537015(office.14).aspx for the latest in Workflow features for Sharepoint 2010.
(2) Go to http://msdn.microsoft.com/en-us/sharepoint/ee513154.aspx and click on the "Test your skills" button to really learn the main ideas to grasp.
(3) look at the code snippets at http://blogs.msdn.com/pandrew/pages/GettingStarted2010Snippets7.aspx,
(4) speed read through Paul Andrew's http://msdn.microsoft.com/en-us/magazine/ee335710.aspx article.
(5) Plan to return to http://msdn.microsoft.com/en-us/sharepoint/ee513154.aspx with your earphones during your lunch break to watch these videos.
And finally ....
(6) repeat steps 1-4 more carefully in order that you master the material.
A similar approach could be taken with the other 9 modules at http://msdn.microsoft.com/en-us/sharepoint/ee513147.aspx for getting up-to-speed on the Sharepoint 2010 platform.
Subscribe to:
Posts (Atom)