Hands-On Lab

Resource Points

Lab version: 1.0

Last updated:10/18/2018

Contents

Overview

System Requirements

Setup

Task 1 – Create SharePoint Site

Task 2 – Prepare Code Snippets

Exercise 1 – Resource Points

Task 1 – Shrink the CPU resource allowance

Task 2 – Create a SharePoint solution that consumes resources

Task 3 – Test your solution

Task 4 – Monitor Resource usage

Task 5 – Reset the CPU resource allowance

Summary

Overview

Lab Time: 20 minutes

Lab Folder: C:\%Office365TrainingKit%\Labs\2.4\Source\Before

Lab Overview: In this lab, you will demonstrate how to monitor resource points for a sandboxed solution. You will learn what constitutes a resource point and create a solution that consumes too many resource points and becomes disabled. Lastly, you will learn how to monitor your own resource usage.

System Requirements

You must have the following items to complete this lab:

  • SharePoint 2010
  • Visual Studio 2010
  • SharePoint Designer 2010
  • Visual Studio 2010 SharePoint Power Tools
  • SharePoint Online site

◦Note: You will need administrator access to an SPOsite collection to perform the steps in this lab.

Setup

Note:These setup instructions apply to all the labs in this unit; you don’t need to repeat these steps if you already performed them in the first lab in the unit.

Task 1 – Create SharePoint Site

In this task, you will create a sub site for this lab in your SharePoint on-premise site.

  1. Navigate to your top-level SharePoint site, e.g.,
  2. From Site Actions, choose New Site.
  3. Choose the Blank Site template.
  4. Enter Lab02 for the name of the site to create.
  5. The site will be created at
  6. Click Create to create the site.

Task 2 – Prepare Code Snippets

This lab contains code snippets that you will use to complete the solution. You can either copy the snippets from the lab script itself or install the code snippets so that you can access them directly from Visual Studio.

  1. Browse to C:\%Office365TrainingKit%\Assets\Code Snippets
  2. Select all the files in this directory and copy them to your clipboard by pressing [Ctrl]+[a] and then [Ctrl]+[c].
  3. Browse to ..\Documents\Visual Studio 2010\Code Snippets\Visual C#\My Code SnippetsPress [Ctrl]+[v] to paste the code snippet files into this directory.

Note: To reduce typing, you can right-click where you want to insert source code, select Insert Snippet, select My Code Snippets and then select the entry matching the current exercise step.

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 Solutionsmonitors 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 atC:\ %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 willcreate 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 FileNewProject.
  2. In the New Project Dialog, select SharePoint2010 in the Installed Templates section and choose Empty SharePoint Project.
  3. Name the project ResourcePoints.
  4. In the SharePoint Customization Wizard dialog, in theWhat local site do you want to use for debugging? textbox, enter the URL of the site you created for this session, e.g. . Select the Deploy as a sandboxed solution radio button.
  5. Right-click the ResourcePointsproject in the Solution Explorer window and select AddNew 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:
  9. Switch ResourceConsumer.ascx to the Design View.
  10. Select Toolbox.
  11. Double click the Button icon to add a button to the form.
  12. In the properties window of the button, change the Text property to Consume the Processor.
  13. Double click the newly-added button to create and bring into focus the click event handler named Button1_Click.
  14. Enter the following code within the Button1_Click method:

(Code Snippet 2.4.1)

C#

//Concatenate strings together for 20 seconds

DateTimeendTime = DateTime.Now.AddSeconds(20);

string s = "";

while (DateTime.NowendTime)

{

s = s + DateTime.Now.ToString();

}

  1. 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.
  1. Click Site Actions and then select Site Settings.
  1. Clickthe Solutions link in the Galleries section to view the site collection’s solution gallery.
  1. Note the Resource Usage.
  1. Next you will add the ResourceConsumer web part to the home page. Navigate to <Your local SharePoint URL>/Lab02 (e.g. ).
  2. Select Page.
  3. Select Edit.
  1. Select Insert.
  2. Select Web Part.
  1. Select CustomResourceConsumerTitle.

  1. Click Add.
  2. Select PageSave and Close.
  1. 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 calculatedby the timer jobSolution 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 directoryC:\%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

  1. 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.

  1. Navigate to your root web (e.g. ).
  1. Click Site Actions and then select Site Settings.
  1. Clickthe Solutions link in the Galleries section to view the site collection’s solution gallery.
  1. Note that the resource usage is over 2 points.
  1. Navigate to <Your local SharePoint URL>/Lab02 (e.g. ).
  1. 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

In this task, you will reset the CPU resource allowance for sandboxed solutions back to the SharePoint defaults.

  1. Browse to the solution package atC:\ %Office365TrainingKit%\Labs\2.4\Source\After\PowerShell scripts
  2. Run ResetCPUSandboxAllowance.bat.

Summary

In this lab, you learned techniques to proactively handle SharePoint exceptions in SharePoint Online. Since developers do not have access to SharePoint ULS Logs, developers must be pro-active about catching their exceptions. Youdemonstrated one technique to do this by logging out exceptions to a list that was created by Access Services. In this lab, you also learned how to monitor sandboxed solutions’ resource usage. You did this by creating a Visual Web Part that simulated a processor intensive process. You deployed this solution to SharePoint Online and noted the resource point usage and the disabling of solutions when the quota is exceeded.