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

10,970

Solution 1

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.

Solution 2

I used the above code but, after a long search I found the solution to get all or custom columns from the sharepoint list. The code for that is shared on ..

Custom Columns or ALL Columns

Solution 3

The GetList() method returns a CAML fragment that includes the list's field (column) definitions. You might want to try an XPath expression:

XmlNode list = yourListService.GetList("yourListName");
XmlNodeList visibleColumns
    = list.SelectNodes("Fields/Field[not(@Hidden) or @Hidden='FALSE']");
Share:
10,970

Related videos on Youtube

Rami Alshareef
Author by

Rami Alshareef

Experienced and enthusiast Full Stack Software Engineer with passion toward best practices, collaborative work, Agile culture, clean code, and software architecture. Experienced with all stages of software development including QA and DevOps. I enjoy aligning business needs with software engineering and always seek to expand my engineering area of expertise and influence . Forward thinker and strategic planner, with aptitude for learning new skills. Team oriented with hands on scrum values and practices. It is a true privilege and beyond rewarding to work at a place that influences thousands of developers around the world. Rami's experiences include but are not limited to: • Scrum, Agile framework, and collaborative work • Restful &amp; web api, Solid OOP, DevOps • Software architecture, design, development, deployment process, and QA • Research and implementation • Database architecture and design

Updated on June 04, 2022

Comments

  • Rami Alshareef
    Rami Alshareef almost 2 years

    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.

  • Rami Alshareef
    Rami Alshareef over 13 years
    I've been looking at 'List' innerXml and I just noticed that there is no attribute Hidden="TRUE" :S I'll rephrase my question: how can I read all columns that are appear to user in the list when he/she opens the list through the sharePoint site?
  • Kit Menke
    Kit Menke over 13 years
    @Rami Shareef: You mean in the default view of the list?
  • Rami Alshareef
    Rami Alshareef over 13 years
    @Kit: exactly, the default view in sharePoint site.
  • Rami Alshareef
    Rami Alshareef over 13 years
    seems interesting, i'll try it ASAP and post up the results
  • Rami Alshareef
    Rami Alshareef over 13 years
    ViewFields Return the names of the fields,so i had to re-featch every field by its name to get its displaying name, and the second issue that the ViewFields not shown all fields that displayed in the sharepoint default view for that list (If the list is a picture list)! any ideas?
  • Rami Alshareef
    Rami Alshareef over 13 years
    I tried this to get Fields, System.Xml.XmlNode list = SPListWebService.GetListAndView(xmlnode.Attributes["Title"].‌​Value,string.Empty); System.Xml.XmlNodeList visibleColumns = list.SelectNodes("View/ViewFields/FieldRef");.... where did i got wrong?
  • Kit Menke
    Kit Menke over 13 years
    Rami, looks like you're right. The ViewFields node contains all the FieldRefs which are the internal column names. When I try it, I get: SelectedFlag, DocIcon, NameOrTitle, ImageSize, FileSizeDisplay, and RequiredField. You could then lookup more info about these fields in the first part of the response: ListAndView/List/Fields.
  • Rami Alshareef
    Rami Alshareef over 13 years
    Kit, this also gave me nothing! i'm lost, how to read from the response correctly
  • j0k
    j0k over 11 years
    Lone link is considered a poor answer since it is meaningless by itself and target resource is not guaranteed to be alive in the future. It would be preferable to include the essential parts of the answer here, and provide the link for reference.

Related