5

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
  • 7,015
  • 12
  • 47
  • 75

4 Answers4

5

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.

Kit Menke
  • 7,046
  • 1
  • 32
  • 54
  • seems interesting, i'll try it ASAP and post up the results – Rami Alshareef Nov 22 '10 at 19:11
  • 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 Nov 23 '10 at 07:15
  • 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? – Rami Alshareef Nov 23 '10 at 07:24
  • 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. – Kit Menke Nov 23 '10 at 14:55
  • Kit, this also gave me nothing! i'm lost, how to read from the response correctly – Rami Alshareef Nov 24 '10 at 07:48
1

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']");
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • 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? – Rami Alshareef Nov 22 '10 at 14:51
  • @Rami Shareef: You mean in the default view of the list? – Kit Menke Nov 22 '10 at 14:59
  • @Kit: exactly, the default view in sharePoint site. – Rami Alshareef Nov 22 '10 at 18:44
1

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

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • 1
    Lone link is [considered a poor answer](http://stackoverflow.com/faq#deletion) since it is meaningless by itself and target resource is not guaranteed to be alive in the future. [It would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – j0k Nov 26 '12 at 17:32
0

Solution is quite simple. Using GetList() or similar functions is the wrong way to go.

Instead use GetListContentTypesAsync() to get Content ID and then get the specific ContentType using GetListContentTypeAsync(), it returns XML which includes all visible columns in sharepoint list:

var Contents = await soapListClient.GetListContentTypesAsync(list.Title, "0x01"); // 0x01 is kind of a root element for sharepoint.
String ContentID = Contents.Body.GetListContentTypesResult.Descendants().FirstOrDefault().Attribute("ID").Value.ToString();
var ContentType = await soapListClient.GetListContentTypeAsync(list.Title, ContentID);
XElement xmll = XElement.Parse(ContentType.Body.GetListContentTypeResult.ToString());
arulmr
  • 8,620
  • 9
  • 54
  • 69