1
votes

I want to create a IIS webservice which has to write list items to SharePoint on Premise.

I want to use CSOM and try to create a 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;
    }

While executing, I get an error at context.ExecuteQuery();

System.Net.WebException: 'The remote server returned an error: (500) Internal Server Error.'

In the Event Log, I see following 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() ................................

In debugging, I also see after creating the ClientContext and before context.ExecuteQuery(); following error at some properties of ClientContext, e.g.:

ServerLibraryVersion = 'context.ServerLibraryVersion' threw an exception of type 'Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException'

5
Might be some security issue with SharePoint online. Try adding the very next line ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; after creating clientcontext and replace NetworkCredential with SharePointOnlineCredentials - Vijay Dodamani

5 Answers

1
votes

Your code seems fine for on-prem SharePoint. I think You should check some settings on the farm that my be the cause of that. Please check the services on farm server if the IIS Admin Service is on enter image description here

also on SharePoint CA check the user profile service and the claims to windows token service (both should be on) enter image description here

... sorry for the lang :)... usually I have access to SharePoint in PL language, but I tried to translate the most important stuff to ang.

Please also check if on IIS the app pools that You try to access are working correctly. I suppose yes, otherwise You would have a lot of other errors, but it's always better to check.

0
votes

Use CSOM, what to use WCF is not clear:

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();  
0
votes

Try below code

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();
}
0
votes

Check if the "SharePoint Web Servies Root" application pool was stopped or the "SharePoint Web Services" web application not start in IIS.

Go to IIS Application Pools, find the "SecurityTokenServiceApplicationPool" and click "Advanced Settings" from the action panel, then Scroll to "ProcessModel" section and change the Identity to your SharePoint Farm Account and do IISRESET.

And create a console application with the CSOM C# code to check if it works.

0
votes

FWIW - I had exactly the same error - CSOM local mode to on-prem SP2016 with a 500 error requesting the list by title.

I had just applied a 10/2019 SharePoint update, but hadn't gone through Product Configuration Wizard and the prompts in Central Admin. Once I did that, CSOM requests worked again.