Search code examples
c#web-servicessharepointsharepoint-2007

Get SharePoint List Visible Columns Names through Web Service using C#


I want to get all visible columns (Hidden == false) for specific list in sharePoint site, I tried to look through the SharePointWebService.Lists.GetList(listName), but couldn't find anything useful, also checked the methods provided by the Lists WebService and also nothing new,

Please advice.


Solution

  • You can use the GetListAndView method of the Lists web service to get the schemas for the list and a view.

    From the documentation, if you leave the viewName parameter empty, the default view will be returned. Then, you can read the <ViewFields></ViewFields> node for the list of fields.

    *Edit*

    Turns out using XPath to query the returned XML was tougher than I thought... here is what I came up with:

    XmlNode result = webService.GetListAndView("My Pictures", string.Empty);
    
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(result.OwnerDocument.NameTable);
    nsmgr.AddNamespace("sp", "http://schemas.microsoft.com/sharepoint/soap/");
    
    string xpathQuery = "sp:View/sp:ViewFields/sp:FieldRef";
    XmlNodeList nodes = result.SelectNodes(xpathQuery, nsmgr);
    
    for (int i = 0; i < nodes.Count; i++)
    {
        Console.WriteLine(nodes[i].Attributes["Name"].Value);
    }
    

    Looks like you have to have a XmlNamespaceManager otherwise your query always returns no values. Something about specifying the namespace... Here is a good reference.