Tuesday, September 29, 2009

SharePoint List C# Part 2


In my previous post SharePoint List C# Part 1, I wrote how to retrieve data from SharePoint list. In this post also I'm going to show you how to retrieve data from SharePoint list which has look up fields. You can use this code in your custom webpart ot feature.

        
  public void getData()
        {
            // choose the site
            SPSite site = new SPSite("http://mysite:5050/");
            SPWeb web = site.OpenWeb();

            // choose the list "Task Categories"
            SPList list = web.Lists["Task Categories"];
            SPListItemCollection itemCollection;

            // pass query to get status
            SPQuery oQuery = new SPQuery();
            // get all items
            oQuery.Query = "";
            itemCollection = list.GetItems(oQuery);

            foreach (SPListItem item in itemCollection)
            {
                if (item != null)
                {
                    // get data in "Sub Category(s)" lookup column
                    if (item["Sub Category(s)"] != null)
                    {
                        SPFieldLookupValueCollection subItemColl = ((SPFieldLookupValueCollection)item["Sub Category(s)"]);
                        foreach (var subitem in subItemColl)
                        {
                            // do something
                        }
                    }
                }
            }
        }

3 comments:

  1. I have worked on SharePoint Lists, but don't started to work on such customization from code behind, it will help me in near future to do such coding.

    ReplyDelete
  2. Hello and thanx for this helpfull tutorial. could you please make a video instate and show how to code and see the results in sharepoint site/server i am a beginner and dont know how to see the result when i deploy the code, maybe do a step by step tutorials for beginners please and thanx in advance.

    my skype is : gangster2world
    please add me if you can help thanx.

    ReplyDelete
  3. Hello and thanx for this helpfull tutorial. could you please make a video instate and show how to code and see the results in sharepoint site/server i am a beginner and dont know how to see the result when i deploy the code, maybe do a step by step tutorials for beginners please and thanx in advance.

    my skype is : gangster2world
    please add me if you can help thanx.

    ReplyDelete