获取SPList项列标题和列数据,包括SPLookupFieldValue

时间:2014-01-11 18:45:01

标签: sharepoint-2010

在source下面只打印SPList第一页上的List Items。不在其他页面中打印ListItems数据,即第2页,第3页等。

此外,还需要打印SPLookupFieldValue,而不是建议某些链接可以满足要求的以下源代码的任何修改都会受到极大的重视

 static void Main(string[] args)
 {
    using (SPSite site = new SPSite("http://intranet.contoso.com"))
    {
      using (SPWeb web = site.OpenWeb())
      {
        // Get data from a list. 
        SPList list = web.Lists["Products"];
        SPView view = list.DefaultView;

        SPListItemCollection items = list.GetItems(view);
        // Get a collection of view field names. 
        StringCollection viewFields = view.ViewFields.ToStringCollection();
        // Print data for each item in the view. 
        foreach (SPListItem item in items)
        {
          // Print the value of each view field. 
          foreach (string fieldName in viewFields)
          {
            Console.WriteLine("{0} = {1}", fieldName, item[fieldName]);
          }
          Console.WriteLine();
        }
      }
    }
  Console.ReadLine();
}

国家/地区列是查找,因此数据将使用ID和值打印。只需打印该值即可。     产品=电视     国家= 2; #uk
    产品=汽车     国家= 1; #india

1 个答案:

答案 0 :(得分:1)

首先使用list.GetItems()

获取项目

第二次检查是字段查找,获取SPFieldLookupValue,然后获取值。

static void Main(string[] args)
{
    using (SPSite site = new SPSite("http://intranet.contoso.com"))
    {
    using (SPWeb web = site.OpenWeb())
    {
        // Get data from a list. 
        SPList list = web.Lists["Products"];
        SPView view = list.DefaultView;

        SPListItemCollection items = list.GetItems();
        // Get a collection of view field names. 
        StringCollection viewFields = view.ViewFields.ToStringCollection();
        // Print data for each item in the view. 
        foreach (SPListItem item in items)
        {
            // Print the value of each view field. 
            foreach (string fieldName in viewFields)
            {
                SPField field = list.Fields.GetField(fieldName);
                if (field.Type == SPFieldType.Lookup)
                {
                    string strValue = item[fieldName].ToString();
                    string value = new SPFieldLookupValue(strValue).LookupValue;
                    Console.WriteLine("{0} = {1}", fieldName, value);
                }
                else
                {
                    Console.WriteLine("{0} = {1}", fieldName, item[fieldName]);
                }
            }
            Console.WriteLine();
        }
    }
    }
    Console.ReadLine();
}