Press "Enter" to skip to content

SharePoint meets LINQ to XML: Building a CAML Query and calling Lists.asmx

I’ve been playing around with LINQ to XML and Silverlight recently, and it is absolutely awesome! The amount of code you have to write compared to working with XML DOM objects like XmlDocument is extremely minimised.

Here is an example on calling the GetListItems() method of Lists.asmx. I am returning the top 100 items in a Task list that are created by a particular user. This code example should be compatible for both WSS v3 and MSF v4.

XElement query = new XElement(“Query”,
        new XElement(“Where”,
            new XElement(“Eq”,
                new XElement(“FieldRef”, new XAttribute(“Name”, “Author”), new XAttribute(“LookupId”, “True”)),
                new XElement(“Value”, new XAttribute(“Type”, “User”), userID)

XElement queryOptions = new XElement(“QueryOptions”);
XElement viewFields = new XElement(“ViewFields”);

_listService.GetListItemsAsync(“Tasks”, null,
    query, viewFields, “100”, queryOptions, null, null);

userID is the SharePoint user value within the current SPSite. You can get this with the usergroup.asmx service.

Hope this helps!