C# 尝试使用Windows窗体从SharePoint获取列表项

C# 尝试使用Windows窗体从SharePoint获取列表项,c#,sharepoint,sharepoint-2010,C#,Sharepoint,Sharepoint 2010,我正在尝试创建一个使用SharePoint列表中的数据的小型Windows窗体应用程序 我已经创建了一个服务引用,它提供了我需要的所有SOAP方法。它看起来与我在SoapUI中插入WSDL时类似,因此我假设引用是正确的 之后,我在DAL中创建了一个类,您可以在下面查看: public class Studies { //Constants private const string _StudyListGuid = "{DA154DB9-1BE8-4C80-B94E-7EE4B81

我正在尝试创建一个使用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
    }
}
但有一个例外是:

The remote server returned an error: (401) Unauthorized.
既然已经说了这么多,我想问题是我发出的请求没有得到授权(duh)。但我以为会是。。(参见Studies.cs中的代码)

因此,我的第一个问题是:我是否仍然需要为请求授权,如果需要,我该如何做

如果没有,我如何才能获得我想要的列表项

我很抱歉写了这么长的文章,但这是我第一次使用C#获取SharePoint数据,我不知道什么数据真正重要,什么不重要,所以我只是发布了所有内容(我想)。因此,如果有人对哪些信息是过时的,哪些是必要的有一些反馈,我很乐意编辑这篇文章

问候
Gravinco

您有
if(soap.ClientCredentials!=null)
,但是如果它为null呢?是否要创建凭据对象?您是否调试并显示您的代码通过“if”语句并填写用户名/密码?@RonBeyer是的,我有,它有。“检查是否为空”只是因为reSharper建议了它,我仍然需要重构该部分,但我希望它首先工作:-/
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'.
The remote server returned an error: (401) Unauthorized.