Windows store 應用調用 SharePoint Service

來源:互聯網
上載者:User

本人之前並沒有開發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中的數值.

用法比較簡單放在這裡為以後要用的同學鋪路吧

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.