none
How to read sharepoint list attributes?

    Question

  • Hi, I need to fetch the attributes of a specific list from sharepoint using C#.

    using(SPSite site = new SPSite("http://sharepointsite/"))
    {
        using(SPWeb web = site.OpenWeb())
        {
           SPListCollection listCollection = web.Lists;
           foreach (SPList list in listCollection)
           {
            comboBoxLists.Items.Add(list.ToString());
           }
        }
    }

    The above code fetch me the sharepoint lists.

    Now my requirement is, to fetch the attributes of specific list. For example, "Tasks" is a list and when we create a new task, it will ask various attributes such as, "Title, priority, assigned to, status, description etc". I need to get this list of attributes and update the property grid with these attributes. How can I achieve this?

     

    Monday, October 25, 2010 6:22 AM

All replies


  • Hi Mahi,

    Do you want to fetch columns for the list?

    if yes then below is code

    using(SPSite site = new SPSite("http://sharepointsite/ "))
    {
        using(SPWeb web = site.OpenWeb())
        {
           SPListCollection listCollection = web.Lists;
           foreach (SPList list in listCollection)
           {

             foreach(SpListItem item in list)

          {

             string title= item["Title"].tostring();

              }

     
           }
        }
    }


    Pathik Rawal Blog: http://pathikhrawal.wordpress.com
    Monday, October 25, 2010 7:07 AM
  • Hi Mahi,

    Maybe you are looking for the Fields of the list. You can access them with the Fields property on the List.

    var list = web.Lists["Tasks"];
    foreach (var field in list.Fields)
    {
     // do something with fields.
    }
    

    Kind Regards,
    Joran Markx


    Joran Markx DiVa BV (The Netherlands) Blog - http://blog.diva.nl Website - http://www.diva.nl
    Monday, October 25, 2010 8:22 AM
  • I can also recomend be carefull using List, try to use CAML to filter and make some read about accessing List Data, when you start to have thousands of items is not good pratice list all If you have Folders inside then you will have some Scope="Recursive" problems with some performance issues.
    This tool will help you creating the CAML and accessing the items you want.
    http://www.u2u.net/res/Tools/CamlQueryBuilder.aspx

    Recomend read this Book before starting working with List will give you some good pratices on how to work:
    "Best Practices: Common Coding Issues When Using the SharePoint Object Model"
    http://msdn.microsoft.com/en-us/library/bb687949(office.12).aspx
    "Working with large lists in Office SharePoint Server 2007"
    http://technet.microsoft.com/en-us/library/cc262813(office.12).aspx

    To update the List.item i can also recomend read this document, have the good pratices on how to update.
    "SPListItem Class"
    http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistitem(v=office.12).aspx

    Hope this help...


    André Lage Microsoft SharePoint, CRM and Sybase Consultant
    Blog:http://aaclage.blogspot.com
    Codeplex:http://spupload.codeplex.com/http://simplecamlsearch.codeplex.com/
    Monday, October 25, 2010 8:28 AM
  • using (SPSite site = new SPSite("http://sharepointsite/"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists[comboBoxLists.SelectedItem.ToString()];
                        SPFieldCollection fields = list.Fields;
                        List<string> fieldNames = new List<string>();
                       
                        foreach (SPField field in fields)
                        {
                            if (!field.ReadOnlyField && !(SPFieldType.Computed == field.Type))
                            {
                              fieldNames.Add(field.Title);//This indicates the attribute name of the field
                            }
                        }
                        //propertyGrid1.Tag = fieldNames;
                        var results = from field in fieldNames
                                      select field;
                        //propertyGrid1.Tag = results.Distinct();
                        foreach (string fieldName in results.Distinct())
                        {
                            comboBoxFields.Items.Add(fieldName);
                        }
                    }
                }

    But how can I bind these attributes to property grid?

    Monday, October 25, 2010 9:56 AM