Search

rafelo

Advancing Productivity and Operational Efficiency through Cloud Services and Apps

Month

September 2008

SharePoint Site Column ID’s

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:

image

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)
        {
            initPage();
        }
    }
    else
    {
        Response.Redirect(“/_layouts/accessdenied.aspx”);
    }
}
public void initPage()
{
    //****************************************************
    // Create a DataTable to hold our Site Column Data
    DataTable tblSiteColumns = new DataTable();
    tblSiteColumns.Columns.Add(“ColumnName”);
    tblSiteColumns.Columns.Add(“ColumnID”);
    tblSiteColumns.Columns.Add(“ColumnDescription”);
    //****************************************************

    //****************************************************
    // 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
    grdSiteColumns.Columns.Add(fldNameField);
    grdSiteColumns.Columns.Add(fldIDField);
    grdSiteColumns.Columns.Add(fldDescriptionField);
    //******************************************************
    //******************************************************
    //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;

        tblSiteColumns.Rows.Add(rowValue);
    }
    tblSiteColumns.AcceptChanges();
    //*******************************************************
    //*******************************************************
    //Bind the DataTable to the Grid
    grdSiteColumns.DataSource = tblSiteColumns;
    grdSiteColumns.DataBind();
    //*******************************************************
}

    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)

Table Height Percentages in SharePoint Master Pages

You may have noticed an issue while specifying height percentages for tables and cells within SharePoint master pages, they don’t always work. The table height attribute itself  has never really been supported by the W3c, and the table row and cell height attributes have been recently deprecated. But why is it that they sometimes seem to work and other times they don’t, even when using the same browser… more importantly why are they so inconsistent in our master pages?

As it turns out most browsers will render the table, table row, and table cell height percentages just fine (maybe with some minor variances across different browsers)  as long as they are specified as part of the elements style. But there are a couple of tricks:

Did you provide a height for your html and body elements?…. It may come as a big surprise, but many browsers don’t automatically set the height of the body to be the same as the height of the window. Your table and or cell height may very well be expanding to 100% of the body, its just that the body itself its not 100% of the window; this can be easily solved by incorporating the following style in your pages or CSS:

html,body{ 
margin:0;
padding:0;
height:100%;
border:none }

You may be wondering why you haven’t run into this in the past, and perhaps it has to do with the next subject; the document declaration. I’ve seen some sample master pages that include only part of it, or don’t include it at all. If you’ve used some of those samples, the height issue… well it may not have been an issue at all. Why? Lets dig in a little deeper.

Check your document declaration, it declares what version of HTML is used in the document. The W3C provides 3 different declarations:

  • The HTML 4.01 Strict DTD – includes all elements and attributes that have not been deprecated or do not appear in frameset documents.
  • The HTML 4.01 Transitional DTD – includes everything in the strict DTD plus deprecated elements and attributes (most of which concern visual presentation).
  • The HTML 4.01 Frameset DTD – includes everything in the transitional DTD plus frames as well.

Most (if not all) of the out-of-the-box master pages use the Transitional DTD. Its declared in each master page as follows:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

Failing to include this directive or declaration in your pages (or including it partially) tells the browser that the page follows no actual HTML standard.. or at least fails to tell the  browser which HTML standard the page follows. Either way the browser takes its best shot, and surprisingly tends to do a better job in understanding what we meant the page to look like. Now, you could remove the declaration altogether; but that would simply be the lazy way out. Including the declarations will probably result in your pages rendering better across existing browsers and next generation browsers.

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: