Wednesday, October 19, 2011

Client Object Model Use Nesting Include To Improve Performance

In Client Object Model if we want to call LoadQuery method, we can specify multiple levels of properties to load. This will help in reducing the number of round trips to the server to retrieve the required Data. Now below example retrieve all lists and associated fields. This will print on console if list or field is hidden.


Download Working Example



 static void Main(string[] args)
        {
            ClientContext _context = new ClientContext("http://anmol-pc");
            
            IEnumerable<List> allLists = _context.LoadQuery(
                _context.Web.Lists.Include(
                    myList => myList.Title,
                    myList => myList.Hidden,
                    myList => myList.Fields.Include(
                        myField => myField.Title,
                        myField => myField.Hidden)));


            _context.ExecuteQuery();


            foreach (List myList in allLists)
            {
                Console.WriteLine("{0}List: {1}",
                    myList.Hidden ? "Hidden " : "", myList.Title);
                foreach (Field myField in myList.Fields)
                    Console.WriteLine("  {0}Field: {1}",
                        myField.Hidden ? "Hidden " : "",
                        myField.Title);
            }


            Console.ReadLine();
        }

3 comments:

  1. Thanks, Keep posting a good post. Thanks

    ReplyDelete
  2. Hi Anmol, I cannot find the .Include in the reference. What am I missing? Really appreciate your help. Thanks

    ReplyDelete
  3. Hi MM
    the above sample is for client object model.
    you need to reference two assemblies:
    Microsoft.SharePoint.Client.dll
    Microsoft.SharePoint.Client.Runtime.dll

    ReplyDelete

Popular Posts