3

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?

0

3 Answers 3

2

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);
        }

    }
1

Try the ListData.svc webservice calls using REST.

1

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

1
  • this only gets you one view, and you have to know its GUID. Commented Nov 7, 2012 at 2:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.