Archive for May, 2008


Convert ASP.NET page to Custom Application Page in SharePoint

There are many ways to run custom code in SharePoint 2007.  Arguably one of the simplest is to create a Custom Application Page, sticking all the source code into a script tag at the top of the page.  You don’t have to muck around with compiling dlls or modifying the web.config file.

  1. Create a new aspx file.  Put this at the top:
    <%@ Assembly Name="Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71E9BCE111E9429C" %>
    <%@ Page language="C#" MasterPageFile="~/_layouts/application.master" Inherits="Microsoft.SharePoint.WebControls.LayoutsPageBase" %>

    Watch out! SharePoint Designer likes to remove the ~/ from your MasterPageFile declaration. I have no idea why it does this. I sat there entering it, hitting save, and watching it disappear like 4 times before I opened up Notepad and put it in. Weird.

  2. Add any namespaces needed.
    <%@ Import Namespace="System" %>
    <%@ Import Namespace="System.Web" %>
    <%@ Import Namespace="System.Web.UI" %>
  3. Add a script tag (don’t forget to runat server!).
    <script runat="server"></script>
  4. Copy the code from your code-behind and stick it into the shiny new script tag you just created.  Don’t include those pesky namespace or class declarations, just the functions you want.
  5. Application Master has some ContentPlaceHolders; we care about three of them.
    • PlaceHolderMain will hold your tag soup from the ASPX file.
    • PlaceHolderPageTitle will hold the title that shows in the title bar of the browser.
    • PlaceHolderPageTitleInTitleArea holds the page title that goes in the SharePoint chrome (the yellow bar of a typical application page).
    <asp:Content ID="Main" contentplaceholderid="PlaceHolderMain" runat="server">
    	<!-- all your aspx content goes here -->
    <asp:Content ID="PageTitle" runat="server" contentplaceholderid="PlaceHolderPageTitle">
    	Title in browser title area
    <asp:Content ID="PageTitleInTitleArea" runat="server" contentplaceholderid="PlaceHolderPageTitleInTitleArea">
    	Title in SharePoint chrome
  6. Create a folder in the LAYOUTS virtual directory.  Should be in the C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\LAYOUTS\ directory.  Don’t ever skip this step! Always separate your grubby code from the pristine beauty of the default code with folders!
  7. Copy your newly created .ASPX into the folder created in step 6.
  8. Now you can get to your snazzy new Custom Application page in any site by adding /_layouts/PageName.aspx to whatever site you want.
  9. There is no step nine.

Have fun!


Testing Syntax Highlighter

I’m using syntaxhighlighter plugin to manage my source code listings. Just activated it, and now I’m testing it out.

Here’s some SQL:

SELECT i.first_nm, i.last_nm, iui.username, s.descr
FROM tbl_individual i
JOIN tbl_ind_userid iui ON i.pn_id = iui.pn_id
JOIN tblkp_ind_status s ON i.status_cd = s.status_cd

Here’s some XML:

<trustLevel name="WSS_Medium" policyFile="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\config\wss_mediumtrust.config" />
<trustLevel name="WSS_Minimal" policyFile="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\config\wss_minimaltrust.config" />
<remove verb="GET,HEAD,POST" path="*" />
<add verb="GET,HEAD,POST" path="*" type="Microsoft.SharePoint.ApplicationRuntime.SPHttpHandler, Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
<add verb="OPTIONS,PROPFIND,PUT,LOCK,UNLOCK,MOVE,COPY,GETLIB,PROPPATCH,MKCOL,DELETE,(GETSOURCE),(HEADSOURCE),(POSTSOURCE)" path="*" type="Microsoft.SharePoint.ApplicationRuntime.SPHttpHandler, Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
<add verb="*" path="Reserved.ReportViewerWebControl.axd" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<add verb="*" path="*/rss.xml" type="CKS.EBE.RssHandler, CKS.EBE, Version=, Culture=neutral, PublicKeyToken=3e8b700c069fb747" />

And here’s some C#:

private string GetRandomPasswordUsingGUID(Int32 length)
string result = System.Guid.NewGuid().ToString();
result.Replace(“-”, String.Empty);
if ((length < 1) || (result.Length < length))
throw new Exception(“Length must be between 1 and ” + result.Length.ToString());
return result.Substring(0, length);


Save as .doc

Ok, this post is for a friend of mine.  By default, Office 2007 saves files in a new format, .docx.  If you have clients that have not upgraded yet, there might be some confusion when they open your files.  Ideally, you would have them get the Office Compatability Pack, but that’s not always feasible.  You can save each file as a .doc file, but that can be onerous to remember every time.

Word Save Option DialogHere’s how you set Office 2007 to save documents every time so that Office 2003 users can use your docs with no headaches.

  1. Open Word 2007
  2. Click the Office circle icon in the upper left corner
  3. Click Word Options button
  4. Click Save in the list on the left
  5. In the “Save files in this format:” drop down list, select “Word 97-2003 Document (*.doc)”
  6. Click OK

If you send other files (Excel, PowerPoint) then you might want to do the same thing for them.  But otherwise, that’s it.  You’re done.



Hi.  My name is Nathan DeWitt, and I’m a software developer.  I’m just getting started with this site, and I think I’m going to keep it the default WordPress look for a while until I figure out the direction (design-wise) I want to go with.

I plan on using this space to write about some professional topics, which for me involve ASP.NET, MOSS/WSS, MS SQL Server and some of the other Microsoft development stack.  There will also be some other stuff mixed in as I explore new areas.  Pretty much stuff I come across that seems interesting should find its way here.  If nothing else, this will be a place to remind myself what section of that web.config should be edited, or how to extend a system partition in WinXP.

I look forward to receiving any comments or emails.