Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 529
Rami Alshareef Avatar asked Nov 22 '10 13:11

Rami Alshareef


3 Answers

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.

like image 145
Kit Menke Avatar answered Nov 16 '22 23:11

Kit Menke


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']");
like image 29
Frédéric Hamidi Avatar answered Nov 17 '22 01:11

Frédéric Hamidi


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

like image 1
Nipesh Shah Avatar answered Nov 17 '22 01:11

Nipesh Shah