尝试使用Windows窗体从SharePoint获取ListItems

时间:2015-05-19 15:49:58

标签: c# sharepoint sharepoint-2010

我正在尝试创建一个使用SharePoint列表中的数据的小型Windows窗体应用程序。

我已经创建了一个服务参考,它给了我所需的所有SOAP方法。它看起来与我在SoapUI中插入WSDL的时候类似,所以我假设引用是正确的。

之后我在DAL中创建了一个类,您可以在下面查看:

public class Studies
{
    //Constants
    private const string _StudyListGuid = "{DA154DB9-1BE8-4C80-B94E-7EE4B8109905}";
    private const string _StudyViewGuid = "{909C4BED-FB5F-49E5-ABE9-375BA15BBBFF}";

    public List<Study> GetStudies()
    {
        var studyRequest = CreateRequest(); // <-- Fills in the constants in a GetListItemsRequest variable

        var soap = new ListsSoapClient();

        if (soap.ClientCredentials != null)
        {
            //soap.ClientCredentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
            //soap.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;

            soap.ClientCredentials.Windows.ClientCredential.UserName = "username";
            soap.ClientCredentials.Windows.ClientCredential.Password = "pwd";
        }

        //ERROR HAPPENS ON THIS LINE
        var studyResponse = soap.GetListItems(studyRequest.Body.listName, studyRequest.Body.viewName, studyRequest.Body.query,studyRequest.Body.viewFields,studyRequest.Body.rowLimit, studyRequest.Body.queryOptions, studyRequest.Body.webID);

        MessageBox.Show(studyResponse.ToString());

        return TranslateResponse(studyResponse); //<-- Not yet implemented
    }
}

这是我的App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="ListsSoap">
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Ntlm"/>
          </security>
        </binding>
        <binding name="ListsSoap1" />
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://website.com/_vti_bin/Lists.asmx"
          binding="basicHttpBinding" bindingConfiguration="ListsSoap"
          contract="SharepointService.ListsSoap" name="ListsSoap" />
    </client>
  </system.serviceModel>
</configuration>

现在,针对实际问题。每当我运行我的应用程序并调用GetStudies()方法时,我会收到错误消息:

An unhandled exception of type 'System.ServiceModel.Security.MessageSecurityException' occurred in mscorlib.dll

Additional information: The HTTP request is unauthorized with client authentication scheme 'Ntlm'. The authentication header received from the server was 'NTLM'.

使用InnerException:

The remote server returned an error: (401) Unauthorized.

既然已经说了所有话,我认为问题在于我发出的请求未经授权(duh)。但我认为它会......(参见Studies.cs的代码)。

所以我的第一个问题是:我是否仍然需要为请求授权,如果是,我该怎么做?

如果没有,我怎样才能获得我想要的ListItem?

我很抱歉这篇文章很长,但这是我第一次使用C#获取SharePoint数据,我不知道哪些数据真正重要,什么不是,所以我只是张贴了所有内容(我认为)。因此,如果有人对哪些信息已过时以及哪些必要信息有任何反馈,我很乐意编辑该帖子。

亲切的问候 Gravinco

0 个答案:

没有答案