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.
- Navigate to your top-level SharePoint site, e.g.,
- From Site Actions, choose New Site.
- Choose the Blank Site template.
- Enter Lab02 for the name of the site to create.
- The site will be created at
- 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.
- Browse to C:\%Office365TrainingKit%\Assets\Code Snippets
- Select all the files in this directory and copy them to your clipboard by pressing [Ctrl]+[a] and then [Ctrl]+[c].
- 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:
- AbnormalProcessTerminationCount
- CPUExecutionTime
- CriticalExceptionCount
- InvocationCount
- PercentProcessorTime
- ProcessCPUCycles
- ProcessHandleCount
- ProcessIOBytes
- ProcessThreadCount
- ProcessVirtualBytes
- SharePointDatabaseQueryCount
- SharePointDatabaseQueryTime
- UnhandledExceptionCount
- 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.
- Browse to the solution package atC:\ %Office365TrainingKit%\Labs\2.4\Source\After\PowerShell scripts.
- 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.
- Launch Visual Studio 2010 and create a new empty SharePoint project by selecting FileNewProject.
- In the New Project Dialog, select SharePoint2010 in the Installed Templates section and choose Empty SharePoint Project.
- Name the project ResourcePoints.
- 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.
- Right-click the ResourcePointsproject in the Solution Explorer window and select AddNew Item.
- Choose Visual Web Part (Sandboxed).
- Name the web part ResourceConsumer.
- Add a button to the form and create an event handler:
- Switch ResourceConsumer.ascx to the Design View.
- Select Toolbox.
- Double click the Button icon to add a button to the form.
- In the properties window of the button, change the Text property to Consume the Processor.
- Double click the newly-added button to create and bring into focus the click event handler named Button1_Click.
- 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();
}
- 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.
- Run your solution to your local SharePoint instance by pressing F5.
- Navigate to your root web (e.g.
- Click Site Actions and then select Site Settings.
- Clickthe Solutions link in the Galleries section to view the site collection’s solution gallery.
- Note the Resource Usage.
- Next you will add the ResourceConsumer web part to the home page. Navigate to <Your local SharePoint URL>/Lab02 (e.g. ).
- Select Page.
- Select Edit.
- Select Insert.
- Select Web Part.
- Select CustomResourceConsumerTitle.
- Click Add.
- Select PageSave and Close.
- 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.
- 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
- 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.
- Navigate to your root web (e.g. ).
- Click Site Actions and then select Site Settings.
- Clickthe Solutions link in the Galleries section to view the site collection’s solution gallery.
- Note that the resource usage is over 2 points.
- Navigate to <Your local SharePoint URL>/Lab02 (e.g. ).
- 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.
- Browse to the solution package atC:\ %Office365TrainingKit%\Labs\2.4\Source\After\PowerShell scripts
- 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.