使用SharePoint REST使用C#添加列表项

时间:2014-10-06 18:21:49

标签: c# rest sharepoint-2013

我有一个托管在一台服务器上的应用程序,该服务器不属于我的SharePoint场。我需要它将新列表项发布到SharePoint列表。默认SharePoint区域(将发布到的位置)配置为使用声明身份验证,集成Windows身份验证。

运行该应用程序的标识对我尝试发布的列表具有完全管理访问权限。我每次发帖都会得到一个禁止的#34; 403#34;错误。如果我删除了endPointRequest.credentials行,我会收到401错误。

    var data = "{'__metadata':{'type':'SP.Data.SpecialListItem'}, 'Special':'" + txtSpecial + "'}";

    HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create("http://portal/_api/web/List/GetByTitle('Special')");
    endpointRequest.Credentials = CredentialCache.DefaultNetworkCredentials;
    endpointRequest.Method = "POST";
    endpointRequest.Accept = "application/json;odata=verbose";
    endpointRequest.ContentType = "application/json;odata=verbose";

    endpointRequest.ContentLength = data.Length;
    StreamWriter writer = new StreamWriter(endpointRequest.GetRequestStream());
    writer.Write(data);
    writer.Flush();

    using (HttpWebResponse endpointResponse = (HttpWebResponse)endpointRequest.GetResponse())
    {
        using (StreamReader reader = new StreamReader(endpointResponse.GetResponseStream()))
        {
            string result = reader.ReadToEnd();
        }
    }

0 个答案:

没有答案