Wednesday, 30 January 2013

Filtering List in SharePoint 2010 using Client Object Model (C#)



Step 1: Create New Project. (Minimum .NET Framework 3.5)
Step 2: Add “Microsoft.SharePoint.Client” and “Microsoft.SharePoint.Client.Runtime” namespaces.
Step 3: Write the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//SharePoint namespace
using Microsoft.SharePoint.Client;

namespace FilteringListByContentType
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "http://sharepointserver.com";
            ClientContext clientContext = new ClientContext(siteUrl);
            clientContext.Credentials = new System.Net.NetworkCredential("admin", "password", "sharepointserver.com");
            Web site = clientContext.Web;

            //Filtering List by Content Type
            ListCollection listCollection = site.Lists;
            IEnumerable<List> filteredLists = clientContext.LoadQuery(listCollection.Where(list => list.BaseType == BaseType.DocumentLibrary));
            clientContext.ExecuteQuery();

            //Printing List
            foreach (List li in listCollection)
            {
                Console.WriteLine("Title: {0}", li.Title);
            }
            Console.ReadLine();
        }
    }
}


No comments:

Post a Comment