Showing posts with label Lookup Columns. Show all posts
Showing posts with label Lookup Columns. Show all posts

Wednesday, December 9, 2009

Add Item To List With LookUp Column Programmatically


In my previous posts on "Working With SharePoint List" I have discussed how we can Read, Add and Update SharePoint List. If you have to add new Item to a list which has lookup column, you have to follow a different procedure.

If you look at the following figure, you can easily understand what I'm going to do. I'm going to add a new record to List_B, which has a lookup column from List_A's Title Field.



This is a sample code to get the above task done,
        
    public void addToLookUp()
    {
        /* open the web */
        SPSite site = new SPSite("http://merdev-moss:5050/testsara");
        SPWeb web = site.OpenWeb();

        string targetList = "List_B";
        string sourceList = "List_A";
        string sourceListField = "Title";

        /* we are going to add new row to List_B,
        * It (List_B) has a lookup column called "A_Column",
        * It (A_Column) get data from Title field of List_A.
        * (List_A has value called "test2" in Title field)
        */

        SPListItemCollection listItems = web.Lists[targetList].Items;
        SPListItem item = listItems.Add();
        item["Title"] = "New Title";
        int ID = get_ID(web, sourceList, sourceListField, "test2");
        if (ID != 0)
            item["A_Column"] = ID;
        item.Update();
    }

    public static int get_ID(SPWeb web, string list, string field, string itemname)
    {
        int id = 0;
        SPList sharedDocumentList = web.ServerRelativeUrl.Equals("/") ? web.GetList(web.ServerRelativeUrl + list) : web.GetList(web.ServerRelativeUrl + "/" + list);
        SPListItemCollection listItems = sharedDocumentList.Items;
        foreach (SPListItem item in listItems)
        {
            if (item[field].ToString() == itemname.ToString())
                id = item.ID;
            break;
        }
        return (id);
    }

Thursday, November 26, 2009

Add Lookup Column to SharePoint List Programmatically


In one of my previous article I discussed how to create a SharePoint list programmatically in C#. There I didn’t discuss how we can add lookup field for our newly created list. Let’s see how, this is a sample code.
        public void createList()
        {
            // choose your site
            SPSite site = new SPSite("http://merdev-moss:5050");
            SPWeb web = site.OpenWeb();
            SPListCollection lists = web.Lists;

            // create new Generic list called "My List"
            lists.Add("My List", "My list Description", SPListTemplateType.GenericList);

            SPList newList = web.Lists["My List"];

            // create Text type new column called "My Column"
            newList.Fields.Add("My Column", SPFieldType.Text, true);

            /*create lookup type new column called "Lookup Column"
            * Here I am going to get the information from the "Title"
            * column of a list called "User Roles"
            */
            SPList targetList = web.Lists["User Roles"];

            newList.Fields.AddLookup("Lookup Column", targetList.ID, false);
            SPFieldLookup lkp = (SPFieldLookup)newList.Fields["Lookup Column"];
            lkp.LookupField = targetList.Fields["Title"].InternalName;
            lkp.Update();

            // make new column visible in default view
            SPView view = newList.DefaultView;
            view.ViewFields.Add("My Column");
            view.ViewFields.Add("Lookup Column");
            view.Update();
        }
In the above code I have created Generic list and lookup column. You can use whatever list type and field type according to your requirement. To learn more about SharePoint lists, follow "SharePoint List C# Part 1".

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