Sharepoint 2010– FBA et Windows Phone 7

Posted on

Faisant suite au précédent article (ICI) permettant de mettre en place la configuration de Sharepoint 2010 en mode FBA depuis un Active Directory, qui est à mon sens le plus lourd dans la démarche voici un sample code permettant de se connecter à Sharepoint et de récupérer les infos d’une liste.

 

Il vous faudra dans un premier temps ajouter la référence à votre service web  depuis Visual Studio  et modifier le ClientConfig de celui-ci comme-ci :

clientconfig

 

Côté XAML : Une simple Listbox pour les tests.

Maintenant le code :

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using Microsoft.Phone.Controls;
using PhoneApp1FBA.WebServiceLists;

namespace PhoneApp1FBA
{
    public partial class MainPage : PhoneApplicationPage
    {
        //Cookie Container et Event Handler
        CookieContainer _cookiecontainer = new CookieContainer();
        public event EventHandler OnAuthenticated;

       //Classe de liste
        public class Demo
        {
            public string Title { get; set; }
            public override string ToString(){return Title;}
        }
      
        public MainPage()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            Authenticate();
            this.OnAuthenticated += new EventHandler(MainPage_OnAuthenticated);
        }

        void MainPage_OnAuthenticated(object sender, EventArgs e)
        {
            GetLists();
        }

        private void GetLists()
        {
            ListsSoapClient lists = new ListsSoapClient();
            lists.CookieContainer = _cookiecontainer;
            lists.GetListItemsCompleted += 
            new EventHandler<GetListItemsCompletedEventArgs>(lists_GetListItemsCompleted);
            lists.GetListItemsAsync(
                "Demo",             //List Name
                String.Empty,       //View Name
                null,               //query
                null,               //view fields
                null,               //row limit
                null,               //query options
                null);              //webID

        }


        void lists_GetListItemsCompleted(object sender, GetListItemsCompletedEventArgs e)
        {
            IEnumerable<XElement> rows = e.Result.Descendants(XName.Get("row", "#RowsetSchema"));
            IEnumerable<Demo> demos = from element in rows select new Demo(){
                Title = (string)element.Attribute("ows_LinkTitle"),};
           
           Deployment.Current.Dispatcher.BeginInvoke(()=>
                {
                    listBox1.ItemsSource = demos;
                          
               });

        }


        #region Authentification Sur Sharepoint
        private void Authenticate()
        {
            System.Uri authServiceUri = 
             new Uri("http://crm2011/sites/fba/_vti_bin/authentication.asmx");

            HttpWebRequest spAuthReq = HttpWebRequest.Create(authServiceUri) as HttpWebRequest;
            spAuthReq.CookieContainer = _cookiecontainer;
            spAuthReq.Headers["SOAPAction"] = "http://schemas.microsoft.com/sharepoint/soap/Login";
            spAuthReq.ContentType = "text/xml; charset=utf-8";
            spAuthReq.Method = "POST";
            spAuthReq.BeginGetRequestStream(new AsyncCallback(spAuthReqCallBack), spAuthReq);
        }

        private void spAuthReqCallBack(IAsyncResult asyncResult)
        {
            string envelope =
                    @"<?xml version=""1.0"" encoding=""utf-8""?>
                    <soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" 
                        xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" 
                        xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
                      <soap:Body>
                        <Login xmlns=""http://schemas.microsoft.com/sharepoint/soap/"">
                          <username>{0}</username>
                          <password>{1}</password>
                        </Login>
                      </soap:Body>
                    </soap:Envelope>";

            UTF8Encoding encoding = new UTF8Encoding();
            HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
            Stream _body = request.EndGetRequestStream(asyncResult);
            envelope = string.Format(envelope, "demofba", "p@ssword01");//VOS IDENTIFIANTS
            byte[] formBytes = encoding.GetBytes(envelope);

            _body.Write(formBytes, 0, formBytes.Length);
            _body.Close();

            request.BeginGetResponse(new AsyncCallback(ResponseCallback), request);
        }

        private void ResponseCallback(IAsyncResult asyncResult)
        {
            HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);
            Stream content = response.GetResponseStream();

            if (request != null && response != null)
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    using (StreamReader reader = new StreamReader(content))
                    {
                        //Code de débogage
                        string _responseString = reader.ReadToEnd();
                        reader.Close();
                    }
                }
            }
            //retour authentification
           OnAuthenticated(null, null);
            
        }
        #endregion
     
      
    }
}
Advertisements

One thought on “Sharepoint 2010– FBA et Windows Phone 7

    Helene said:
    December 26, 2011 at 6:28 pm

    All of my qusetions settled-thanks!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s