A buddy of mine recently asked if there was an easy way to retrieve the site column ID’s of a site…. Not “out of the box”… but its easy enough for me to create one, and other people might benefit. Its also a good example of how we can use the SPGridView control in our custom application pages, a subject on which I might elaborate later. The results look something like this:


So…. here it is John 😉

protected override void OnLoad(EventArgs e)

    SPWeb web = SPContext.Current.Web;
    // Get a reference the roles that are
    // bound to the user and the sites role
    // definition to which we need to verify
    // the user against
    SPRoleDefinitionBindingCollection usersRoles = web.AllRolesForCurrentUser;
    SPRoleDefinitionCollection siteRoleCollection = web.RoleDefinitions;
    SPRoleDefinition roleDefinition = siteRoleCollection[“Full Control”];

    //Check if the user is in the role
    if ((usersRoles.Contains(roleDefinition)) || web.CurrentUser.IsSiteAdmin)
        //Check if post back to run
        //code that initiates the page
        if (IsPostBack != true)
public void initPage()
    // Create a DataTable to hold our Site Column Data
    DataTable tblSiteColumns = new DataTable();

    // We will use an SPGridView to display the data
    // Create and SPBoundField for each column that
    // will be displayed in the SPGridView
    SPBoundField fldNameField = new SPBoundField();
    fldNameField.DataField = “ColumnName”;
    fldNameField.HeaderText = “Column Name”;
    fldNameField.ItemStyle.Wrap = false;

    SPBoundField fldIDField = new SPBoundField();
    fldIDField.DataField = “ColumnID”;
    fldIDField.HeaderText = “Column ID”;
    fldIDField.ItemStyle.Wrap = false;

    SPBoundField fldDescriptionField = new SPBoundField();
    fldDescriptionField.DataField = “ColumnDescription”;
    fldDescriptionField.HeaderText = “Column Description”;

    // Add the columns to the SPGridView
    // on the grid
    //Populate the DataTable with the Name ID and
    //Description of each field in the site
    foreach (SPField field in SPContext.Current.Web.Fields)
        string[] rowValue = new string[3];
        rowValue[0] = field.Title;
        rowValue[1] = field.Id.ToString();
        rowValue[2] = field.Description;

    //Bind the DataTable to the Grid
    grdSiteColumns.DataSource = tblSiteColumns;

    Site Column IDs

    Site Column IDs



As with my other examples, copy the code above and save it as an ASPX page in the layouts directory
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS

Then access the page from any SharePoint site as follows: http://siteaddress/_layouts/pagename.aspx

And for those of you wondering; there is no need to close the SPWeb object in this example; it is a Shared Resource, a reference to the SPContext and should be left to be managed by the system. For more information on other “Best SharePoint Coding Practices” see the MSDN article Best Practices: Using Disposable Windows SharePoint Services Objects (http://msdn.microsoft.com/en-us/library/aa973248.aspx)