Windows store 应用调用 SharePoint Service
本人之前并没有开发SharePoint 的相关经验不够最近做了一个 Windows store 和 SharePoint Service的小工程多亏朋友们帮忙,在这里总结一下经验供大家参考。
首先ShearPoint每个Page或者WebSite都有自身强大的WebService或WCF。 只需要在URL 站点目录后面加上 /_vti_bin/Lists.asmx
这里有一个重要的问题就是, Windows store 应用在自动生成引用代码的时候会错误生成一些http://******/_vti_bin/Lists.asmx,而实际我们添加的地址是: http://******/sites/*******/_vti_bin/Lists.asmx所以我需要在项目中使用VS的替换功能把错误的地址替换掉。
引用完成后不要忘记在 appxmanifest 文件中勾选 Private Network 和 Enterprise Authentication选项 因为我这个工程是在公司域中可以使用windows 集成验证方法登陆.
另外我提供一下调用service的方法 其中System.ServiceModel.Security.MessageSecurityException 这个异常是用户没有加入域需要用户名密码验证的错误,System.ServiceModel.EndpointNotFoundException 是网络连接错误。
其次获取一张表单的内容是调用 GetListItemsAsync 方法.
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; }
实际对应的就是SharePoint 中每个Item中的数值.
用法比较简单放在这里为以后要用的同学铺路吧