本人之前並沒有開發SharePoint 的相關經驗不夠最近做了一個 Windows store 和 SharePoint Service的小工程多虧朋友們幫忙,在這裡總結一下經驗供大家參考。
升級到WP8必需知道的13個特性 系列文章目錄地址:http://www.linuxidc.com/Linux/2013-08/89003.htm
首先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中的數值.
用法比較簡單放在這裡為以後要用的同學鋪路吧