65.9K
CodeProject is changing. Read more.
Home

Access a Forms Based SharePoint Site's Web Service

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.60/5 (11 votes)

Mar 10, 2008

CPOL

2 min read

viewsIcon

113382

downloadIcon

2247

This article describes how to access the Web Services of a Forms baed authenticated SharePoint site.

Introduction

SharePoint site provides built-in Web Services with which we can access the SharePoint Object Model. To access those Web Services, we need to provide credentials to the Web Service. But if the SharePoint site uses Form based authentication, you may get the error: “The request failed with HTTP status 403: Forbidden”. It’s because Form based authentication works in a different way than others. To work this problem out, you need to use another authentication mechanism in SharePoint which is cookie based.

Concept

To get the list of available Web Services, go to IIS and expand the SharePoint web application. Then, under the node _vti_bin:

In a Form based SharePoint site, to invoke a Web Service, the user must be authenticated with the Authentication.asmx Web Service. The Web Service will return the LoginResult object which specifies the authentication status. The Web Service will not generate any exception if the authentication fails; rather the return object LoginResult will contain the login status. If authentication is successful, then the Web Service will put the credential key in the Web Services’ cookie. But to put the credential to the Web Service’s cookie, we need to initialize the cookie container of the Web Service as:

AuthenticationWSObject.CookieContainer = new CookieContainer();

You can get the cookie as follows:

Cookie cookie = cookies[loginResult.CookieName];

If the Web Service is invoked after initializing the cookie container property, the cookie will be filled with the credential token. Now, we can set the cookie in another Web Service’s cookie to access the site.

The process is shown in the following figure:

Picture2.JPG

Using the code

The given example is a desktop application with two web references to the SharePoint Web Services Authnectincation.asmx and Lists.asmx. So, put the two Web Service addresses in the textbox:

Picture3.JPG

Thanks

I faced a problem in accessing a SharePoint site with Forms based authentication. My ex-team lead Moim Hossain helped me to figure out the solution. So I personally thank him. I have taken for granted that someone will face the same problem and they may get help form this article.