Tuesday, September 29, 2009

SharePoint List C# Part 1


Here I'm going to show you how you can use the SharePoint object model to read sharepoint list using C#.

To do this in your custom webpart or feature first you have to add the reference to Microsoft.Sharepoint.dll (Then in your code use "using Microsoft.Sharepoint;"). And also if you are going to run the program, you have to be in the server.

Read SharePoint List

public void getData()
{
    // choose your site
    string strUrl = "http://mysite:5050/";
    using (SPSite site = new SPSite(strUrl))
    {
        using (SPWeb web = site.OpenWeb())
        {
            // choose the list
            SPList list = web.Lists["insert your list Name"];

            SPQuery myquery = new SPQuery();
            myquery.Query = "insert your query here";

            // if you dosent insert query (myquery.Query ="") you will get all items

            SPListItemCollection items = list.GetItems(myquery);

            foreach (SPListItem item in items)
            {
                // check the item for null
                if (item != null)
                {
                    // do something
                }
            }
        }
    }
}

Adding Item

string strUrl = "http://mysite:5050/";
            using (SPSite site = new SPSite(strUrl))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPListItemCollection listItems = web.Lists["List_Name"].Items;
                    SPListItem item = listItems.Add();
                    item["Title"] = "New Item Title";

                    item.Update();
                }
            }

You can use U2U CAML Query Builder to write queries you want.

If you want to get information from lookup field, please refer SharePoint List C# Part 2, for delete items please refer Delete items from SharePoint list.

18 comments:

  1. Very helpful. Your style makes coding in sharepoint seem easy to understand.

    ReplyDelete
  2. Hi,

    It was really helpful, Thanks a lot. But I have a question for you : How can I copy a listItem into another list whose columns are exactly the same as the first list's columns?

    ReplyDelete
  3. @ Anonymous,
    thanks for the comment,
    In the above example you can see getData() method. There inside the for loop you can write;

    SPListItemCollection listItems_new = web.Lists["Second_List_Name"].Items;
    SPListItem item_new = listItems_new.Add();
    item_new["Title"] = item["Title"].ToString();
    // update other columns
    item_new.Update();

    ReplyDelete
  4. Hi Saranga,

    I'm trying to sync the data from the sharepoint to my SQL database. But somehow the GetListItems seems to break down. I'm using the "GetViewCollection" to get the "listID" and the "viewID". then I passed this 2 inside to my GetListItems.

    XmlNode xmlList = m_gel.m_List.GetListItems(listID, viewID, _n, _n, "" + Int32.MaxValue, _n);

    Somehow it's not able to sync. Can you please advice?

    Thank you very much in advance

    ReplyDelete
  5. Hi,

    Please see,
    SharePoint List Web Service GetListItems
    check whether you are querying correctly.

    Thank You !

    ReplyDelete
  6. Hi Saranga,
    I'm the Anonymous posted earlier.

    Thank you very much for your fast response.. It really meant a lot.

    Well I just want to ask, can i pass in "null" for the "query", "viewfields" and "queryOptions"?

    My structure works in such a way,

    XmlNode xmlList = m_gel.m_List.GetListItems(listID, viewID, _n, _n, "" + Int32.MaxValue, _n);


    XmlTextReader rL = new XmlTextReader(xmlList.OuterXml, XmlNodeType.Document, null);

    Then populate the data from the XmlTextReader straight. This being said, means that I never declare XMLDocument Object and never use the CAML query.

    Will this be the reason of why the sync is not working?

    Please do advice, really thankful for your fast response.

    Warmest Regards,
    Michael,

    ReplyDelete
  7. Hi Saranga,
    Thanks for this post. I am new to sharepoint and your post on reading the list items helped me do things faster.
    One of my CAML queries should return more than 200 rows but because the view has some rowlimit and paging, I am getting only 100 rows. How do I read the next rows? Also my rs:data is like this...If this cud be of any help

    ReplyDelete
  8. @ Shambhavi,
    Did you check it using;
    SPQuery myquery = new SPQuery();
    myquery.RowLimit = 300; or something ?

    ReplyDelete
  9. Thanks for your response. But, mine is a .net class library (not refering SP utilities).
    I have referenced to a SP List web sevices and am using the GetListItems method in my application.

    ReplyDelete
  10. Hi Saranga,

    Just to confirm with you, do I really need to have the Query Option using the CAML?

    If I have the view GUID, I don't need the query option right?

    Based on my previous post here. Is there anything wrong with the code structure?

    XmlNode _n = null;


    XmlNode xmlList = m_gel.m_List.GetListItems(m_ListID, m_ViewID, _n, _n, "" + Int32.MaxValue, _n);

    XmlTextReader rL =
    new XmlTextReader(xmlList.OuterXml, XmlNodeType.Document, null);

    Thank you in advance,

    Best Regards,
    Michael Lim

    ReplyDelete
  11. I this is very helpful for me.Here my problem is " I have a pages library list and i want to archive those items based on month.Suppose for September month we have 10 entries.i have to display 5 items in home page and remaining in archive folder.pls tell how to write a code for this.it is very urgent for me....pls..

    ReplyDelete
  12. 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
  13. Hey Saranga,
    obv this is for usage on the sp server itself.
    how would it work if i wanted to use it on the client side? i cant find my way through it - do you know?

    best regards,
    gary

    ReplyDelete
  14. @ Bi0logiCaL,
    You can use web service for get that task done.
    Try this post;
    SharePoint List Web Service GetListItems

    ReplyDelete
  15. HI ALL
    I am an existing SharePoint Web site is not error. Please what should I do?
    string URL = "http://reza:9091/COMPUTER/SitePages/Home.aspx";

    using (SPSite site = new SPSite(URL))
    {
    using (SPWeb web = site.OpenWeb())
    {
    // choose the list
    SPList list = web.Lists["GOD"];

    This code should be written in the Web Part or in any part of a vs was running?

    ReplyDelete
  16. HI ALL
    Run-time error code in the program but does not find the website address.

    string URL = "http://reza:9091/COMPUTER/SitePages/Home.aspx";

    using (SPSite site = new SPSite(URL))
    {
    using (SPWeb web = site.OpenWeb())
    {
    // choose the list
    SPList list = web.Lists["GOD"];

    This code should be written in the Web Part or in any part of a vs was running?

    ReplyDelete