Exercise 1 – Resource Points

Sandboxed Solutions were created to prevent a SharePoint solution from affecting the rest of the SharePoint site collection. SharePoint Online will throttle and even disable a solution if that solution consumes too many resources. Sandboxed Solutions monitors the resource usage of each Sandboxed Solution and assigns resource points based upon its behavior. SharePoint 2010 and SharePoint Online track metrics that contribute to the quota of a given solution. These are:

  1. AbnormalProcessTerminationCount
  2. CPUExecutionTime
  3. CriticalExceptionCount
  4. InvocationCount
  5. PercentProcessorTime
  6. ProcessCPUCycles
  7. ProcessHandleCount
  8. ProcessIOBytes
  9. ProcessThreadCount
  10. ProcessVirtualBytes
  11. SharePointDatabaseQueryCount
  12. SharePointDatabaseQueryTime
  13. UnhandledExceptionCount
  14. UnresponsiveprocessCount

In this lab, you will be building a solution that consumes the processor for 20 seconds at a time to simulate a resource wasting solution. Normally, this task will only contribute a small amount to the resource quota since 3600 seconds (1 hour) of processor usage constitute 1 resource point by default. For the sake of this lab, you will be modifying these settings in order to demonstrate what happens when a sandboxed solution is disabled by resource usage. Changing these settings cannot be changed by users in SharePoint Online at this time, so this entire exercise will be done on-premise.

Task 1 – Shrink the CPU resource allowance

In this task, you will reduce the CPU resource allowance for sandboxed solutions. By default, any sustained CPU utilization under 100 milliseconds is not counted against your total CPU utilization since this is considered normal operation. Additionally, in order to consume 1 resource point, your sandboxed solution must use 200 seconds of CPU time. You will reduce both of these values using a PowerShell script. This script will remove the 100 millisecond minimum threshold and make one second of CPU utilization count as one resource point. The script will also lower the resource point threshold for sandbox solutions from 300 points per day to 2 points per day.

Note:
This script will change every web application’s resource quota to 2 points per day. You will be resetting them back to the default later in this lab. If you previously customized the quotas in other web applications, you will need to change them back after this lab.
  1. Browse to the solution package at C:\ %Office365TrainingKit%\Labs\2.4\Source\After\PowerShell scripts.
  2. Run ShrinkCPUSandboxAllowance.bat.

Task 2 – Create a SharePoint solution that consumes resources

In this task, you will create a visual web part that consumes processor resources when a button is clicked. This will create a corresponding resource point increase in the solution gallery.

  1. Launch Visual Studio 2010 and create a new empty SharePoint project by selecting File >> New >> Project.
  2. In the New Project Dialog, select SharePoint >> 2010 in the Installed Templates section and choose Empty SharePoint Project.
  3. Name the project ResourcePoints.
  4. In the SharePoint Customization Wizard dialog, in the What local site do you want to use for debugging? textbox, enter the URL of the site you created for this session, e.g. https://intranet.contoso.com/Lab02. Select the Deploy as a sandboxed solution radio button.
  5. Right-click the ResourcePoints project in the Solution Explorer window and select Add >> New Item.
  6. Choose Visual Web Part (Sandboxed).
  7. Name the web part ResourceConsumer.
  8. Add a button to the form and create an event handler:
    1. Switch ResourceConsumer.ascx to the Design View.
    2. Select Toolbox.
    3. Double click the Button icon to add a button to the form.
    4. In the properties window of the button, change the Text property to Consume the Processor.
    5. Double click the newly-added button to create and bring into focus the click event handler named Button1_Click.
  9. Enter the following code within the Button1_Click method:

    (Code Snippet 2.4.1)

    C#

    //Concatenate strings together for 20 seconds DateTime endTime = DateTime.Now.AddSeconds(20); string s = ""; while (DateTime.Now < endTime) { s = s + DateTime.Now.ToString(); }
  10. Build the solution to confirm there are no compile-time errors.

Task 3 – Test your solution

In this task, you will deploy and test your solution to SharePoint installed on premise. You will first see next to 0 resource usage, then consume the processor by clicking the button Consume the processor.

  1. Run your solution to your local SharePoint instance by pressing F5.
  2. Navigate to your root web (e.g. https://intranet.contoso.com/).
  3. Click Site Actions and then select Site Settings.

  4. Click the Solutions link in the Galleries section to view the site collection’s solution gallery.

  5. Note the Resource Usage.

  6. Next you will add the ResourceConsumer web part to the home page. Navigate to <Your local SharePoint URL>/Lab02 (e.g. https://intranet.contoso.com/lab02).
  7. Select Page.
  8. Select Edit.

  9. Select Insert.
  10. Select Web Part.

  11. Select Custom >> ResourceConsumer Title.

  12. Click Add.
  13. Select Page >> Save and Close.

  14. Click the button Consume the Processor. The web page will return after 20 seconds.

Task 4 – Monitor Resource usage

In this task, you will monitor resource usage of your solution. The amount of resource points a solution uses are calculated by the timer job Solution Resource Usage Update. This timer job is set to run every 15 minutes by default. You will use a PowerShell script to run it immediately. Resource use will be updated several minutes after the timer job completes. You will then demonstrate that the sandboxed solution is disabled.

  1. Browse to the directory C:\%Office365TrainingKit%\Labs\2.4\Source\After\PowerShell scripts
    Note:
    You can also run this job through the Central Administration by navigating to Central Administration >> Monitoring >> Review Job definitions
  2. Run RunSandboxCalculationTimerJob.bat.
    Note:
    Even after the timer job finishes, it may take several minutes for the resource usage to be reflected in the solution gallery.
  3. Navigate to your root web (e.g. https://intranet.contoso.com).
  4. Click Site Actions and then select Site Settings.

  5. Click the Solutions link in the Galleries section to view the site collection’s solution gallery.

  6. Note that the resource usage is over 2 points.
  7. Navigate to <Your local SharePoint URL>/Lab02 (e.g. https://intranet.contoso.com/lab02).
  8. Since you temporarily set the daily resource usage quota to 2, the Resource Consumer web part has been disabled because it exceeded its daily resource usage quota.

Task 5 – Reset the CPU resource allowance

  1. In this task, you will reset the CPU resource allowance for sandboxed solutions back to the SharePoint defaults.
  2. Browse to the solution package at C:\ %Office365TrainingKit%\Labs\2.4\Source\After\PowerShell scripts
  3. Run ResetCPUSandboxAllowance.bat.