把sharepoint list转化成DateTable

 /// <summary>
        /// 把sharepoint list 转化成DataTable
        /// </summary>
        /// <returns></returns>
        private DataTable ListToDataTable()
        {
            //构造表头
            DataTable _sourceDataTable;
            _sourceDataTable = this.ColumnNameDataTable(); 
            //SPList sourceList = pSourceSPWeb.Lists[this._sourceSPListName];
            //SPListItemCollection caseItems = sourceList.Items;

            SPSite site = new SPSite(siteurl);
            SPWeb web = site.OpenWeb();
            SPList treeList = web.Lists[sourceTreeList];
            DataTable dt = new DataTable();
            SPListItemCollection caseItems = treeList.Items;

            foreach (SPListItem caseItem in caseItems)
            {
                try
                {
                    DataRow rowNew = _sourceDataTable.NewRow();
                    for (int intI = 0; intI < this.pSourceSPListFields.Length; intI++)
                    {
                        if (caseItem[pSourceSPListFields[intI]] != null)
                        {
                            if (treeList.Fields[this.pSourceSPListFields[intI]].FieldValueType.ToString() == "Microsoft.SharePoint.SPFieldUserValue")
                            {
                                string createrUserName = caseItem[this.pSourceSPListFields[intI]].ToString();
                                int index = createrUserName.IndexOf(';');
                                int createrUserID = Convert.ToInt32(createrUserName.Substring(0, index));
                                SPUser createrUser = web.AllUsers.GetByID(createrUserID);
                                rowNew[pSourceSPListFields[intI]] = createrUser.Name;
                            }
                            else
                            {
                                Object columnType = _sourceDataTable.Columns[this.pSourceSPListFields[intI]].DataType;
                                rowNew[pSourceSPListFields[intI]] = caseItem[pSourceSPListFields[intI]];
                            }
                        }
                    }
                    _sourceDataTable.Rows.Add(rowNew);
                }
                catch
                {
                }
            }
            return _sourceDataTable;
        }


        /// <summary>
        /// 根据sharepoint列表创建DataTable字段
        /// </summary>
        /// <returns></returns>
        public DataTable ColumnNameDataTable()
        {

            SPSite site = new SPSite(siteurl);
            SPWeb web = site.OpenWeb();
            SPList treeList = web.Lists[sourceTreeList];
            DataTable dt = new DataTable();
            SPListItemCollection caseItems = treeList.Items;

            for (int intI = 0; intI < pSourceSPListFields.Length; intI++)
            {
                DataColumn columnNew = new DataColumn();

                columnNew.ColumnName = pSourceSPListFields[intI];
                dt.Columns.Add(columnNew);
            }
            return dt;
        }

posted @ 2013-02-22 11:03  天空行马  阅读(203)  评论(0编辑  收藏  举报