private async Task<XElement> GetDataFromService(string serviceName, string userName = null, string password = null, string domain = null)
{
SPService.ListsSoapClient client = new SPService.ListsSoapClient();
var binding = ((BasicHttpBinding)client.Endpoint.Binding);
binding.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.TransportCredentialOnly;
binding.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Ntlm;
client.ClientCredentials.Windows.ClientCredential = new NetworkCredential(userName, password, domain);
GetListItemsResponse lists = null;
try
{
lists = await client.GetListItemsAsync(serviceName, string.Empty, null, null, "1000", null, null);
}
catch (System.ServiceModel.Security.MessageSecurityException e)
{
throw new MessageSecurityException("Please check your user name and password.");
}
catch (System.ServiceModel.EndpointNotFoundException e)
{
throw new EndpointNotFoundException("Please check your Microsoft network connection and access permissions.");
}
return lists.Body.GetListItemsResult;
}
前面的方法会返回一个XElement 需要我们手动解析不过也很简单.
public async Task<ObservableCollection<DashBoard>> GetDashBoard(string userName = null, string password = null, string domain = null)
{
XElement xml = await GetDataFromService("DashBoardTable", userName, password, domain);
var items = xml.Elements().Elements().ToList();
var result = from o in items
select new DashBoard()
{
Department = o.Attribute("ows_Department").GetStringFromXMLAttribute(),
Attained = o.Attribute("ows_Attained").GetStringFromXMLAttribute(),
Target = o.Attribute("ows_Target").GetStringFromXMLAttribute(),
};
ObservableCollection<DashBoard> List = new ObservableCollection<DashBoard>(result);
return List;
}