sharepoint查阅项读取

人走茶凉 提交于 2020-03-20 23:19:38

 protected void InitData()
        {
            string siteUrl = SPContext.Current.Web.Url;
            SPSite site = new SPSite(siteUrl);
            SPWeb web = site.OpenWeb();
            DataTable table = GetCamlDataListRetTable(GetCamlDataList("EmployeeList"));
            DataTable tb = new DataTable();
            tb.Columns.Add("EmployeeName");
            tb.Columns.Add("Department");
            tb.Columns.Add("Sex");
            tb.Columns.Add("Age");
            tb.Columns.Add("ID");

            foreach (DataRow item in table.Rows)
            {
                SPFieldLookupValue fieldLookup = new SPFieldLookupValue(item["部门"].ToString());
                string test = fieldLookup.LookupValue;//查阅项值
                tb.Rows.Add(item["用户名"].ToString(), test, item["性别"].ToString(), item["年龄"].ToString(), item["ID"].ToString());

            }
            gridview.DataSource = tb;
            gridview.DataBind();
        }

///当查阅项是多选时

                    SPFieldLookupValueCollection spflvCollection = new SPFieldLookupValueCollection(sq["参会人员"].ToString());
                    List<string> persons = new List<string>();
                    List<string> jiluperson = new List<string>();
                    #region 参会人员判断
                    foreach (SPFieldLookupValue lookValue in spflvCollection)
                    {
                        if (!persons.Contains(lookValue.LookupValue))
                        {
                            persons.Add(lookValue.LookupValue);
                        }
                        txt_Person.Text += lookValue.LookupValue + ";";
                    }
                    #endregion

/// <summary>
        /// 返回查询列表所有数据
        /// </summary>
        /// <param name="listName">列表名称</param>
        /// <returns></returns>
        public SPListItemCollection GetCamlDataList(string listName)
        {
            SPList list = SPContext.Current.Web.Lists[listName];
            SPQuery query = new SPQuery();
            StringBuilder sb = new StringBuilder();
            sb.Append("   <OrderBy>");
            sb.Append("     <FieldRef Name=" + listName + " />");//列名称
            sb.Append("   </OrderBy>");
            query.Query = sb.ToString();
            SPListItemCollection spic = list.GetItems(query);
            return spic;
        }

        /// <summary>
        /// 根据Caml查询返回数据表
        /// </summary>
        /// <param name="spic">Caml结果集</param>
        /// <returns></returns>
        public DataTable GetCamlDataListRetTable(SPListItemCollection spic)
        {
            DataTable dt = new DataTable();
            for (int i = 0; i < spic.List.ContentTypes[0].Fields.Count; i++)
            {
                if (dt.Columns == null || dt.Columns.Count < 1)
                    dt.Columns.Add(spic.List.ContentTypes[0].Fields[i].Title);
                else
                {
                    bool dtColumns = true;
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        if (dt.Columns[j].ColumnName == spic.List.ContentTypes[0].Fields[i].Title)
                        {
                            dtColumns = false;
                        }
                    }
                    if (dtColumns)
                    {
                        dt.Columns.Add(spic.List.ContentTypes[0].Fields[i].Title);
                    }
                }
            }
            dt.Columns.Add("ID");
            if (spic.List.ItemCount > 0)
            {
                for (int i = 0; i < spic.Count; i++)
                {
                    DataRow dr = dt.NewRow();
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        dr[dt.Columns[j].ColumnName] = Convert.ToString(spic[i][spic[i].Fields[dt.Columns[j].ColumnName.ToString()].InternalName]);
                    }
                    dt.Rows.Add(dr);
                }
            }
            return dt;
        }

 <ContentTypes>
      <ContentType ID="0x0100c2917396131641a896d310050623ef03" Name="Demo - Employee" Group="自定义内容类型" Description="我的内容类型" Inherits="TRUE" Version="0">
        <FieldRefs>
          <FieldRef ID="{0A27A629-CB34-4B6D-A68F-9873EAC77CA5}" Name="EmployeeName" />
          <FieldRef ID="{13E113B0-4E72-4E61-99FB-3E66C671D636}" Name="Sex" />
          <FieldRef ID="{A7F0556E-FCBF-4698-AC6B-EBE6E91C5CE5}" Name="Age" />
          <FieldRef ID="{250E89A0-73D4-44C8-8310-4E152BA848A8}" Name="Department" />
        </FieldRefs>
        <XmlDocuments>
          <XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms/url">
            <FormUrls xmlns="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms/url">
              <New>_layouts/Demo/EmployeeAdd.aspx</New>
            </FormUrls>
          </XmlDocument>
        </XmlDocuments>
      </ContentType>
    </ContentTypes>
    <Fields>
      <Field ID="{0a27a629-cb34-4b6d-a68f-9873eac77ca5}" Name="EmployeeName" DisplayName="用户名" Type="Text" SourceID="http://schemas.microsoft.com/sharepoint/v3" Required="TRUE" StaticName="EmployeeName" />
      <Field ID="{13e113b0-4e72-4e61-99fb-3e66c671d636}" Name="Sex" DisplayName="性别" Type="Text" SourceID="http://schemas.microsoft.com/sharepoint/v3" Required="TRUE" StaticName="Sex" />
      <Field ID="{a7f0556e-fcbf-4698-ac6b-ebe6e91c5ce5}" Name="Age" DisplayName="年龄" Type="Text" SourceID="http://schemas.microsoft.com/sharepoint/v3" Required="TRUE" StaticName="Age" />
      <Field ID="{250e89a0-73d4-44c8-8310-4e152ba848a8}" Name="Department" DisplayName="部门" List="Lists/DepartmentList" ShowField="Title" Type="Lookup" SourceID="http://schemas.microsoft.com/sharepoint/v3" Required="TRUE" StaticName="Department"  EnforceUniqueValues="FALSE" RelationshipDeleteBehavior="None" />
    </Fields>  //ShowField="Title"

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!