A recent MSDN forums question prompted this posting.  Since SharePoint has all these built in web services it is extremely easy to integrate SharePoint with other systems, LOTS of other systems.  Typically people use the NTLM SharePoint site and thus typically don’t worry all too much about authentication of their code when it comes to the SharePoint web services.  Sometimes though, for whatever reason, perhaps you don’t own the SharePoint environment, etc. but your SharePoint site ONLY uses FBA, and thus your stuck with trying to call the SharePoint web services using FBA.  At first this might seem like a daunting task but in reality its really no different/harder than the typical way you would call a SharePoint web service.  The difference is mainly that you need to first call an authentication web service, and then use the cookie result you get back in all your subsequent web service calls.  Enough Rambling…BRING ON THE CODE!!!

Pre Reqs:

Code:

I created a simple console program project and added 2 web references to it:

//authenticate to the web services of a SharePoint FBA site
using (AuthenticationSvc.Authentication authSvc = new AuthenticationSvc.Authentication())
{
    authSvc.Url = @"http://winsvr2003base:14059/_vti_bin/authentication.asmx";
    authSvc.CookieContainer = new System.Net.CookieContainer();     //create a new cookie container
    authSvc.AllowAutoRedirect = true;
 
    //set the FBA login information
    AuthenticationSvc.LoginResult result = authSvc.Login("triplet", "P@ssw0rd");
 
    //check our loginresult to make sure that we don't have any errors
    //if we don't have any errors, then consider us authenticated and we can then call our
    //other SharePoint web services by passing in our authentication cookie
    if (result.ErrorCode == AuthenticationSvc.LoginErrorCode.NoError)
    {
        try
        {
            //now that we're authenticated through FBA try and call the lists web service
            using (ListsSvc.Lists listSvc = new SharePointFBAWebSvcTester.ListsSvc.Lists())
            {
                listSvc.Url = @"http://winsvr2003base:14059/_vti_bin/lists.asmx";
                
                //set our authentication cookie that we got above
                listSvc.CookieContainer = authSvc.CookieContainer;  
                
                //get the lists from the site
                XmlNode listCol = listSvc.GetListCollection();
 
                //dump all the lists in our site
                Console.WriteLine(listCol.InnerXml);
            }
        }
        catch (Exception ex)
        {
           Console.WriteLine("Exception occured while calling lists.asmx" + ex.Message);
        }
    }
    else
        Console.WriteLine("Error authentication to web service.");
}

*** AuthenticationSvc is the name of the web service reference I added to Authentication.asmx

*** ListsSvc is the name of the web service reference I added to Lists.asmx


Pretty simple, huh?  With the basic code above, you should be able to authenticate to the FBA site, and then call whatever SharePoint web service you want.


Posted in:   Tags:
Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2017 Tony Testa's World