Sharepoint使用Web服务

时间:2014-03-19 13:49:19

标签: c# asp.net web-services sharepoint

我在调用服务器上安装wss 3.0(我认为是Sharepoint 2007)时默认安装的asmx webservices时遇到了一些问题。

我们的sharepoint正在使用表单身份验证。 在IIS中我可以看到该网站,并且在该站点中存在虚拟目录“_vti_bin”(这是web服务所在的位置。)

我可以毫无问题地浏览到WSDL,所以我可以在我的visual studio解决方案中将它添加到我的Web引用中(我使用“web引用”而不是“服务引用”方法来添加对我的解决方案的引用)。 我无法浏览到asmx webservice本身,它会一直重定向到登录页面。

在我的代码中,我尝试使用身份验证asmx服务,但我似乎无法正确使用它。

这是我的代码:

    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;
        }

我在使用以下代码的行中收到以下错误:

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

错误:请求失败,并显示错误消息:

对象移至< HREF = “https://sharepointserverxx.com/_layouts/1033/error.aspx?ErrorText=Failed%20to%20Execute%20URL%2E” >此处

我认为我的webservice调用总是被重定向到sharepoint的登录页面。有没有人知道如何解决这个问题?

如果我取消注释以下行,我会收到另一个错误“错误:客户端发现响应内容类型为'text / html; charset = utf-8',但预计'text / xml'。” (因为响应是登录页面的html页面)

//spAuthentication.AllowAutoRedirect = true;

2 个答案:

答案 0 :(得分:1)

我很确定您的问题是由于_vti_bin需要与当前服务器和网站集相关。

这是我在类构造函数中调用的代码,用于设置我需要的所有服务。

    // 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
    }

然后,当我打电话给服务时,我这样做:

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

答案 1 :(得分:1)

您收到的错误可能有多种原因。您应该在Sharepoint中启用详细日志记录(在管理中心=&gt;操作中)以查看确切的错误。

你在IIS7上运行吗?在经典模式下,IIS7上的asmx处理程序存在一些兼容性问题。你能尝试在你的sharepoint网站的Web.Config中添加缺少的httpHandler吗?

<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>