SIN Cycle handlers require SP SIN version 1.2.

The SIN Cycle is the name of the request cycle that SP SIN goes through while processing a page request. This cycle has thee main stages:

  1. Loading and preparing context
  2. Loading resource items and evaluating whether they should be added
  3. Processing resource items

A SIN Cycle Handler is a way for developers to hook into each of these stages to interact with the SP SIN request cycle.

At each stage, there is a Before event and an After event and a corresponding method to override if you want to hook into that particular event. In addition, there is a hook that triggers before and after the loading of the resources, and one hook at the very end, after all other processing has occurred. 

SINCycleEventMethods

The event firing order is:

  1. BeforeContextLoad
  2. AfterContextLoad
  3. BeforeResourceItemsLoad
  4. AfterResourceItemsLoad
  5. BeforeResourceItemShouldLoadEvaluation
  6. AfterResourceItemShouldLoadEvaluation
  7. BeforeResourceItemGetResourceStringEvaluation
  8. AfterResourceItemGetResourceStringEvaluation
  9. AfterProcessingComplete

Note that events 5-8 evaluate for each resource item in the resource list.

To create a SIN Cycle handler, you need to

  • build a WSP to deploy a feature
  • decorate that feature with three properties
  • build an assembly with a class that inherits from SINCycleReceiver or one of its children

The three properties with which you need to decorate your feature are:

SPSIN_SINCycleReceiver_Assembly
Value should be full strong name of assembly

SPSIN_SINCycleReceiver_Class
Value should be full class name including namespace of the class inheriting from SINCycleReceiver

SPSIN_SINCycleReceiver_Sequence
Value should be an integer. A higher number means processing happens later if there are multiple Sin Cycle handlers installed.

Here’s an example feature.xml taken from the sample project available on the release page for version 1.2.

<?xml version="1.0" encoding="utf-8"?>
<Feature  Id="4fae4925-fe10-45bd-80c5-a20c32cc4820"
          Title="SINCycleSample"
          Description="Description for SINCycleSample"
          Version="1.0.0.0"
          Hidden="FALSE"
          Scope="Web"
          DefaultResourceFile="core"
          xmlns="
http://schemas.microsoft.com/sharepoint/">
  <ElementManifests>
    <ElementManifest Location="elements.xml"/>   
  </ElementManifests>
  <Properties>
    <Property Key="SPSIN_SINCycleReceiver_Assembly"
              Value="SINCycleSample, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c1a97bf69466f2c6"/>
    <Property Key="SPSIN_SINCycleReceiver_Class"
              Value="SINCycleSample.SINCycleSample"/>
    <Property Key="SPSIN_SINCycleReceiver_Sequence"
              Value="10000"/>
  </Properties>
</Feature>

Your class should, as a best practice, only override the methods you intend to use. The parent class SINCycleReceiver will handle the default behavior for any methods you do not override.

Here is an example method override that will prepend all resource item outputs with the title of the resource item inside an HTML comment.

public override string BeforeResourceItemGetResourceStringEvaluation(SINCycleContext context, SPListItem resourceItem)
{
    string output = string.Format("<!-- {0} -->", context.CurrentResourceItem.Title);

    return output;
}

This may result in output like this:

<!-- jQuery 1.8.2 CDN (cdnjs.com) All Pages -->
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<!-- SPServices 0.7.2 CDN (cdnjs.com) All Pages -->
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/0.7.2/jquery.SPServices-0.7.2.min.js"></script>

The receiver objects survive through the entire SIN Cycle. You can utilize this for example to track the time it takes to run through the SIN Cycle. Here’s a class that will accomplish this, outputting the result in an HTML comment:

public class SINCycleSample : SINCycleReceiver
{
    DateTime start = new DateTime();
    DateTime end = new DateTime();

    public override void BeforeContextLoad(SINCycleContext context)
    {
        start = DateTime.Now;
        base.BeforeContextLoad(context);
    }

    public override string AfterProcessingComplete(SINCycleContext context, string Output)
    {
        end = DateTime.Now;

        TimeSpan duration = end-start;

        Output += string.Format(@"<!--
SP SIN stats:
Start: {0}
End: {1}
Duration: {2} seconds
-->", start.ToLongTimeString(), end.ToLongTimeString(), duration.TotalSeconds);

        return base.AfterProcessingComplete(context, Output);
    }
   
}

The result of this SIN Cycle handler may be something like this:

<!--
SP SIN stats:
Start: 7:00:36 AM
End: 7:00:36 AM
Duration: 0.0624 seconds
-->

Last edited Oct 16, 2012 at 6:21 AM by furuknap, version 2

Comments

No comments yet.