33
BEST PRACTICE FOR DEVELOPING SHAREPOINT SOLUTIONS Shakir Majeed Khan www.shakirmajeed.com @meetsmk [email protected]

Sharepoint Saturday India Online best practice for developing share point solution

Embed Size (px)

Citation preview

Page 1: Sharepoint Saturday India Online best practice for developing share point solution

BEST PRACTICE FOR DEVELOPING SHAREPOINT SOLUTIONS

Shakir Majeed Khanwww.shakirmajeed.com@[email protected]

Page 2: Sharepoint Saturday India Online best practice for developing share point solution

MySelf5 year of SharePoint

Blogger, Speaker, Hobbyist photographer

Technical Lead @ Kalsoft

Page 3: Sharepoint Saturday India Online best practice for developing share point solution

Karachi, Pakistan

Page 4: Sharepoint Saturday India Online best practice for developing share point solution

#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

Page 5: Sharepoint Saturday India Online best practice for developing share point solution

Best Practice for Developing SharePoint Solutions

Page 6: Sharepoint Saturday India Online best practice for developing share point solution

Content to Cover

SharePoint Solution

Farm Solutions

Working With Large List

General Consideration

Sand boxed Solution

Event Receivers Timer Jobs

Page 7: Sharepoint Saturday India Online best practice for developing share point solution

SharePoint Solutions

General ConsiderationAvoid unnecessary construction

of ObjectsDisposing objectsObject CachingOptimizing code performance

Page 8: Sharepoint Saturday India Online best practice for developing share point solution

Avoid unnecessary construction of SPWeb/SPSite ObjectsSPWeb

SPSite

SPWebApplication webApplication = SPWebApplication.Lookup(new Uri(System.String);

SPFarm farm = webApplication.Farm;

SPContentDatabase content = webApplication.ContentDatabases[SSystem.Int32];

Page 9: Sharepoint Saturday India Online best practice for developing share point solution

Disposing objects

Page 10: Sharepoint Saturday India Online best practice for developing share point solution

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

Page 11: Sharepoint Saturday India Online best practice for developing share point solution

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

Page 12: Sharepoint Saturday India Online best practice for developing share point solution

Disposing objects (Cont..) Rule of thumb

Never dispose SPContext, SPContext.Site, SPContext.Current.Site, SPContext.Web, and SPContext.Current.Web.

Page 13: Sharepoint Saturday India Online best practice for developing share point solution

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()) {

//

}}

Page 14: Sharepoint Saturday India Online best practice for developing share point solution

Disposing objects (Cont..)

SPSite siteCollection = siteCollections.Add(URL, "DOMAIN\\User", EMAIL);

using (SPSite siteCollection = siteCollections.Add(URL, "DOMAIN\\User", EMAIL)

{

}

Page 15: Sharepoint Saturday India Online best practice for developing share point solution

Disposing objects (Cont..)

SPWeb web = siteCollection.AllWebs.Add(URL);

using (SPWeb web = siteCollection.AllWebs.Add(URL)

{

}

Page 16: Sharepoint Saturday India Online best practice for developing share point solution

Disposing objects (Cont..)Download the SPDispose Check tool

http://code.msdn.microsoft.com/SPDisposeCheck

Page 17: Sharepoint Saturday India Online best practice for developing share point solution

Object Cachingpublic void CacheData() { SPListItemCollection

oListItems; oListItems = (SPListItemCollection)Cache["ListItemCacheName"]; if(oListItems == null) { oListItems = DoQueryToReturnItems(); Cache.Add("ListItemCacheName", oListItems, ..); } }

Page 18: Sharepoint Saturday India Online best practice for developing share point solution

Object Cachingpublic void CacheData() {

DataTable oDataTable; SPListItemCollection oListItems;

lock(_lock) {

oDataTable = (DataTable)Cache["ListItemCacheName"]; if(oDataTable == null) {

oListItems = DoQueryToReturnItems();

oDataTable = oListItems.GetDataTable(); Cache.Add("ListItemCacheName", oDataTable, ..); } } }

Page 19: Sharepoint Saturday India Online best practice for developing share point solution

Optimizing code performance

SPWeb myWeb = SPContext.Current.Web; myWeb.Lists["Tasks"].Title = "List_Title"; myWeb.Lists["Tasks"].Description = "List_Description"; myWeb.Lists["Tasks"].Update();

SPWeb myWeb = SPContext.Current.Web; SPList myList = myWeb.Lists["Tasks"]; myList.Title="List_Title"; myList.Description="List_Description"; myList.Update();

Page 20: Sharepoint Saturday India Online best practice for developing share point solution

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)

Page 21: Sharepoint Saturday India Online best practice for developing share point solution

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

Page 22: Sharepoint Saturday India Online best practice for developing share point solution

Working With Large Lists

Page 23: Sharepoint Saturday India Online best practice for developing share point solution

Working With Large ListsSPWeb.Lists[strDisplayName]

SPWeb.Lists[GUID]SPWeb.GetList(strURL)

Page 24: Sharepoint Saturday India Online best practice for developing share point solution

Working With Large Lists (Cont..)

SPList.ItemsSPList.Items.AddSPList.Items.GetItemById

SPList.GetItems(SPQuery query)SPList.AddItemSPList.GetItemById(int id, string

field1, params string[] fields)

Page 25: Sharepoint Saturday India Online best practice for developing share point solution

Working With Large Lists (Cont..)

SPList.Items.CountSPList.Items[System.Guid]SPList.Items[System.Int32] SPList.Items.GetItemById(Syste

m.Int32)

SPList.ItemCountSPList.GetItemByUniqueId(Syste

m.Guid) SPList.GetItemById(System.Int32)

Page 26: Sharepoint Saturday India Online best practice for developing share point solution

Working With Large Lists (Cont..)

SPFolder.Files.CountSPFolder.Files[System.String]

SPFolder.ItemCountSPFolder.ParentWeb.GetFile(SPU

rlUtility.CombineUrl(SPFolder.Url, System.String)

Page 27: Sharepoint Saturday India Online best practice for developing share point solution

Working With Large Lists (Cont..) Deleting Versions

SPListItemVersion.GetVersionFromID(System.Int32). Delete();

SPFileVersionCollection. DeleteByID(System.Int32);

Page 28: Sharepoint Saturday India Online best practice for developing share point solution

Event Receivers

Page 29: Sharepoint Saturday India Online best practice for developing share point solution

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.

Page 30: Sharepoint Saturday India Online best practice for developing share point solution

Event Receivers (Cont..)Instantiate an SPWeb, SPSite,

SPList, or SPListItemUpdate method

properties.OpenWeb()properties.ListItem

Page 31: Sharepoint Saturday India Online best practice for developing share point solution

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,SPContext.Current.Web

Page 32: Sharepoint Saturday India Online best practice for developing share point solution

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

Page 33: Sharepoint Saturday India Online best practice for developing share point solution

Thank You

Shakir Majeed Khanwww.shakirmajeed.com@[email protected]