Here in the SharePoint development tutorial, we will discuss SharePoint development best practices while developing a custom solution using visual studio 2017/2015 for SharePoint 2016/2013/2010.
There are various best practices needs to be followed for developing a robust custom solution for SharePoint.
SharePoint development best practices
If you are using SharePoint Object model to develop custom solution then there are certain things to remember that are related to performance, extensibility, and scalability.
While writing code it should ensure that any unmanaged resources should be released as soon as they are no longer needed. Though garbage collector automatically releases memory allocated to unused objects, it will release in an unpredictable manner.
The .NET Framework provides the IDisposable interface, which exposes a Dispose method that you should call to explicitly release these unmanaged resources. To invoke the Dispose method, you can use the using keyword, you can use the try/finally, code block or you can explicitly invoke Dispose method.
Example of using keyword:
using (SPSite site = new SPSite(“URL of the Site Collection”))
// Code here
Internally the compiler converts the using block to try/finally block as shown below.
SPSite site = null;
site = new SPSite(“URL of the Site Collection”);
// Code here
if (site != null)
The SPSite and SPWeb types both implement the IDisposable interface, and both allocate unmanaged memory. If you do not correctly release SPSite and SPWeb instances, you will probably experience memory leaks, crashes, and frequent application pool recycles.
But if they are derived from the context object then the framework will take care of releasing resources, you should not release the resources.
For example SPWeb web = new SPSite(SPContext.Current.Web.Url).OpenWeb().
Here the web object is derived from the SPContext, so we should not dispose of that. Here SPWeb object web.Dispose of () automatically called. Read this tutorial Disposing SharePoint Objects.
SPDisposeCheck is a tool that analyzes the custom SharePoint solution that uses the SharePoint object model. If you can using SPSite or SPWeb objects then it is very much necessary to dispose of the objects. Many SharePoint API’s allocate COM based memory that is not released by CLR garbage collection and must be released by calling the Dispose() methods.
As the name suggests SPDisposeCheck will check your assemblies and will let you know the memory leaks according to the Microsoft standards. It is a command line utility and called by the Visual Studio add-in. It takes the path to a managed.DLL or.EXE or the path to a directory containing many managed assemblies. Then it starts analyzing the memory leaks. You can download them from this URL.
After downloading this and during installation you can tick on the checkboxes to integrate SPDisposeCheck to visual studio 2010.
Once the Integration happens it can be found under Tools -> SharePoint Dispose Check in Visual Studio.
If you are using the SPQuery object to retrieve data from a large list then you should give a RowLimit else it performs poorly and will fail on large lists. So give RowLimit between 1 and 2000.
Also, you should limit the result set in the threshold in Microsoft SharePoint Foundation 2010, which is by default 5000 items.
While deploying code using wsp solutions, ensure code is not in debug mode when compiled, it runs slower and potentially can crash/holdup your production environment.
Use the new SharePoint 2010 feature Developer Dashboard for viewing SharePoint page performance.
If you are working with event receiver then Do not instantiate an SPWeb, SPSite, SPList, or SPListItem object within an event receiver. Because it creates sometimes Significant additional roundtrips to the database and Calls to the Update method on these instances can cause subsequent Update calls in other registered event receivers to fails.
For example, follow like below:
So instead of writing like
using (SPSite site = new SPSite(properties.WebUrl))
use like below:
SPWeb web = properties.OpenWeb();
SPListItem item = properties.ListItem;
You may like following SharePoint custom solutions:
- SharePoint 2016/2013 event receiver example using Visual Studio 2017
- Add SharePoint 2016/2013 List/Library Columns to view programmatically using the server object model in Visual Studio 2017
- Create Site Columns programmatically in SharePoint 2016/2013 using Server Object Model in Visual Studio 2017
- How to debug custom web part and timer job in SharePoint 2013 using Visual Studio
- Create a Custom Action Group using Visual Studio 2015 for SharePoint 2016
- How to use asp.net repeater control in SharePoint 2013 using Visual Studio 2013?
- SharePoint 2016 Develop and deploy custom feature using Visual Studio 2015
- SharePoint 2016 How to create list instance and list definition using Visual Studio 2015
- Create a web part page using visual studio 2013 in SharePoint 2013
- Create and Deploy SharePoint 2013 site pages or content pages using Visual Studio 2013
- Steps to create Site column using Visual Studio 2013 in SharePoint 2013
- How to create site definition using Visual Studio 2013 in SharePoint 2013?
- Create List Definition and List Instance in SharePoint 2013 using Visual Studio 2013
- How to add navigation link in site settings navigation page SharePoint 2013 using Visual Studio 2013?
Hope this SharePoint best practices tips will be helpful to develop a robust SharePoint custom solution using visual studio 2010/2015/2017.
SharePoint Online FREE Training
JOIN a FREE SharePoint Video Course (3 Part Video Series)