4. #Best Practice Quotes
“We cannot control the winds, but we can adjust the
sails.”
Anonymous
“It's not necessarily the amount of time you spend at
practice that counts; it's what you put into the practice.”
Eric Lindros
“My secret is practice”
David Beckham
10. Disposing objects (Cont..)
Symptoms
Application pool recycle frequently,
especially under heavy loads
System perform poorly, especially under
heavy loads
System crash or do users experience
unexpected errors such as timeouts or
page-not-available errors, especially under
heavy loads
11. Disposing objects (Cont..)
Why Dispose?
SPSite class and SPWeb class objects, are
created as managed objects
Each instance of SPSite and SPWeb
contains a reference to an SPRequest
object that, in turn, contains a reference to
an unmanaged COM object
12. Disposing objects (Cont..)
Rule of thumb
Never dispose
SPContext,
SPContext.Site, SPContext.Current.
Site, SPContext.Web, and
SPContext.Current.Web.
13. Disposing objects (Cont..)
using (SPWeb web = new
SPSite(SPContext.Current.Web.
Url).OpenWeb()) { //
}
using (SPSite siteCollection = new
SPSite(SPContext.Current.Web.Url)) {
using (SPWeb web =
siteCollection.OpenWeb()) {
//
}}
20. Farm Solutions
Target farm level solutions to the specific
web application instead of deploying to
all web applications.
Try to deploy all the resource files
(CSS, JPG) from within the Solution
(Applicable to Sandboxed solution as
well)
21. Sandboxed Solutions
Plan which servers will run the
sandboxed solutions service.
Plan which site collections will be able
to run sandboxed solutions.
Design your Sand Boxed solution as per
the Site collection quota
29. Event Receivers
Always use Event Receivers to execute
the code immediately. Workflows can
perform similar function but it will run as
a timer job which may delay the code
execution.
30. Event Receivers (Cont..)
Instantiate an SPWeb, SPSite,
SPList, or SPListItem
Update method
properties.OpenWeb()
properties.ListItem
31. Timer Jobs
Always run the timer job in off hours
Always perform take out the timer job
when deactivating the respective
feature
Don’t use
SPContext,SPContext.Current,SPCo
ntext.Current.Web
32. General Considerations
Use proper feature name and
description.
Use the feature as it require by design.
Make sure solutions have proper
consistent naming convention.
Use the correct older version of WSPs
to retract the solution before deploying
or upgrading the custom code
Hello every one, good after noon every one, Welcome to SharePoint Saturday India Online and to my session. Let me assure you the next hour will be quite exciting for us.
I am participating in this event from across the border. I am speaking from Karachi, Pakistan
An SPWeb or SPSite object can occupy a lot of memory. Avoid constructing objects of these types simply to get a reference to a parent object. Instead, to get a reference to a web application, use the static SPWebApplication.Lookup(Uri) method, and pass it a Uri object that is created with the URI of the web application. You can then get a reference to the farm by using the Farm property of the web application object. (You can get a reference to a remote farm by using the static Open(String) method.) The ContentDatabases property of the web application object contains a collection of the content databases in the web application. You can get a reference to a particular content database through this property if you know its index in the collection. For more information, see the reference topic for the SPContentDatabaseCollection class.
Each instance of SPSite and SPWeb contains a reference to an SPRequest object that, in turn, contains a reference to an unmanaged COM object that handles communications with the database server. SharePoint Foundation monitors the number of SPRequest objects that exist in each specific thread and in parallel threads, and adds useful entries to the logs.Whenever the number of SPRequest objects exceeds threshold (Default is 8), an entry appears in the ULS logs:
Caching is a good way to improve system performance. However, you must weigh the benefits of caching against the need for thread safety, because some SharePoint objects are not thread safe and caching causes them to perform in unexpected ways.Caching SharePoint Objects That Are Not Thread SafeYou might try to increase performance and memory usage by caching SPListItemCollection objects that are returned from queries. In general, this is a good practice; however, the SPListItemCollection object contains an embedded SPWeb object that is not thread safe and should not be cached.
The use of the cache in the preceding example is functionally correct; however, because the ASP.NET cache object is thread safe, it introduces potential performance problems. (For more information about ASP.NET caching, see the Cache class.) If the query in the preceding example takes 10 seconds to complete, many users could try to access that page simultaneously during that amount of time. In this case, all of the users would run the same query, which would attempt to update the same cache object. If that same query runs 10, 50, or 100 times, with multiple threads trying to update the same object at the same time—especially on multiprocess, hyperthreaded computers—performance problems would become especially severe.
Avoid creating and destroying objects unnecessarily in code, as this may require that extra queries be made against the database and may even involve code that is incorrect. Tasks list must be instantiated each time the indexer is used to set properties and the method for updating is called. This is not a recommended practice.Tasks list object only once and assigns it to the myList variable in order to set properties and call the method.
Target farm level solutions to the specific web application instead of deploying to all web applications unless you are deploying new feature to all the applications in the farm. This way custom feature for the specific web application won’t available on site collection features page or site features page on all the web applications in the farm.Try to deploy all the resource files (CSS, JPG) from within the Solution (Applicable to Sandboxed solution as well)
I wont go into the details of the sandboxed solution here, but these are the general rule of thumbPlan which servers will run the sandboxed solutions service.Plan which site collections will be able to run sandboxed solutions.Design your Sand Boxed solution as per the Site collection quota
Using the GUID is preferable because it is unique, permanent, and requires only a single database lookup. The display name indexer retrieves the names of all the lists in the site and then does a string comparison with them. If you have a list URL instead of a GUID, you can use the GetList method in SPWeb to look up the list's GUID in the content database before retrieving the list.
When you delete multiple versions of a list item, use the DeleteByID() method; do not use the Delete() method. You will experience performance problems if you delete each SPListItemVersion object from an SPListItemVersionCollection object. The recommended practice is to create an array that contains the ID properties of each version and then delete each version by using the SPFileVersionCollection.DeleteByID method.
Do not instantiate an SPWeb, SPSite, SPList, or SPListItem object within an event receiver. Event receivers that instantiate these objects instead of using the instances passed via the event properties can cause the following issues: Significant additional roundtrips to the database (one write operation can result in up to five additional roundtrips in each event receiver). Calls to the Update method on these instances can cause subsequent Update calls in other registered event receivers to fail.
Make sure solutions have proper feature name and description. Feature name and description would be visible on the site collection and site features page.Make sure solutions have proper consistent naming conventionDeployment discipline – During the deployment and retraction process while updating or upgrading custom solutions, please use the correct older version of WSPs to retract the solution before deploying or upgrading the custom code with newer version of WSPs. This would ensure activating or deactivating features reference correct version of WSPs.