Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C#

Access a Forms Based SharePoint Site's Web Service

Rate me:
Please Sign up or sign in to vote.
4.60/5 (12 votes)
9 Mar 2008CPOL2 min read 112K   2.2K   21  
This article describes how to access the Web Services of a Forms baed authenticated SharePoint site.
using System;
using System.Collections.Generic;
using System.Text;
using SharePointWebServiceAccess.SPAuthentication;
using SharePointWebServiceAccess.SPList;
using System.Net;
using System.Xml;
using System.Web.Services.Protocols;

namespace SharePointWebServiceAccess
{
    public class SharePointWSManager
    {
        public string QueryToSharePoint(string authenticationWSAddress, string userName, string password, string listWSAddress)
        {
            string response = string.Empty;
            try
            {
                Authentication spAuthentication = new Authentication();
                spAuthentication.Url = authenticationWSAddress;
                spAuthentication.CookieContainer = new CookieContainer();
                Lists spLists = new Lists();
                spLists.Url = listWSAddress;
                //Try to login to SharePoint site with Form based authentication
                LoginResult loginResult = spAuthentication.Login(userName, password);
                Cookie cookie = new Cookie();
                //If login is successfull
                if (loginResult.ErrorCode == LoginErrorCode.NoError)
                {
                    //Get the cookie collection from the authenticatin web service
                    CookieCollection cookies = spAuthentication.CookieContainer.GetCookies(new Uri(spAuthentication.Url));
                    //Get the specific cookie which contains the security token
                    cookie = cookies[loginResult.CookieName];
                    //Initialize the cookie container of the list web service
                    spLists.CookieContainer = new CookieContainer();
                    //set the cookie of list web service to the authenticatio cookie
                    spLists.CookieContainer.Add(cookie);
                    XmlNode responseNode = spLists.GetListCollection();
                    response = responseNode.InnerXml;
                }
            }
            catch (SoapException exp)
            {
                return "SOAP ERROR: " + Environment.NewLine + exp.Message;
            }
            catch (Exception exp)
            {
                return "ERROR: " + Environment.NewLine + exp.Message;
            }
            return response;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect ImpleVista Aps
Denmark Denmark
Sohel has more than six years of experience in professional software development with extensive involvement in Web based Object-Oriented, Multi-Tiered application design and development. He's Familiar with Test Driven Development (TDD) and refactoring techniques as well as having expertise in architecturing large enterprise applications. He has Experience in working with Content Management System and Portal Management System tools like SharePoint, DotNetNuke, Ektron.

Over last few years, he’s involved in development with projects on Microsoft SharePoint and received Microsoft MVP for SharePoint Server Development in the year 2011 and 2012. Currently he's working in a software company located Copenhagen,Denmark on a project integrating SharePoint and SAP. You can read his popular blog at: http://ranaictiu-technicalblog.blogspot.com

Comments and Discussions