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);

Colors are the Key to Asthetic Appeal

I found a great color chart at 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.

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)"

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