locked
Reading SharePoint List from Console Application RRS feed

  • Question

  • I'm writing a console application that needs to access a SharePoint list on a remote server. It needs to read the "table" from the list (do not need to write/modify, just read the data), and then process the data into a report (filtering it by date, modified by, and some custom columns for categorization). What would be the best way to do this?

    I currently have it working by exporting the list as an Access database which is linked. Then, my program connects to this accdb and gets the information. The only problem is that some columns do not come through correctly. I have some columns with Choice and Lookup types (checkmarks for categorization, a list of names to choose from, etc), however these do not come through. When I view the list online through SharePoint, the values are displayed correctly. In my console application though, all single-line text or multi-line text fields come through perfectly (including formatting for rich text and HTML fields), however the Choice and Lookup fields do not come through properly (the data in those fields shows up as "Caiv></div>ExternalClass134...").

    How can I either fix the current access method, or is there a better way to connect to a SharePoint site and read a list without modifying it?

     

    Many thanks in advance!

    • Moved by Figo Fei Monday, November 1, 2010 3:15 AM (From:Visual C# General)
    • Moved by Peter Jausovec Monday, November 8, 2010 11:30 PM (From:Sharepoint Development with Visual Studio)
    Tuesday, October 26, 2010 8:55 PM

All replies

  • Hi George,

    The first design question to consider is where you console application will be run?  If the console application will be run from a SharePoint Server then you could use the SharePoint Object Model.  If you plan to run the console application from computer without SharePoint then you should use Web Services. 

    The next question is what version of SharePoint are you using?  If you are using MOSS 2007 then your choices are basically the object model or web services.  If you are using SharePoint 2010 then you can the Object Model, Web Services (ASX, WCF, Rest), or the client-side API.

    Eitherway, you want to treat the SharePoint list as a database table.  Meaning it is just a repository storing your data. 

    Here is a great example of reading values from a task list:

    SPWeb oWebsite = SPContext.Current.Web;
    
    SPList oList = oWebsite.Lists["Tasks"];
    SPListItemCollection collItem = oList.GetItems("Title", "Status");
    
    foreach(SPListItem oItem in collItem)
    {
      Response.Write(SPEncode.HtmlEncode(oItem["Title"].ToString()) + " :: " + 
        SPEncode.HtmlEncode(oItem["Status"].ToString()) + "<BR>");
    }
    
    

    Reference:

    http://msdn.microsoft.com/en-us/library/ms460897.aspx

    http://msdn.microsoft.com/en-us/library/ee857094(office.14).aspx - Contains Console Application Examples.

     


    Dennis Bottjer | Follow Me: @dbottjer | Blog: Dennis Bottjer.com
    Wednesday, October 27, 2010 1:24 AM
  • Hello Dennis, thank you very much for your reply.

    The console application will not be running on the SharePoint server (I do not have access to that server, exception for SharePoint via my web browser). I'd like the application to be able to run from any machine (except for the SharePoint server). The SharePoint server is running 12.0.0.6504 (2007). My development machines are running Windows 7 and Windows Server 2008 R2 (Using Visual Studio 2010 with a .NET 4 project).

    Wednesday, October 27, 2010 8:33 PM
  • Hi George,

    Since you are using MOSS 2007 you have fewer options than are available with SharePoint 2010 but your task is still quite achievable.  SharePoint exposes web services for use within client applications such as the console application you are developing.  I would suggest reference the list service: http://<server-url>/_vti_bin/lists.asmx

    References:

    http://blogs.msdn.com/b/ericwhite/archive/2009/01/06/getting-started-with-sharepoint-wss-web-services-using-linq-to-xml.aspx

    • Edited by dbottjer Monday, November 1, 2010 3:36 AM More Information
    Monday, November 1, 2010 3:24 AM
  • Thanks Dennis, that article is great and is (partially) working for me.

     

    It successfully pulls some information from all lists available in my SharePoint site. However, I only need it to access one specific list, and then give me all of the data from that list. Unfortunately, the way the code works now, it gives me the name of the list and the description, however it won't list the description of each line item in a given list (although it does list the name of the line item). The Description field in my list is where some vital data I am trying to extract is stored, and I have many other columns/fields that I need to pull from. I'm not sure how to make it pull more column data out.

     

    How can I add more columns/fields to retrieve data from, including custom fields I've created? Also, how can I limit this code to only a single, specific list, rather than pulling everything?

     

    Thanks again for your help. It's my first time working with SharePoint via ASP.NET, so I'm really appreciate of any help I can get.

    Wednesday, December 8, 2010 5:20 PM
  • A more common call to get list items from a specific list is GetListItems http://msdn.microsoft.com/en-us/library/lists.lists.getlistitems(v=office.12).aspx. There's a code sample in the article that builds up a <Query> you also have a <ViewFields> element which is where you define the columns you want to return (please note this will need to use the internal name of the columns).


    My SharePoint Blog - http://www.davehunter.co.uk/blog
    Wednesday, December 8, 2010 6:42 PM