PerformancePoint Dashboard Render Callback

Doing some JIT branding on a SharePoint 2010 site with bootstrap v2, I needed a way to determine when the PerformancePoint elements on a dashboard had finished loading. PerformancePoint has a problem (or maybe it’s my problem) calculating the width of its components with regard to bootstrap; I fooled it by giving the content area extra margin-left (67px in my case), and then taking it away once all PerformancePoint elements are loaded:

<script type="text/javascript">
    var NotifyBrowserOfAsyncUpdateTimeout = false;

    function NotifyBrowserOfAsyncUpdate(elem) {
        window.console && window.console.log('NotifyBrowserOfAsyncUpdate');
        if (NotifyBrowserOfAsyncUpdateTimeout) {
            clearTimeout(NotifyBrowserOfAsyncUpdateTimeout);
        }
        NotifyBrowserOfAsyncUpdateTimeout = setTimeout(function() {
            // do some stuff
            jQuery('#MSO_ContentTable').css('margin-left', '0');
            window.console && window.console.log('NotifyBrowserOfAsyncUpdateTimeout');
        }, 100);
    }
</script>

I added in the setTimeout/clearTimeout with a nominal 100ms duration, but it probably is not necessary; given some very light testing, by the time the first callback hits, PerformancePoint has correctly calculated the width of its components.

Credit: http://stackoverflow.com/questions/3372565/performancepoint-sharepoint-2010-and-jquery

SharePoint 2010 SPSecurity.RunWithElevatedPrivileges HandleAccessDenied

The Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges method enables you to supply a delegate that runs a subset of code in the context of an account with higher privileges than the current user.

Using SharePoint context with an unauthenticated user does not actually elevate privileges:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    // do something with SPContext.Current.Web 
    // fails with a HandleAccessDenied Exception
    // because SPContext is loaded with the site,
    // not within this delegate block.
});

So to get actual elevated privileges, you have to reload the context:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite site = new SPSite(this.Page.Request.Url.ToString()))
    {
        using (SPWeb thisWeb = site.OpenWeb())
        {
            // do something with thisWeb
        }
    }
});

See here for a better explanation: http://www.danlarson.com/spsecurity-runwithelevatedpriveleges/

C# Hash String to Hexadecimal String

Outputs a 40 character hexadecimal hash string using the MD5 algorithm.

private string ToHash(string source)
{
    byte[] bytes;
    char[] c;
    byte b;
	
    //using (SHA1 m = new System.Security.Cryptography.SHA1Managed())
    using (MD5 m = System.Security.Cryptography.MD5.Create())
    {
        bytes = m.ComputeHash(System.Text.UTF8Encoding.UTF8.GetBytes(source));
        c = new char[bytes.Length * 2];
        for (int i = 0; i < bytes.Length; ++i)
        {
            b = ((byte)(bytes[i] >> 4));
            // replace 0x57 with 0x37 to output uppercase
            c[i * 2] = (char)(b > 9 ? b + 0x57 : b + 0x30);
            b = ((byte)(bytes[i] & 0xF));
            // replace 0x57 with 0x37 to output uppercase
            c[i * 2 + 1] = (char)(b > 9 ? b + 0x57 : b + 0x30);
        }
    }
    return new string(c);
}

C# Thread.Sleep(TimeSpan) to Run Every 10 minutes

Based on truncating a DateTime to the second in C# while preserving the “Kind” (Local, UTC, Undefined):

dateTime = dateTime.AddTicks( - (dateTime.Ticks % TimeSpan.TicksPerSecond));

Thread.Sleep until the next 10 minute mark (e.g. Run every 00, 10, 20, 30, 40, 50 minutes):

Thread.Sleep(
    TimeSpan.FromTicks(
        (TimeSpan.TicksPerMinute * 10) - 
        (DateTime.Now.Ticks % (TimeSpan.TicksPerMinute * 10))
    )
);

Credit: http://stackoverflow.com/a/1005222/152852

In the real world, the above code appears to be waking up a fraction of a second too early. So perhaps adding an additional tick might be the way to go:

Thread.Sleep(
    TimeSpan.FromTicks(
        1 + (TimeSpan.TicksPerMinute * 10) - 
        (DateTime.Now.Ticks % (TimeSpan.TicksPerMinute * 10))
    )
);

Less than percent colon – code render blocks in ASP.NET

ASP.NET 4.0 introduces the following code render block syntax:

<%: YourOutput() %>

The search terms “less than percent colon” and “less than percentage colon” did not turn up anything for me in google, but thanks to stackoverflow:

http://stackoverflow.com/questions/2676236/are-and-the-same-thing-as-embbed-code-expression-blocks

<%: is almost the same as <%= except that the output from <%: is automagically html encoded.

Generic List<T> to DataTable using Reflection

The following function takes in a System.Collections.Generic.List<T> and returns a System.Data.DataTable with the properties (via reflection) of T as columns.

public static System.Data.DataTable ListToDataTable<T>(
    System.Collections.Generic.IList<T> elements)
{
    System.Reflection.PropertyInfo[] arrPropInfo = typeof(T).GetProperties();
    System.Data.DataTable dt = new DataTable();
    System.Data.DataRow dr;

    foreach (System.Reflection.PropertyInfo pInfo in arrPropInfo)
    {
        dt.Columns.Add(pInfo.Name, pInfo.PropertyType);
    }
    foreach(object elem in elements)
    {
        dr = dt.NewRow();
        foreach (System.Reflection.PropertyInfo pInfo in arrPropInfo)
        {
            dr[pInfo.Name] = pInfo.GetValue(elem, null);
        }
        dt.Rows.Add(dr);
    }

    return dt;
}

C# Serialization

Xml Serialization and Binary Serialization to a Base64 string

Xml Serialization

Snippit: Take a serializable object and serialize it into an xml string, stored in a System.Text.StringBuilder

System.Xml.Serialization.XmlSerializer serializer = 
    new System.Xml.Serialization.XmlSerializer(typeof(ObjectToSerialize));
System.Text.StringBuilder sb = new System.Text.StringBuilder();
using (System.IO.StringWriter stringWriter = new System.IO.StringWriter(sb))
{
    serializer.Serialize(stringWriter,  myObject);
    stringWriter.Close();
}

Binary Serialization to Base64 string

Snippit: Take a serializable object and serialize it into a Base64 string

byte[] bResult;
System.Runtime.Serialization.IFormatter formatter =
    new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
using (System.IO.MemoryStream s = new System.IO.MemoryStream())
{
    formatter.Serialize(s, myObject);
    bResult = s.ToArray();
    s.Close();
}
string s = Convert.ToBase64String(bResult);

Snippit: Deserialize

ObjectToSerialize myObject = null;
byte[] b = Convert.FromBase64String(str);
System.Runtime.Serialization.IFormatter formatter =
    new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
using (System.IO.MemoryStream ms = System.IO.new MemoryStream(b))
{
    myObject = (ObjectToSerialize)formatter.Deserialize(ms);
    ms.Close();
}