SharePoint Claims Web Part

Besides Visual Studio debugging there are not many tools out there for troubleshooting your SharePoint Claims Based environment. Even SharePoint itself doesn’t show much information about the claims environment. Because of this, SharePoint Administrators can find this new way of managing corporate identities quite challenging. As part of the deployment plan for claims based SharePoint, include the development of some tools to assist in this. For instance, you could create a simple Web Part or Application Page showing the claims the currently logged in user has.

Although I found a few tools on the web, I decided to create one myself. Both easy and fun!

I started with an empty SharePoint Project in Visual Studio and added a normal Web Part item. I had to go with a Farm solution, because the Identity Framework is not available in Sandboxed solutions.

Important: The Claims Based model SharePoint uses is built on Windows Identity Foundation (WIF). To be able to work with this framework, we need to have the Windows Identity Foundation SDK, so be sure to have it installed. You can download the SDK here.

Next step: add the reference to the Identity Foundation:

image

Add the Web Part code:

using System;
using System.Data;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.IdentityModel.Claims;

namespace DeltaN.SharePoint.WebParts
{
    [ToolboxItemAttribute(false)]
    public class MyClaims : WebPart
    {
        protected override void CreateChildControls()
        {
            try
            {
                // Get the claims
                IClaimsPrincipal cp = Page.User as IClaimsPrincipal;
                if (cp != null)
                {
                    DataRow dr;
                    DataTable claimsTable = new DataTable();
                    claimsTable.Columns.Add("Type", typeof(string));
                    claimsTable.Columns.Add("Value", typeof(string));

                    IClaimsIdentity ci = (IClaimsIdentity)cp.Identity;
                    foreach (Claim c in ci.Claims)
                    {
                        dr = claimsTable.NewRow();
                        dr["Type"] = c.ClaimType.ToString();
                        dr["Value"] = c.Value.ToString();
                        claimsTable.Rows.Add(dr);
                    }

                    // Standard SPGridView to display our claims table
                    SPGridView claimsGrid = new SPGridView();

                    // This eventhandler is used to add the word-break style
                    claimsGrid.RowDataBound += new GridViewRowEventHandler(claimsGrid_RowDataBound);

                    // AutoGenerate must be false for SPGridView
                    claimsGrid.AutoGenerateColumns = false;
                    claimsGrid.DataSource = claimsTable;

                    SPBoundField boundField;

                    boundField = new SPBoundField();
                    boundField.HeaderText = "Type";
                    boundField.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
                    boundField.DataField = "Type";
                    claimsGrid.Columns.Add(boundField);

                    boundField = new SPBoundField();
                    boundField.HeaderText = "Value";
                    boundField.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
                    boundField.DataField = "Value";
                    claimsGrid.Columns.Add(boundField);

                    for (int i = 0; i < claimsGrid.Columns.Count; i++)
                    {
                        claimsGrid.Columns[i].ItemStyle.Wrap = true;
                        // Distribute the columns evenly
                        claimsGrid.Columns[i].ItemStyle.Width = Unit.Percentage(100 / claimsGrid.Columns.Count);
                    }

                    claimsGrid.DataBind();

                    this.Controls.Add(claimsGrid);
                }
            }
            catch (Exception ex)
            {
                this.Controls.Add(new LiteralControl(ex.Message));
            }
        }

        protected void claimsGrid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // Claim values can contain SIDs or long token references which mess up the Web Part layout
                e.Row.Cells[1].Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
            }
        }

    }
}

Most code has to do with style and formatting the SPGridView. From here it’s just Package and Deploy. This is how the Web Part should look on the page:

MyClaims1

You can download the Visual Studio 2010 solution here.

Advertisements

2 comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s