- Tạo các columns ứng với tùng kiểu dữ liệu trong List.
- Hiển thị tất cả dữ liệu lên gridview.
- Xây dựng các Phương thức GetData từ các columns của List (tất cả các loại) bằng cách sử dụng những Class mà Sharepoint cung cấp sẵn.
- Các phương thức add new.
Tạo các columns ứng với tùng kiểu dữ liệu trong List.
ProvinceName : Dòng văn bản đơn
Number: Số
PlainText: Nhiều dòng văn bản
RichText: Nhiều dòng văn bản
EnhancedRichText: Nhiều dòng văn bản
DateTimeField: Ngày và Giờ
DropDownChoiceField: Lựa chọn
RadioButtonChoiceField: Lựa chọn
CheckBoxChoiceField: Lựa chọn
NumberField: Số
CurrencyField: Tiền Tệ
SingleLookUp: Tra cứu
MultiLookUp: Tra cứu
HyperLink: Siêu liên kết hoặc Hình ảnh
Picture: Siêu liên kết hoặc Hình ảnh
SingleUser: Cá nhân hoặc Nhóm
MultiUser: Cá nhân hoặc Nhóm
Calculated: Tính toán (phép tính dựa trên các cột khác)
Hiển thị tất cả dữ liệu lên gridview.
Các bước thực hiện:
Viết 1 phương thức để trả ra DataTable để đổ dữ liệu lên gridview
DataTable GetDataTableFromList(string listName, string viewFields, string camlQuery)
        {
            DataTable dataTable = null;
            try
            {
                SPSite spSite = SPControl.GetContextSite(Context);
                using (SPWeb spWeb = spSite.OpenWeb())
                {
                    SPList spList = spWeb.Lists[listName];
                    SPQuery spQuery = new SPQuery();
                    spQuery.ViewFields = viewFields;
                    spQuery.Query = camlQuery;
                    SPListItemCollection spListItemCollection = spList.GetItems(spQuery);
                    dataTable = spListItemCollection.GetDataTable();
                }
            }
            catch (Exception ex)
            {
                literalError.Text = ex.Message;
            }
            return dataTable;
       }
Khai báo và khởi tạo Button để hiển thị gridview và Khai báo event handleClick  cho Button
Button btnShowGridView = new Button();this.Controls.Add(btnShowGridView);btnShowGridView.Text = "Show All Fields";btnShowGridView.Click += new EventHandler(btnShowGridView_Click);
Gọi phương thức GetDataTable trong sự kiên click của button
string listName = "ProvinceTest";DataTable dataTable = GetDataTableFromList(listName, null, null);gridView.DataSource = dataTable;gridView.DataBind();
Xây dựng các Phương thức GetData từ các columns của List (tất cả các loại) bằng cách sử dụng những Class mà Sharepoint cung cấp sẵn.
Code + giải thích:
           Table table = ASPControl.Table(20, 5);
            table.Rows[0].Cells[0].Text = "Control";
            table.Rows[0].Cells[1].Text = "ID";
            table.Rows[0].Cells[2].Text = "Value";
            SPWeb spWeb = SPControl.GetContextSite(Context).OpenWeb();
            SPList spList = spWeb.Lists["ProvinceTest"];
            SPListItem item = spList.GetItemById(1);
            #region SingleLine
            table.Rows[1].Cells[0].Text = "SingleLine";
            table.Rows[1].Cells[2].Text = item["Title"].ToString();
            #endregion
            #region PlainText
            table.Rows[2].Cells[0].Text = "PlainText";
            table.Rows[2].Cells[2].Text = SPEncode.HtmlEncodePreserveSpaces(item["PlainText"].ToString());
            #endregion
            #region RichText
            table.Rows[3].Cells[0].Text = "RichText";
            table.Rows[3].Cells[2].Text = item["RichText"].ToString();
            #endregion
            #region EnhancedRichText
            table.Rows[4].Cells[0].Text = "EnhancedRichText";
            table.Rows[4].Cells[2].Text = Convert.ToString( item["EnhancedRichText"]);
            #endregion
            #region DropDownChoiceField
            SPFieldMultiChoiceValue fieldDropDownChoice = new SPFieldMultiChoiceValue(item["DropDownChoiceField"].ToString());
            table.Rows[5].Cells[0].Text = "DropDownChoice";
            table.Rows[5].Cells[2].Text += fieldDropDownChoice[0].ToString();
            #endregion
            #region RadioButtonChoiceField
            SPFieldMultiChoiceValue fieldRadioButtonChoice = new SPFieldMultiChoiceValue(item["RadioButtonChoiceField"].ToString());
            table.Rows[6].Cells[0].Text = "RadioButtonChoice";
            table.Rows[6].Cells[2].Text += fieldRadioButtonChoice[0].ToString();
            #endregion
            #region CheckBoxChoiceField
            SPFieldMultiChoiceValue fieldCheckBoxChoice = new SPFieldMultiChoiceValue(item["CheckBoxChoiceField"].ToString());
            table.Rows[7].Cells[0].Text = "CheckBoxChoice";
    for (int i = 0; i < fieldCheckBoxChoice.Count; i++)
            {
                table.Rows[7].Cells[2].Text += fieldCheckBoxChoice[i].ToString() + "<br>";
            }
            #endregion
            #region NumberField
            table.Rows[8].Cells[0].Text = "NumberField";
            table.Rows[8].Cells[2].Text = item["NumberField"].ToString();
            #endregion
            #region CurrencyField
            table.Rows[9].Cells[0].Text = "CurrencyField";
            table.Rows[9].Cells[2].Text = item["CurrencyField"].ToString();
            #endregion
            #region DateTimeField
            table.Rows[10].Cells[0].Text = "DateTimeField";
            table.Rows[10].Cells[2].Text = item["DateTimeField"].ToString();
            #endregion
            #region SingleLookUp
            SPFieldLookupValue singleLookUp = new SPFieldLookupValue(item["SingleLookUp"].ToString());
            //Single LookUp: Get ID, Value
            table.Rows[11].Cells[0].Text = "Single LookUp";
            table.Rows[11].Cells[1].Text = singleLookUp.LookupId.ToString();
            table.Rows[11].Cells[2].Text = singleLookUp.LookupValue.ToString();
            #endregion
            #region MultiLookUp
            SPFieldLookupValueCollection multiLookup = new SPFieldLookupValueCollection(item["MultiLookUp"].ToString());
            table.Rows[12].Cells[0].Text = "Multi LookUp";
            for (int i = 0; i < multiLookup.Count; i++)
            {
                table.Rows[12].Cells[1].Text += multiLookup[i].LookupId.ToString() + "<br>";
                table.Rows[12].Cells[2].Text += multiLookup[i].LookupValue.ToString() + "<br>";
            }
            #endregion
            #region Hyperlink & Picture
            SPFieldUrlValue url = new SPFieldUrlValue(item["HyperLink"].ToString());
            table.Rows[13].Cells[0].Text = "HyperLink";
            table.Rows[13].Cells[2].Text = url.Url.ToString();
            //table.Rows[13].Cells[1].Text = url.Description.ToString();
            #endregion
            #region Calculate
            SPFieldCalculated calculated = (SPFieldCalculated)item.Fields["Calculated"];
            table.Rows[17].Cells[0].Text = "Calculated";
            table.Rows[17].Cells[2].Text = calculated.GetFieldValueAsText(item["Calculated"]);
            #endregion
            this.Controls.Add(table);
Các phương thức add new.
SPWeb spWeb = SPControl.GetContextSite(Context).OpenWeb();
            SPList spList = spWeb.Lists["ProvinceTest"];
            SPListItemCollection spListItemCollection = spList.Items;
            SPListItem spListItem = spListItemCollection.Add();
            spListItem["Title"] = txtTitle.Text;
            spListItem["ProvinceName"] = txtProvinceName.Text;
            spListItem["Number"] = txtNumber.Text;
            spListItem["PlainText"] = txtPlainText.Text;
            spListItem["RichText"] = txtRichText.Text;
            spListItem["EnhancedRichText"] = txtEnhancedRichText.Text;
            spListItem["DateTimeField"] = txtDateTimeField.Text;
            //khai báo giá trị cần Add New với Class SPFieldLookupValue
            SPFieldLookupValue spFieldLookupValue = new SPFieldLookupValue(217, "Báo cáo tiến độ Dự án Falcon Portal");
     //Gán giá trị đó vào ListItem
            spListItem["SingleLookUp"] = spFieldLookupValue.ToString();                       
            //Khai báo SPFieldLookupValueCollection để chứa tập các giá trị
            SPFieldLookupValueCollection spFieldLookupValueCollection = new SPFieldLookupValueCollection();
     //khai báo giá trị cần Add New với Class SPFieldLookupValue và add vào  SPFieldLookupValueCollection
     spFieldLookupValueCollection.Add(new SPFieldLookupValue(217, "Báo cáo tiến độ Dự án Falcon Portal"));
            spFieldLookupValueCollection.Add(new SPFieldLookupValue(212, "Họp cuối tuần"));
            spListItem["MultiLookUp"] = spFieldLookupValueCollection;
            //khai báo Class SPFieldUserValueCollection để chứa các tập hợp User
            SPFieldUserValueCollection singleUser = new SPFieldUserValueCollection();
                     //khai báo Class SPFieldUserValue lấy ra 1 use
            SPFieldUserValue singleUserToAdd = ConvertLoginName(spWeb, spWeb.CurrentUser.ToString());
            singleUser.Add(singleUserToAdd);
            spListItem["SingleUser"] = singleUser;
            //khai báo nhiều User
            string userControlValue = @"SPSERVER\lamtd,FPORTAL\hungdq";
            spWeb.AllowUnsafeUpdates = true;
                     //cũng tương tự trên nhưng là lấy ra nhiều User để insert vào List 
            SPFieldUserValueCollection userCollection = new SPFieldUserValueCollection();
            string[] userArray = userControlValue.Split(',');
            for (int i = 0; i < userArray.Length; i++)
            {
                     SPUser spUser = spWeb.EnsureUser(userArray[i]);
SPFieldUserValue multiUserToAdd = new SPFieldUserValue(spWeb, spUser.ID, spUser.Name);
SPFieldUserValue multiUserToAdd = new SPFieldUserValue(spWeb, spUser.ID, spUser.Name);
                     userCollection.Add(multiUserToAdd);
            }
            spListItem["MultiUser"] = userCollection;
            //khai báo Class SPFieldUrlValue để lấy ra được đối tượng hình ảnh
            SPFieldUrlValue spFieldUrlValue = new SPFieldUrlValue("http://192.168.1.216/PublishingImages/Capture.JPG");                      
            spListItem["Picture"] = spFieldUrlValue;
            spListItem.Update();





