Monday, May 05, 2014

SharePoint.NET Client Side Object Model - CSOM

SharePoint.NET Client Side Object Model(CSOM) is one of the approach to retrieve or CRUD operation on the SharePoint data on Client side application using ASP.NET web application,Windows application or Silverlight application.

 This asp.net web application must be hosted on where SharePoint server installed. You need to add the Microsoft.SharePoint.Client.Runtime.dll and Microsoft.SharePoint.Client.dll from the Common Files\Microsoft Shared\web server extensions\15\ISAPI location.



  Example code for retrieve the SharePoint list data and showing in ASP.NET grid.
        protected void btn_Click(object sender, EventArgs e)
        {
            var fList = new List();
            FriendEntity entity = null;
            var context = new ClientContext("http://win-49qfnhnl4fj/");
            List friendList = context.Web.Lists.GetByTitle("Friends");
            var query = new CamlQuery {ViewXml = ""};
            ListItemCollection items = friendList.GetItems(query);
            context.Load(items);
            context.ExecuteQuery();
            foreach (var item in items)
            {

                entity = new FriendEntity();
                entity.Prefix = item["Title"].ToString();
                entity.FirstName = item["First_x0020_Name"].ToString();
                entity.LastName = item["Second_x0020_Name"].ToString();
                fList.Add(entity);
            }
            grid.DataSource = fList;
            grid.DataBind();

        }
        public class FriendEntity
        {
            public string Prefix { get; set; }
            public  string FirstName { get; set; }
            public  string LastName { get; set; }
        }