简体   繁体   中英

c# iterate through object members

The context here is that I want to see the permissions an object in a sharepoint list has. But the question has got to do with C#

Sharepoint has an object called SPListItem and you can view various details about the object by iterating over it's index.

I am able to iterate through the index of SPListItem by using integer numbers ( splistitem[i] ). But, the problem is that I don't know what property/detail is my program printing out.

How do I print out the name of the index and the index value as well ?

here is my code

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();
        }
    }
}

You can use below approach

 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);
               }
            }
}

This should do the trick:

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])
        }
    }
}

Regards, Martin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM