2014-09-25 143 views
0

这里的上下文是,我想看到一个对象在SharePoint列表中的权限。 但问题与C有关#c#遍历对象成员

Sharepoint有一个对象叫做SPListItem,你可以通过迭代它的索引来查看关于该对象的各种细节。

我能够通过使用整数(splistitem[i])遍历SPListItem索引。但问题是我不知道我的程序打印出来的属性/细节。

如何打印索引名称和索引值?

这里是我的代码

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Collections; 
using System; 
using Microsoft.SharePoint; 

namespace Test 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Console.WriteLine("-----"); 
      using (SPSite site = new SPSite("http://c4968397007/sites/anupamsworkspace/")) 
      { 
       using (SPWeb web = site.OpenWeb()) 
       { 
        SPList oSPList = web.Lists["Check2"]; 
        SPListItem oSPListItem = oSPList.Items[0]; 

        for (int i = 0; i < 100;i++) 
         //printing out the index value using int index, how do I print the name of the value it's printing out ? 
         Console.WriteLine(oSPListItem[i]); 
       } 
      } 
      Console.ReadLine(); 
     } 
    } 
} 

回答

0

这应该做的伎俩:

using (SPSite site = new SPSite("http://c4968397007/sites/anupamsworkspace/")) 
{ 
    using (SPWeb web = site.OpenWeb()) 
    { 
     var list = web.Lists["Check2"]; 
     var item = list.Items[0]; 
     foreach (var field in list.Fields) 
     { 
      Console.WriteLine("Key: {0} Value: {1}", field.StaticName, item[field.StaticName]) 
     } 
    } 
} 

问候, 马丁

+0

感谢您的回复martin,但visualstudio抱怨说“对象不包含StaticName的定义”。任何解决它的方法? – 2014-09-26 17:05:32

+0

这很奇怪,字段应该是一个带有StaticName属性的SPField对象(http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfield.staticname(v=office.15).aspx)。 将foreach语句更改为:foreach(list.Fields中的SPField字段) – 2014-09-29 20:02:45

0

您可以使用下面的方法

using (SPWeb web = site.OpenWeb()) 
      { 
       SPList list = web.GetList("Check2"); 
       if (list.ItemCount > 0) 
       { 
        SPListItem item = list.Items[0]; 
        Hashtable ht = item.Properties; 
        foreach (DictionaryEntry de in ht) 
        Console.WriteLine("Key: {0} Value: {1}", de.Key, de.Value); 
       } 
      } 
} 
+0

我不只需要'item.pro perties'我需要打印所有索引......其中约有75个 – 2014-09-26 03:05:15