Social Icons

Pages

Friday, May 10, 2013

Creating List in SharePoint using Client Object Model (C#)

In this post I'm going to show how to create new list using Client Object model.
 

Step: 1 Add the SharePoint Reference
             Refer this article 

Step: 2  Add the SharePoint namespace
 using Microsoft.SharePoint.Client;

Step: 3 Here is the sample code
        public string CreateList(string Url, string UserName, string Password, String ListName, string Describtion)

      {

          try

          {

              ClientContext context = new ClientContext(Url);

              context.Credentials = new NetworkCredential(UserName, Password);
              Web site = context.Web;

              //Creating List
              ListCreationInformation infor = new ListCreationInformation();
              infor.Description = Describtion;
              infor.Title = ListName ;
              infor.TemplateType = 107;// Task List
              List list = web.Lists.Add(infor);

              //Creating Columns
              var RefidfieldXml = "<Field Type=\"Text\" MaxLength=\"100\" DisplayName=\"ReferenceID\"  Name=\"ReferenceID\" />";
              Field RefIDField = list.Fields.AddFieldAsXml(RefidfieldXml, true, AddFieldOptions.DefaultValue);
              context.ExecuteQuery();
              return "";
          }
          catch (Exception ex)
          {
              return ex.Message;
          }
      }

No comments:

Post a Comment