将Sharepoint Web服务XML列表加载到WPF DataGrid中

时间:2015-01-04 04:06:58

标签: c# xml wpf sharepoint datagrid

我尝试使用来自Sharepoint Web Services的返回列表填充我的DataGrid,我发现的所有教程/指南都是使用XAML文件执行此操作。

这是我用来提取XML的代码,

private String GetListNode()
    {
        webService.Lists siteClient = new webService.Lists();
        System.Net.NetworkCredential passCredentials = new System.Net.NetworkCredential("username", "password", "domain");
        siteClient.Credentials = passCredentials;

        XmlNode getNode = siteClient.GetListItems("Tasks", string.Empty, null, null, string.Empty, null, null);

        return getNode.OuterXml;
    }

这将返回页面XML,在此页面中我想抓住这些项目,

ows_ID, ows_Client, ows_AssignedTo, ows_LinkTitle, ows_Status, ows_Priority, ows_DueDate

我想为DataGrid制作这些项目列,然后填充数据。所有帮助将不胜感激,谢谢你提前。

这不是完整的XML,这是一小部分,

<listitems
xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"
xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
xmlns:rs="urn:schemas-microsoft-com:rowset"
xmlns:z="#RowsetSchema"
xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<rs:data ItemCount="691">
    <z:row ows_Attachments="0" ows_ID="2108" ows_Task_x0020_Type="Issue" ows_Client="City University of New York" >
     />
</rs:data>

1 个答案:

答案 0 :(得分:1)

您可以考虑以下方法:

/// <summary>
/// Bind SP Data Source 
/// </summary>
private void BindSPDataSource()
{
    var data = GetListItems("Tasks");
    var result = XElement.Parse(data.OuterXml);
    XNamespace z = "#RowsetSchema";
    var taskItems = from r in result.Descendants(z + "row") select new
            {
                TaskName = r.Attribute("ows_LinkTitle").Value,
                DueDate = r.Attribute("ows_DueDate") != null ? r.Attribute("ows_DueDate").Value : string.Empty,
                AssignedTo = r.Attribute("ows_AssignedTo") != null ? r.Attribute("ows_AssignedTo").Value : string.Empty,
            };
    dgTasks.ItemsSource = taskItems; 
}

,其中

private XmlNode GetListItems(string listTitle)
{
    var client = new Lists.Lists();
    client.Url = webUri + "/_vti_bin/Lists.asmx";
    return client.GetListItems(listTitle, string.Empty, null, null, string.Empty, null, null);
}

XAML

<DataGrid  Name="dgTasks"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AutoGenerateColumns="False" >
     <DataGrid.Columns>
         <DataGridTextColumn Header="Task Name" Binding="{Binding TaskName}" />
         <DataGridTextColumn Header="Due Date" Binding="{Binding DueDate}"/>
         <DataGridTextColumn Header="Assigned To" Binding="{Binding AssignedTo}"/>
     </DataGrid.Columns>
 </DataGrid>

<强>结果

enter image description here