Wednesday, August 8, 2012

SharePoint 2010 get list items using list.asmx service in C#

 

SharePoint has many services and list.asmx is one service which lets you deals with its list and document libraries. In following example we will see how to call GetListItems method in list.asmx service

Lets start by creating a Console application in VS 2010. We will name this project as ConsoleApplication1

Next thing we will add is the reference to service list.asmx

image

Above service reference will also add a app.config file. We need to edit some its setting for our NTLM SharePoint site.

<security mode="None">
<
transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<
message clientCredentialType="UserName" algorithmSuite="Default" />
</
security>

After changes we should have something like following

<security mode="TransportCredentialOnly">
<
transport clientCredentialType="Ntlm" />
<!--
<message clientCredentialType="UserName" algorithmSuite="Default" />-->
</
security>

Above changes will help you get past following error


The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'NTLM'.


Now we will write implement our service call. We will query list with name TestList. GetListItems method will return all the items under request list in XML format.





static void Main(string[] args)
{
const string listName = "TestList";

var client = new ListServiceProxy.ListsSoapClient();
var query = new XElement("Query", "");
var viewFields = new XElement("ViewFields", "");
var queryOptions = new XElement("QueryOptions", "");


var result = client.GetListItems(listName, null, query, viewFields, null, queryOptions, null);

Console.WriteLine(result);
}

Following will be the output of above code


image

4 comments:

  1. Hi i am getting an exception please help..
    Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown.

    ReplyDelete
  2. Hi Varun,

    First thing you can make sure is that you run the code as service account or farm admin account. Next make sure the listname you are passing exists in the root site and not in any sub site.

    Do you see more details in ex.Detail.InnerText?

    ReplyDelete
  3. Hi Ojas Thanks for the reply.
    i have added this line
    listService.Url = "http://something/something/_vti_bin/lists.asmx";
    to the code before calling
    var ndListItems = listService.GetListItems("Posts", null, null, viewFileds, null, null, null);

    and now no error i can retrieve the columns.
    but what this line has does i dont know. i dont want to hard code the url.
    one difficulty now i am facing is that one of the column is multi line and while retieving that the text comes with HTML..but i only want the content..If i am able to make u understand my problem?

    ReplyDelete
  4. Hi Varun,

    Does your multiline column support rich text. If yes then you will get HTML while reading it. If your multiline column was created as Plain text then you will get plain text.

    ReplyDelete