SharePoint 2013:为什么在创建clientContext时出现500错误?

时间:2019-07-02 06:32:15

标签: c# wcf sharepoint

我想创建一个IIS Web服务,该服务必须将列表项写入Premise上的SharePoint。

我想使用CSOM并尝试创建ClientContext。

public string AddListItem()
string result = string.Empty;
string siteUrl = "https://serverUrl";

using (ClientContext context = new ClientContext(siteUrl))
        {
            context.Credentials = new NetworkCredential("User", "Password", "Domain");
            List list = context.Web.Lists.GetByTitle("Test");
            context.Load(list);
            context.ExecuteQuery();
        }

        return result;
    }

执行时,我在context.ExecuteQuery();处出错

  

System.Net.WebException:“远程服务器返回错误:(500)内部服务器错误。”

在事件日志中,看到以下错误:

  

WebHost无法处理请求。    发件人信息:System.ServiceModel.ServiceHostingEnvironment + HostingManager / 41028758    异常:System.ServiceModel.ServiceActivationException:由于编译期间发生异常,因此无法激活服务“ /_vti_bin/client.svc”。异常消息是:由于对象的当前状态,操作无效。---> System.InvalidOperationException:由于对象的当前状态,操作无效。      在Microsoft.SharePoint.Administration.SPWebApplication.get_Context()   ................................

在调试中,我还看到在创建ClientContext之后和context.ExecuteQuery()之前;在ClientContext的某些属性上出现以下错误,例如:

  

ServerLibraryVersion ='context.ServerLibraryVersion'引发了类型为'Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException'的异常

5 个答案:

答案 0 :(得分:0)

使用CSOM,不清楚使用什么WCF:

ClientContextctx = newClientContext("http://gowtham.sharepoint.com");  
            List oList = ctx.Web.Lists.GetByTitle("Announcements");  
            ListItemCreationInformationitemCreateInfo = newListItemCreationInformation();  
            ListItemnewItem = oList.AddItem(itemCreateInfo);  
            newItem["Title"] = "Test Item!";  
            newItem["Body"] = "welcome to Gowtham Blog";  
            newItem.Update();
            context.ExecuteQuery();  

答案 1 :(得分:0)

尝试下面的代码

using (ClientContext clientContext = new ClientContext(ServerURL))
{
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    var securePassword = new SecureString();
    foreach (char c in password)
    {
        securePassword.AppendChar(c);
    }
    clientContext.Credentials = new SharePointOnlineCredentials(your_mail_ID, securePassword);
    Web web = clientContext.Web;
    var list = web.Lists.GetByTitle("your_list_name");
    clientContext.Load(list.RootFolder);
    clientContext.ExecuteQuery();
}

答案 2 :(得分:0)

您的代码对于本地SharePoint似乎很好。我认为您应该检查服务器场上的某些设置,这是造成该问题的原因。 如果启用了IIS Admin Service,请检查服务器场服务器上的服务 enter image description here

还要在SharePoint CA上检查用户配置文件服务和对Windows令牌服务的声明(均应启用) enter image description here

...抱歉,lang:)...通常我可以使用PL语言访问SharePoint,但是我尝试将最重要的内容翻译为ang。

还请检查IIS上您尝试访问的应用程序池是否正常运行。我想是的,否则您会遇到很多其他错误,但是检查总是更好。

答案 3 :(得分:0)

检查“ SharePoint Web服务根”应用程序池是否已停止或“ SharePoint Web服务” Web应用程序是否未在IIS中启动。

转到IIS应用程序池,找到“ SecurityTokenServiceApplicationPool”,然后从操作面板中单击“高级设置”,然后滚动到“ ProcessModel”部分,然后将“标识”更改为您的SharePoint场帐户并执行IISRESET。

并使用CSOM C#代码创建控制台应用程序以检查其是否有效。

答案 4 :(得分:0)

FWIW-我遇到了完全相同的错误-CSOM本地模式到本地SP2016,出现500个错误,要求按标题列表。

我刚刚应用了10/2019 SharePoint更新,但没有经历产品配置向导和Central Admin中的提示。一旦完成此操作,CSOM请求就会再次起作用。