This document provides guidance on building scalable, high-performance SharePoint applications. It discusses considerations for working with SharePoint lists, accessing list data efficiently from code, updating lists in batches, and designing high-performing web parts. The document also provides links to additional resources and demonstrates monitoring and optimizing SharePoint resource usage.
UiPath Community: Communication Mining from Zero to Hero
Scaling SharePoint Apps for High Performance
1. 907: BUILDING SCALABLE, HIGH-
PERFORMANCE SHAREPOINT
APPLICATIONS
SPTechCon
2009-06-24, dynaTrace software Inc
Andreas Grabner, andreas.grabner@dynatrace.com
Technology Strategist
dynaTrace
2. About me
• Andreas Grabner
• Technology Strategist
• dynaTrace Software (http://www.dynaTrace.com)
• Blog: http://blog.dynaTrace.com
• Why Performance is such a big topic for me?
dynaTrace 2
3. Agenda
• While the development of SharePoint-based services is relatively
easy, making them perform and scale can be a real challenge. This
class will show you code in the SharePoint Component Model, so
you can learn about what the framework is doing under the hood
when it is used by a WebPart or an external application. This insight
is vital in order to build high-performing and scalable applications
based on SharePoint
• LOTS OF DEMOS!!
dynaTrace 3
4. Agenda
• SharePoint Object Model
• Considerations when working with lists
• Different ways to access list content
• Batch updating lists
• Memory Considerations & Native Resources
• WebParts
• Design Guidelines
• Performance Considerations
• How to debug/analyze/profile
• Tips & Tricks
dynaTrace 4
5. Working with SharePoint Lists (1)
• Do not treat SharePoint Lists as database tables
• Use database tables for transient or transactional data.
• The 2000 Items per List Myth
• What you read on blogs/articles/...
• Consider the restriction of a maximum of 2000 items per list container in document
libraries and lists
• Create containers (folders) in your list to overcome the 2000 item limit
• What I think
• > 2000 is not a problem at all
• If you only request those items in a list that the user needs in the particular use case
• Use Row Limits, Paging, queries, ... to selectively retrieve list items
• Maximum number of items supported in a list with recursive containers
(folders) is 5 million items
dynaTrace 5
6. Working with SharePoint Lists (2)
• Consider caching the contents of a list to a DataTable or DataSet if
the list will be queried multiple times in your application
• Consider using PortalSiteMapProvider which implements a result
cache based on SPQuery‘s
• Use Views to limit the number of columns that are retrieved
dynaTrace 6
7. Analyze List Usage Behavior
• Do not do pre-mature optimization
• Analyze Usage Patterns of Lists and Views
• Define Index Columns and modify views to improve query performance
Analyze usage and performance of all lists in SharePoint Analyze usage and performance of all views in SharePoint
dynaTrace 7
8. Access SharePoint Lists from Code (1)
• Getting Item Count of a List
DO NOT
int noOfItems = SPContext.Current.List.Items.Count;
ALL List Items are retrieved from the Database
DO
int noOfItems = SPContext.Current.List.ItemCount;
Item Count is kept redundant in the AllUserData table and also kept in memory
dynaTrace 8
9. Access SharePoint Lists from Code (2)
• Iterating through List Items – THE WRONG WAY
DO NOT
for (int itemIx=0;itemIx< SPContext.Current.List.Items.Count;itemIx++) {
SPListItem listItem = SPContext.Current.List.Items[itemIx];
// do something ...
}
Every access to Count and Items Property queries the whole SharePoint list
We end up with 202 SQL Executions with a total exec time of > 1s
dynaTrace 9
10. Access SharePoint Lists from Code (3)
• Iterating through List Items – THE RIGHT WAY
DO
SPListItemCollection items = SPContext.Current.List.Items;
foreach (SPListItem listItem in items) {
// do something ...
}
Only first access to the collection queries the data
dynaTrace 10
11. Access SharePoint Lists from Code (4)
• Limit Rows by using SPQuery
• Accessing the SPList object always requests ALL items in the list
• Use SPQuery and the RowLimit property to only query a certain amount of
elements
DO
SPQuery query = new SPQuery();
query.RowLimit = 100;
SPListItemCollection items = SPContext.Current.List.GetItems(query);
for (int itemIx=0;itemIx<items.Count;itemIx++) {
SPListItem listItem = items[itemIx];
// do something ...
}
SPQuery properties are taken into the generated SQL Statment. Only the first X rows are selected
dynaTrace 11
12. Access SharePoint Lists from Code (5)
• Limit Columns by using a View or SPQuery.ViewFields
• Accessing SPList always returns ALL Fields
• ONLY request the columns that you really need
DO
SPQuery query = new SPQuery(SPContext.Current.ViewContext.View);
or DO
SPQuery query = new SPQuery();
query.ViewFields = "<FieldRef Name='ID'/><FieldRef Name=‘Text Field'/><FieldRef Name=‘XYZ'/>";
SELECT clause when accessing SPList
SELECT clause when using a View or
ViewFields
dynaTrace 12
13. Access SharePoint Lists from Code (6)
• Pagine through SPQuery Results
• Process query results in batches or
• Use this feature when implementing custom paging
DO
SPQuery query = new SPQuery();
query.RowLimit = 10; // Thats our page size
do
{
SPListItemCollection items = SPContext.Current.List.GetItems(query);
// do something with the first batch of items...
query.ListItemCollectionPosition = items.ListItemCollectionPosition;
} while (query.ListItemCollectionPosition != null)
Individual SQL Statements are executed for each page of data
ListItemCollectionPosition is used in WHERE clause
dynaTrace 13
14. Updating Data in SharePoint Lists (1)
• Use Batch Updates when updating multiple items at once
DO NOT
for (int itemIx=0;itemIx<newItems;itemIx++) {
SPListItem newItem = items.Add();
// fill the individual fields
newItem.Update();
}
Every Update is done separately and requires a roundtrip to the DB
dynaTrace 14
15. Updating Data in SharePoint Lists (2)
• Construct a CAML Update Query and Execute it via SPWeb
DO
StringBuilder query = new StringBuilder();
for (int itemIx=0;itemIx<newItems;itemIx++) {
query.AppendFormat("<Method ID=”{0}”>" +
"<SetList>{1}</SetList>" +
"<SetVar Name=“ID”>New</SetVar>" +
"<SetVar Name=”Cmd”>Save</SetVar>" +
"<SetVar Name=”{3}Title”>{2}</SetVar>" +
"</Method>“, itemIx, listGuid, someValue, "urn:schemas-microsoft-com:office:office#");
}
SPContext.Current.Web.ProcessBatchData("<?xml version="1.0" encoding="UTF-8"?>" +
"<ows:Batch OnError="Return">{0}</ows:Batch>", query.ToString())
CAML Query is processed in Batch by ProcessBatchData Without Batch
Almost 2 seconds difference for
inserting 100 items
dynaTrace 15
16. Updating Data in SharePoint Lists (3)
• Use the Web Service API as an alternative
• http://msdn.microsoft.com/en-us/library/lists.lists.updatelistitems.aspx
DO
StringBuilder query = new StringBuilder();
for (int itemIx=0;itemIx<newItems;itemIx++) {
query.AppendFormat("<Method ID=”{0}”>" +
"<SetList>{1}</SetList>" +
"<SetVar Name=“ID”>New</SetVar>" +
"<SetVar Name=”Cmd”>Save</SetVar>" +
"<SetVar Name=”{3}Title”>{2}</SetVar>" +
"</Method>“, i, listGuid, someValue, "urn:schemas-microsoft-com:office:office#");
}
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
System.Xml.XmlElement elBatch = xmlDoc.CreateElement("Batch");
elBatch.SetAttribute("OnError", "Return");
elBatch.InnerXml = methods.ToString();
localhost.Lists listService = new SPConsole.localhost.Lists();
listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
listService.UpdateListItems(listname, elBatch);
dynaTrace 16
17. Summary on SharePoint Object Model
• Count List Items
• SPList.ItemCount instead of SPListItemCollection.Count
• Iterating through SPList
• Store SPListItemCollection in variable instead of accessing List property in loop
• Limit the number of Items retrieved by using SPQuery and RowLimit
• Limit Columns
• Use a View or SPQuery to limit the number of columns and rows that will be retrieved
• Paging through data
• Make use of SPQuery ListItemCollectionPosition feature to page through data
• Use an appropriate RowLimit value to define the page size
• List Updates
• Do batch updates via WebService Lists.UpdateListItems or SPWeb.ProcessBatchData
• List Item Collections
• Store myList.Items in a SPListItemCollection variable when accessed multiple times
dynaTrace 17
18. Interesting Links on SharePoint Lists
• SharePoint List Performance
• http://blog.solanite.com/keith/Lists/Posts/Post.aspx?ID=15
• http://blog.thekid.me.uk/archive/2007/02/24/deleting-a-considerable-
number-of-items-from-a-list-in-sharepoint.aspx
• http://blog.solanite.com/keith/Lists/Posts/Post.aspx?ID=15
• Link Collection about Performance
• http://blogs.msdn.com/joelo/archive/2007/07/09/capacity-planning-key-
links-and-info.aspx
• Information about Row Limit and Paging
• http://msdn.microsoft.com/en-us/library/cc404818.aspx
dynaTrace 18
19. SharePoint Object Model
DEMO
• Whats going on „under the hood“ when using the SharePoint Object
Model?
• How to improve SharePoint Data Access?
dynaTrace 19
20. INEFFICIENT use of RESOURCES
• SharePoint Object Model
• SPSite and SPWeb hold references to native COM objects
• Release SPSite & SPWeb in order to free native resources
• Querying too much data results in high memory usage
• Reference
• SPDisposeCheck tool
http://blogs.msdn.com/sharepoint/archive/2008/11/12/announcing-
spdisposecheck-tool-for-sharepoint-developers.aspx
• http://msdn.microsoft.com/en-us/library/bb687949.aspx
• http://msdn2.microsoft.com/en-
us/library/aa973248.aspx#sharepointobjmodel_otherobjectsthatrequire-
disposal
dynaTrace 20
22. Data is REQUESTED in an INEFFICIENT way
DEMO
• How to identify a SPSite/SPWeb Resource Leak?
• How to identify resource intensive WebParts?
• How to monitor SharePoint Memory Issues down to the Object Model‘s
Data Access classes?
dynaTrace 22
23. Web Parts Design Guidelines
• Design Web Parts to perform only a single function in order to improve reuse
• Design Web Parts to be configurable or customizable by users
• Include a Web Part Manager in custom master pages that will be used by
Web Part pages
• Consider using Web Part verbs to allow users to perform discrete actions
• Consider categorizing your properties to distinguish them from Web Part
properties
• Dispose properly of any SharePoint objects and unmanaged resources
that you create in your Web Parts
• Many SharePoint Objects hold references to unmanaged objects
dynaTrace 23
24. WebPart Troubleshooting
• Attach to w3wp.exe process
• Use Process Explorer to find correct w3wp (-ap parameter)
• Understand ASP.NET Page Execution LifeCycle
• ASP.NET is the underlying technology
• Understand where your custom code fits in
• Be careful with VIEWSTATE
• Easy to use but comes with many side-effects
• http://www.sitepoint.com/article/aspnet-performance-tips/2/
• Memory Management
• Be careful with allocating too many small short living objects
• Make sure to free references
• Resource Management
• Dispose/Release objects
• Hold on to resources only as long as you need it
dynaTrace 24
25. Tips & Tricks
• Use RenderContents or CreateChildControl vs. Render
• http://www.andrewconnell.com/blog/archive/2008/02/18/Understanding-how-Web-
Parts-are-rendered-why-to-never-use.aspx
• Turn on IIS-Compression
• http://planetmoss.blogspot.com/2007/06/dont-forget-iis-compression-
colleague.html
• http://www.sitepoint.com/article/aspnet-performance-tips/3/
• BLOB Caching
• http://office.microsoft.com/en-us/sharepointserver/HA101762841033.aspx
• Delay loading core.js
• http://support.microsoft.com/kb/933823
• General ASP.NET Performance Tuning
• http://www.sitepoint.com/article/aspnet-performance-tips/
dynaTrace 25
26. Tips & Tricks
• Pre-Create Personal Site
• UserProfile.CreatePersonalSite()
• Can take several seconds per user
• Do it up-front to avoid heavy load when releasing new SharePoint
installation
using (SPSite spSite = new SPSite(@“http://server“))
{
ServerContext siteContext = ServerContext.GetContext(spSite);
UserProfileManager pmManager = new UserProfileManager(siteContext);
UserProfile spUser = pmManager.GetUserProfile(„domainusername“);
spUser.CreatePersonalSite();
}
dynaTrace 26
27. References & Contact
• MS SharePoint Team Blog
• http://blogs.msdn.com/sharepoint/default.aspx
• http://blogs.msdn.com/sharepoint/archive/2006/02/27/539689.aspx
• Contact me for follow up
• Andreas Grabner
• Mail: andreas.grabner@dynatrace.com
• Blog: http://blog.dynatrace.com
• Web: http://www.dynatrace.com
dynaTrace 27