Thursday, 21 November 2013

Select Insert update delete SharePoint custom list data using CSOM C# Example

Share it Please
public class EmpInfo
        {
            public EmpInfo(string Name, string Age)
            {
                _Name = Name;
                _Age = Age;
            }
            private string _Name;
            private string _Age;
            public string Name
            {
                get { return _Name; }
                set { _Name = value; }
            }
            public string Age
            {
                get { return _Age; }
                set { _Age = value; }
            }
        }
public void getdata()
        {
            NetworkCredential credentials = new NetworkCredential("UserName", "Pwd ", "DomainName");
            List<EmpInfo> lsts = new List<EmpInfo>();
            ClientContext cc = new ClientContext("http://Abc:4000/sites/Rsite1/");
            cc.Credentials = credentials;
            Web web = cc.Web;
            List list = web.Lists.GetByTitle("Users");
            CamlQuery caml = new CamlQuery();
            ListItemCollection items = list.GetItems(caml);
            cc.Load<List>(list);
            cc.Load<ListItemCollection>(items);
            cc.ExecuteQuery();
            foreach (Microsoft.SharePoint.Client.ListItem item in items)
            {
                string Name = Convert.ToString(item.FieldValues["Name"]);
                string Age = Convert.ToString(item.FieldValues["Age"]);
                lsts.Add(new EmpInfo(Name, Age));
            }
            GridView1.DataSource = lsts;
            GridView1.DataBind();
        }
Insert item:-
            NetworkCredential credentials = new NetworkCredential("UserName", "Pwd ", "DomainName");
            ClientContext context = new ClientContext("http://Abc:4000/sites/Rsite1/");
            context.Credentials = credentials;
            Web web = context.Web;
            List list = web.Lists.GetByTitle("Users");
            ListItemCreationInformation newItem = new ListItemCreationInformation();
            ListItem listItem = list.AddItem(newItem);
            listItem["Name"] = "Mohamed";
            listItem["Age"] = "27";
            listItem.Update();
            context.ExecuteQuery();
Update Item:-
            NetworkCredential credentials = new NetworkCredential("UserName", "Pwd", "DomainName");
            ClientContext context = new ClientContext("http://Abc:4000/sites/Rsite1/");
            context.Credentials = credentials;
            List list = context.Web.Lists.GetByTitle("Users");
            CamlQuery query = new CamlQuery();
            query.ViewXml = "<View/>";
            ListItemCollection listItems = list.GetItems(query);
            context.Load(listItems);
            context.ExecuteQuery();
            ListItem item = listItems[0];
            item["Name"] = "Mohamed sithik...";
            item["Age"] = "26";
            item.Update();
            context.ExecuteQuery();
Delete Item:-
            NetworkCredential credentials = new NetworkCredential("UserName", "Pwd ", "DomainName");
            ClientContext context = new ClientContext("http://Abc:4000/sites/Rsite1/");
            context.Credentials = credentials;
            List list = context.Web.Lists.GetByTitle("Users");
            ListItemCollection listItems = list.GetItems(new CamlQuery() { ViewXml = "<View/>" });
            context.Load(listItems);
            context.ExecuteQuery();
            listItems[2].DeleteObject();
            context.ExecuteQuery();

No comments:

Post a Comment

Followers

Follow The Author