Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'd like to get all list views via web service using C# code, containing the columns:

  • modified by
  • created by
  • subject
  • body
  • id
  • created
  • modified

How can this be done?

share|improve this question
add comment

3 Answers

Try the ListData.svc webservice calls using REST.

share|improve this answer
add comment

you can use GetListAndView method of http://site/_vti_bin/Lists.asmx. Click on the link for more info...

share|improve this answer
    
this only gets you one view, and you have to know its GUID. –  Derek Gusoff Nov 7 '12 at 2:05
add comment

You will need to call the _vti_bin/views.asmx GetViewCollection method That will give a list of view guids you can then use with the GetView method. The returned xml will give you a list of fields you will need to iterate through in order to check against the fields you want.

Here is a snippet of some rough code to help you. It uses an old style web reference to the Views.asmx called Views (thus the View.Views object references)

  public class ViewsWrapper
    {
        private static string viewsWebServiceSuffix = "/_vti_bin/views.asmx";
        private Views.Views views;

        public ViewsWrapper()
        {
            views = new Views.Views();
            views.Url = SharePoint.BaseSiteUrl + viewsWebServiceSuffix;
            views.UseDefaultCredentials = true;
        }

        private void moveToWeb(string webName)
        {
            var w = new WebsWrapper();
            string url = w.GetWebUrl(webName);
            this.views.Url = url + viewsWebServiceSuffix;
        }  

        public List<string> GetViewsWithColumns(string webName, string listName)
        {
            var result = new List<string>();
            moveToWeb(webName);

            XmlNode xresult = views.GetViewCollection(listName);
            XmlNodeList items = SharePoint.XpathQuery(xresult, @"/sp:Views/sp:View");
            foreach (XmlNode xNode in items)
            {
                string viewName = xNode.Attributes["Name"].Value;
                XmlNode xmlView = views.GetView(listName, viewName);
                XmlNodeList field = XpathQuery(xmlView, @"/sp:View/sp:ViewFields/sp:FieldRef[@Name='LinkFilename' or @Name='ContentType' or @Name='DocumentType']");
                if (field.Count == 3)
                {
                    result.Add(listName + ":" + xNode.Attributes["DisplayName"].Value);
                }
            }
            return result;
        }

        public static XmlNodeList XpathQuery(XmlNode xmlToQuery, string xPathQuery)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xmlToQuery.OuterXml);
            XmlNamespaceManager mg = new XmlNamespaceManager(doc.NameTable);
            mg.AddNamespace("sp", "http://schemas.microsoft.com/sharepoint/soap/");
            mg.AddNamespace("z", "#RowsetSchema");
            mg.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");
            mg.AddNamespace("y", "http://schemas.microsoft.com/sharepoint/soap/ois");
            mg.AddNamespace("w", "http://schemas.microsoft.com/WebPart/v2");
            mg.AddNamespace("d", "http://schemas.microsoft.com/sharepoint/soap/directory");
            return doc.SelectNodes(xPathQuery, mg);
        }

    }
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.