Social Icons

Pages

Sunday, May 12, 2013

Updating & Retrieving SharePoint List using Client Object Model (C#)

Step: 1 Add the SharePoint Reference 
              Refer this article 

Step: 2 Add the SharePoint namespace

using Microsoft.SharePoint.Client;
Update the Share Point List
public string UpdateTaskList(string Url, string UserName, string Password, string Title, string Describtion, string ListID)
       {
           try
           {
               ClientContext context = new ClientContext(Url);
               context.Credentials = new NetworkCredential(UserName, Password);
               Web site = context.Web;
               Guid id = new Guid(ListID.ToString());
               context.Load(web);
               List objList = web.Lists.GetById(id);
               objList.Title = Title;
               objList.Description = Describtion;
               objList.Update();
               context.ExecuteQuery();
               return "";

           }
           catch (Exception e)
           {
               return e.Message;
           }
       }
 Create List Table
       void CreateListtable()

      {
          try
          {
              dtList = new DataTable();
              dtList.Columns.Add("List", typeof(string));
              dtList.Columns.Add("Description", typeof(string));
              dtList.Columns.Add("ListType", typeof(string));
              dtList.Columns.Add("ItemCount", typeof(string));
              dtList.Columns.Add("LastModified", typeof(string));
              dtList.Columns.Add("ID", typeof(string));
          }
          catch (Exception)
          {

              throw;
          }
      }
  Retrieving SharePoint List
       public DataTable GetTaskList(string Url, string UserName, string Password)
       {
           try
           {
               CreateListtable();
               ClientContext context = new ClientContext(Url);
               context.Credentials = new NetworkCredential(UserName, Password);
               Web site = context.Web; ;
               ListCollection listcollection = web.Lists;
               context.Load(listcollection);
               context.ExecuteQuery();
               foreach (List listName in listcollection)
               {
                   //Only Task list
                   if (listName.BaseTemplate == 107)
                   {
                       DataRow dr = dtList.NewRow();
                       dr["List"] = listName.Title;
                       dr["Description"] = listName.Description;
                       dr["ListType"] = listName.BaseTemplate;
                       dr["ItemCount"] = listName.ItemCount;
                       dr["LastModified"] = listName.LastItemModifiedDate;
                       dr["ID"] = listName.Id;
                       dtList.Rows.Add(dr);
                   }
               }
               return dtList;
           }
           catch (Exception)
           {

               return null;
           }
      

No comments:

Post a Comment