Saturday, January 18, 2014

Calculate the page load time for both cached and un-cached pages using System.Net.WebClient

Recently I was trying to create a console application for calculating the page load time for sharePoint site pages. I need both the cached and un-cached page load time. Here is the simplest way, using System.Net.WebClient and System.Diagnostics.stopwatch to calculate the page load time. 
Code Sample
class Program
{
    static void Main(string[] args)
    {

        string urls = "https://www.google.com/";
        using (WebClient client = new WebClient())
        {
            int i = 0;
            for (i = 0; i < 2; i++)
            {
        //Get uncached data for the first time and next time get data from cache
                if (i == 0)
                    client.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.BypassCache);
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();// start System.Diagnostics.Stopwatch before downloading url
                client.Credentials = new System.Net.NetworkCredential("Username", "Password", "Domain");
                String result = client.DownloadString(urls);
                stopwatch.Stop();
                Console.WriteLine(String.Format("{0} = {1}", urls, stopwatch.Elapsed.TotalSeconds));

            }

        }
    }
}

Wednesday, January 1, 2014

Get all sub sites inside a particular SharePoint Site Collection using built-in web services

Recently I came across a scenario where I had to create a console application for getting all sub sites  inside a particular SharePoint site collections using SharePoint web services. I used GetAllSubWebCollection() method in Webs.asmx web service. Well following are the steps:-

Steps:
  1. Add "{your site coll url}/_vti_bin/Webs.asmx" as web reference.
  2. Use the GetAllSubWebCollection() method in Webs.asmx webservice to get all sub sites and site collections.
  3. Point to remember - User should have atleast "Read" permission to all the sites.
Code Snippet
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         //webs object from the service reference named "WebRef"
  6.         WebRef.Webs myweb = new WebRef.Webs();
  7.         
  8.         //Enter you network credentials
  9.         myweb.Credentials = new System.Net.NetworkCredential("username", "password", "domain");
  10.  
  11.         //Replace {your site coll url} with your site collection url
  12.         myweb.Url = "{your site coll url}/_vti_bin/Webs.asmx";
  13.         
  14.         //Get a collection of all the subsites inside the site collection
  15.         XmlNode objXmlNode = myweb.GetAllSubWebCollection();
  16.         XmlNodeList objNodelist = objXmlNode.ChildNodes;
  17.         foreach (XmlNode objNode in objNodelist)
  18.         {
  19.             Console.WriteLine(objNode.Attributes["Url"].Value.ToString());
  20.         }
  21.         Console.ReadLine();
  22.     }
  23. }