SharePoint 2013:为什么在创建 clientContext 时出现 500 错误?
SharePoint 2013: Why getting 500 error while creating clientContext?
我想创建一个 IIS 网络服务,它必须将列表项写入 SharePoint on Premise。
我想使用 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: 'The remote server returned an error: (500) Internal Server Error.'
在事件日志中,我看到以下错误:
WebHost failed to process a request.
Sender Information: System.ServiceModel.ServiceHostingEnvironment+HostingManager/41028758
Exception: System.ServiceModel.ServiceActivationException: The service '/_vti_bin/client.svc' cannot be activated due to an exception during compilation. The exception message is: Operation is not valid due to the current state of the object.. ---> System.InvalidOperationException: Operation is not valid due to the current state of the object.
at Microsoft.SharePoint.Administration.SPWebApplication.get_Context()
................................
在调试中,我也看到在创建 ClientContext 之后和 context.ExecuteQuery() 之前; ClientContext 的某些属性出现以下错误,例如:
ServerLibraryVersion = 'context.ServerLibraryVersion' threw an exception of type 'Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException'
用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();
试试下面的代码
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();
}
您的代码似乎适合 on-prem SharePoint。我认为您应该检查农场的一些设置,我是造成这种情况的原因。
如果 IIS 管理服务打开,请检查场服务器上的服务
也在 SharePoint CA 上检查用户配置文件服务和对 windows 令牌服务的声明(两者都应该打开)
...对不起 lang :)...通常我可以使用 PL 语言访问 SharePoint,但我试图将最重要的内容翻译成 ang。
另请检查您尝试访问的应用程序池在 IIS 上是否正常工作。我想是的,否则你会有很多其他错误,但检查总是更好。
检查 "SharePoint Web Servies Root" 应用程序池是否已停止或 "SharePoint Web Services" Web 应用程序未在 IIS 中启动。
转到 IIS 应用程序池,找到 "SecurityTokenServiceApplicationPool" 并单击操作面板中的 "Advanced Settings",然后滚动到 "ProcessModel" 部分并将身份更改为您的 SharePoint 场帐户并执行IISRESET.
并使用 CSOM C# 代码创建一个控制台应用程序以检查它是否有效。
FWIW - 我有完全相同的错误 - CSOM 本地模式到 on-prem SP2016,按标题请求列表时出现 500 错误。
我刚刚应用了 10/2019 SharePoint 更新,但没有完成产品配置向导和 Central Admin 中的提示。执行此操作后,CSOM 请求再次生效。
我想创建一个 IIS 网络服务,它必须将列表项写入 SharePoint on Premise。
我想使用 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: 'The remote server returned an error: (500) Internal Server Error.'
在事件日志中,我看到以下错误:
WebHost failed to process a request. Sender Information: System.ServiceModel.ServiceHostingEnvironment+HostingManager/41028758 Exception: System.ServiceModel.ServiceActivationException: The service '/_vti_bin/client.svc' cannot be activated due to an exception during compilation. The exception message is: Operation is not valid due to the current state of the object.. ---> System.InvalidOperationException: Operation is not valid due to the current state of the object. at Microsoft.SharePoint.Administration.SPWebApplication.get_Context() ................................
在调试中,我也看到在创建 ClientContext 之后和 context.ExecuteQuery() 之前; ClientContext 的某些属性出现以下错误,例如:
ServerLibraryVersion = 'context.ServerLibraryVersion' threw an exception of type 'Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException'
用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();
试试下面的代码
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();
}
您的代码似乎适合 on-prem SharePoint。我认为您应该检查农场的一些设置,我是造成这种情况的原因。
如果 IIS 管理服务打开,请检查场服务器上的服务
也在 SharePoint CA 上检查用户配置文件服务和对 windows 令牌服务的声明(两者都应该打开)
...对不起 lang :)...通常我可以使用 PL 语言访问 SharePoint,但我试图将最重要的内容翻译成 ang。
另请检查您尝试访问的应用程序池在 IIS 上是否正常工作。我想是的,否则你会有很多其他错误,但检查总是更好。
检查 "SharePoint Web Servies Root" 应用程序池是否已停止或 "SharePoint Web Services" Web 应用程序未在 IIS 中启动。
转到 IIS 应用程序池,找到 "SecurityTokenServiceApplicationPool" 并单击操作面板中的 "Advanced Settings",然后滚动到 "ProcessModel" 部分并将身份更改为您的 SharePoint 场帐户并执行IISRESET.
并使用 CSOM C# 代码创建一个控制台应用程序以检查它是否有效。
FWIW - 我有完全相同的错误 - CSOM 本地模式到 on-prem SP2016,按标题请求列表时出现 500 错误。
我刚刚应用了 10/2019 SharePoint 更新,但没有完成产品配置向导和 Central Admin 中的提示。执行此操作后,CSOM 请求再次生效。