Friday, July 20, 2012

Cycle through all internets quicker way in SharePoint 2010

We are studying so many principles each day and implementing/writing lot of value. But, we might not a bit of your energy and energy to create the value more effective way or more cleanser way. So, this publish is again relevant to how to create value effective in SharePoint 2010.

So far, till SharePoint 2007 to loop through all the webs in site collection we use the code:
  1. using(SPSite site = new SPSite("http://sitecollectionurl")  
  2. {  
  3. foreach(SPWeb web in site.AllWebs)  
  4. {  
  5. //Some code here  
  6. web.Dispose();  
  7. }  
  8. }  
The above code is not at all wrong. 

Case 1:
If you need the web item only for studying the general details of the website like name, url, id etc… then it is a very costly function. For this, in SharePoint 2010 there is a workaround and that will fill outcomes 10 periods quicker than above value. Below is the effective code:

  1. using(SPSite site = new SPSite("http://sitecollectionurl")  
  2. {  
  3. foreach(SPWebInfo webInfo in site.AllWebs.WebsInfo)  
  4. {  
  5. //Code here to read web information   
  6. }  
  7. }  

This way you only reading the web information object instead of complete Web object. You can take a look more about WebInfo class  here in

Case 2: 
If you want to read the properties you needed then there is a more better way than simply loop through AllWebs property in for each. The complete explaination is here This is a very good post and very very faster way to read the properties in all webs. 

No comments:

Post a Comment