Rise_against
Rise_against

Reputation: 1060

Sharepoint consume webservices

I am having some issues with calling the asmx webservices that are installed by default when installing the wss 3.0 (Sharepoint 2007 I think) on the server.

Our sharepoint is using forms authentication. In IIS I can see the website and in that site the virtual directory "_vti_bin" exists (this is were the webservices are located.)

I can browse without problems to the WSDL, so I can add it to my web references in my visual studio solution (I used the "web reference" instead of "service reference" method to add the reference to my solution). I can't browse to the asmx webservice itself, it keeps redirecting to the login page.

In my code, I tried to use the Authentication asmx service, but I can't seem to consume it correctly..

This is my code:

    protected Cookie _authCookie;
    CookieContainer _cookieContainer;

    using (Authentication spAuthentication = new Authentication())
        {
            spAuthentication.Url = "https://sharepointserverxx.com/_vti_bin/Authentication.asmx";
            spAuthentication.CookieContainer = new CookieContainer();
            //spAuthentication.AllowAutoRedirect = true;
            spAuthentication.PreAuthenticate = true;

            LoginResult loginResult = spAuthentication.Login(txtUser.Text, txtPassword.Text);
            _authCookie = new Cookie();

            if (loginResult.ErrorCode == LoginErrorCode.NoError)
            {
                CookieCollection cookies = spAuthentication.CookieContainer.GetCookies(new Uri(spAuthentication.Url));
                _authCookie = cookies[loginResult.CookieName];
                _cookieContainer = new CookieContainer();
                _cookieContainer.Add(_authCookie);
                return true;
            }
            else
                txtResult.Text = "Invalid Username/Password while authenticating to the sharepoint webservices.";
            return false;
        }

I am getting the following error on the line with the following code:

LoginResult loginResult = spAuthentication.Login(txtUser.Text, txtPassword.Text);

Error: The request failed with the error message:

Object moved to < href="https://sharepointserverxx.com/_layouts/1033/error.aspx?ErrorText=Failed%20to%20Execute%20URL%2E">here

I think that my webservice call is always redirected to the login page of sharepoint.. Does anyone have an idea how I can fix this?

If I uncomment the following line, I get another error "Error: Client found response content type of 'text/html; charset=utf-8', but expected 'text/xml'." (because the response is the html page of the login page)

//spAuthentication.AllowAutoRedirect = true;

Upvotes: 1

Views: 2473

Answers (2)

emp
emp

Reputation: 5065

The error you are receiving might have multiple causes. You should enable Verbose logging in Sharepoint (in Central Administration => Operations) to see the exact error.

Are you running on IIS7? There are some compatibility problems with asmx handlers on IIS7 in Classic mode. Can you try adding missing httpHandlers in the Web.Config of your sharepoint website?

<system.web><httpHandlers>
<remove verb="*" path="*.asmx" /> 

<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> 

<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> 


<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false" /> 

<add verb="*" path="_vti_bin/ReportServer" type="Microsoft.ReportingServices.SharePoint.Soap.RSProxyHttpHandler, RSSharePointSoapProxy, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" /> 

<add verb="*" path="Reserved.ReportViewerWebPart.axd" type="Microsoft.ReportingServices.SharePoint.UI.WebParts.WebPartHttpHandler, Microsoft.ReportingServices.SharePoint.UI.WebParts, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" /> 
</httpHandlers></system.web>

Upvotes: 1

Amicable
Amicable

Reputation: 3091

I'm pretty sure your issue is due to the to _vti_bin needing to be relative to the current server and site collection.

This is code I call in my class constructor to set up all the services I need.

    // Server URL is the root server E.G.
    // I set mine in class constructor
    String serverUrl = "http://192.168.100.10:1234/";

    /// <summary>
    /// Web referenced must be relative to the site collection you are seeking to query
    /// </summary>
    /// <param name="siteCollectionUrl"></param>
    private void SetWebReferencePaths(string siteCollectionUrl)
    {
        string siteUrl = serverUrl;

        if (!siteUrl.EndsWith("/"))
            siteUrl += "/";

        if (!siteCollectionUrl.EndsWith("/"))
            siteCollectionUrl += "/";

        string fullPath = "";

        if (siteUrl != siteCollectionUrl)
        {
            fullPath = siteUrl + siteCollectionUrl;
        }
        else
        {
            fullPath = siteUrl;
        }

        listServiceUrl        = fullPath + "_vti_bin/Lists.asmx";
        groupsServiceUrl      = fullPath + "_vti_bin/UserGroup.asmx";
        permissionsServiceUrl = fullPath + "_vti_bin/Permissions.asmx";
        siteDataServiceUrl    = fullPath + "_vti_bin/SiteData.asmx";
        websServiceUrl        = fullPath + "_vti_bin/Webs.asmx";
        listServiceUrl        = fullPath + "_vti_bin/Lists.asmx";
        groupsServiceUrl      = fullPath + "_vti_bin/UserGroup.asmx";
        permissionsServiceUrl = fullPath + "_vti_bin/Permissions.asmx";
        siteDataServiceUrl    = fullPath + "_vti_bin/SiteData.asmx";
        websServiceUrl        = fullPath + "_vti_bin/Webs.asmx";
        // result would look like
        // http://sharep2007/SiteDirectory/FirstSite/_vti_bin/Lists.asmx
        // It's broken down like this
        // http://sharep2007/ + SiteDirectory/FirstSite/ + _vti_bin/Lists.asmx
        // serverURL + siteCollectionURL + _vti_bin/Lists.asmx
    }

Then when I call the a service I do it like this:

using (ShareLists.Lists listService = new Lists())
{
    listService.Url = listServiceUrl;
    // Code using service
}

Upvotes: 1

Related Questions