Solved

XML inquiry , hide not needed columns like ows_MetaInfo

Posted on 2014-04-07
4
467 Views
Last Modified: 2014-04-11
Im trying to run a query on a sharepoint List and my goal here is to only return 4 columns Title, Region, Category and URL however im getting alot of columns that i dont need ows_MetaInfo, ows_PermMask etc. Any help would be appreciated
thanks

 SPPlaySiteLists1.Lists listService = new SPPlaySiteLists1.Lists();
            
            
            listService.Credentials =
                System.Net.CredentialCache.DefaultCredentials;

            /*Set the Url
                "http://mylist/_vti_bin/Lists.asmx";

            /* Instantiate an XmlDocument object */
            System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();

          
            string listName = "List Name";
            //string viewName = "{B9764C7C-9AF2-4062-9A8E-FA7F4299DD32}";
            //string rowLimit = "150";

      
            System.Xml.XmlElement query = xmlDoc.CreateElement("Query");
            System.Xml.XmlElement viewFields =  xmlDoc.CreateElement("ViewFields");
            System.Xml.XmlElement queryOptions =   xmlDoc.CreateElement("QueryOptions");


            query.InnerXml = "<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns>" +
         "<DateInUtc>TRUE</DateInUtc>";

            viewFields.InnerXml = "<FieldRef Name='Title' />" + "<FieldRef Name='Region' />" + "<FieldRef Name='Category' />" + "<FieldRef Name='URL' />";
            queryOptions.InnerXml = "<IncludeMandatoryColumns>False</IncludeMandatoryColumns>";
            

            System.Xml.XmlNode nodeListItems =
                listService.GetListItems
                (listName, null, query, viewFields, null, queryOptions, null);

            /*Loop through each node in the XML response and display each item.*/

            System.IO.StringReader sr = new System.IO.StringReader(nodeListItems.OuterXml);
            XmlTextReader tr = new XmlTextReader(sr);
            DataSet ds = new DataSet("resultDataSet");
            ds.ReadXml(tr);
            if(ds != null && ds.Tables.Count >= 2)
            {
                this.dataGridView1.DataSource = ds.Tables[1];
            }

Open in new window

0
Comment
Question by:kentrg11
  • 2
4 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 39984209
You can set the datasource to that and just have your grid bind to the columns that are needed.  It won't hurt anything and in fact makes the next person's life easier when they need to add another column.

Turn off your auto generate and add bound columns. Example here:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.datapropertyname.aspx
0
 

Author Comment

by:kentrg11
ID: 39984297
Is there an easier route without binding?
0
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 250 total points
ID: 39984453
I guess you could select it to a new datatable . . . but you're wasting a lot of resources because you're essentially looping over the entire table again, getting the columns you need, and pointing the datasource as the copied source.    VS just getting the copy source once and then displaying what you need to display.
var data = ds.Tables[1].AsEnumerable().Select
            (r => new
            {

                 //Column Name = r.Field<TYPE>("TableColumnName")
                  //eg:

                Title = r.Field<string>("Title"),
                Region = r.Field<string>("Region")
  //other columns

                      
                         }).ToList();
    this.dataGridView1.DataSource = data;

Open in new window

0
 
LVL 15

Assisted Solution

by:colly92002
colly92002 earned 250 total points
ID: 39985748
You could create a XPathNavigator to select the nodes you want and and iterate over them, choosing each field you want?

Eg something like this:

// Create navigator   
XPathNavigator navigator = ndListItems.CreateNavigator();
// Set up namespace manager for XPath   
XmlNamespaceManager ns = new XmlNamespaceManager(navigator.NameTable);
ns.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");
ns.AddNamespace("z", "#RowSetSchema");
 XmlNodeList nodes = ndListItems.SelectNodes("//*[name()='z:row'] ", ns);

foreach (XmlNode node in nodes)
{
                    // Get the ID and put it in a datatable...
                    DataRow dr = _dt.NewRow();
                    string id = node.Attributes["ows_ID"].InnerText;
                    dr["ID"] = id;
                    /// Do whatever else you want to do....
}

Open in new window

0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Cognos Reporting - SharePoint Upgrade 1 16
MailAddress in vb 4 29
Customizing XML with Powershell 2 23
Adding where clause to entity statement c# 1 21
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question