5

Today I copied the following code from my book to test my first SharePoint-CSOM application, but I cannot get it to work properly:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;

namespace csom
{
    class Program
    {
        static void Main(string[] args)
        {
            ClientContext ctx = new ClientContext("http://serverone");
            ctx.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication;
            FormsAuthenticationLoginInfo loginInfo = new FormsAuthenticationLoginInfo
            {
                LoginName = "myusername@domain.com",
                Password = "mypassword"
            };
            ctx.FormsAuthenticationLoginInfo = loginInfo;

            Site site = ctx.Site;
            ctx.Load(site);
            Web web = site.RootWeb;
            ctx.Load(web);
            List list = web.Lists.GetByTitle("Project types");
            ctx.Load(list);
            ctx.ExecuteQuery();
        }
    }
}

The following exception is thrown and I really cannot find the reason for this. I enabled FormAuthentication in the Central Administration and in IIS for this Website so this cant be the problem...

An unhandled exception of type 'System.Web.Services.Protocols.SoapException' occurred in System.Web.Services.dll

Additional information: Server was unable to process request. ---> The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework SDK documentation and inspect the server trace logs.

Any idea what I could still try? Of course the values for servers and passwords are changed in this post :)

I googled how to turn on this attribute, and I figured that I have to change my web.config as follows:

    <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
        <behaviors>
      <serviceBehaviors>
        <behavior name="debug">
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="Microsoft.SharePoint.Client.dll" behaviorConfiguration="debug" />
    </services> 
  </system.serviceModel>

However, this does not work. How can I turn on includeExceptionDetailInFaults in this case?

Thanks

  • THe code seems fine to me. Do you have access to server-side logs? Or maybe turn on IncludeExceptionDetailInFaults – Aziz Kabyshev Mar 16 '16 at 20:54
  • How about server logs? It's hard to advise on generic "internal error" failure, we'll need details – Aziz Kabyshev Mar 17 '16 at 16:00
  • Do you mean the logs in "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\LOGS". There I can find exactly the same information like Visual Studio gives me, but no more. Is there another server log I can look in? – SPDeveloper Mar 17 '16 at 18:28
  • Is SharePoint configured to use Forms Authentication? If not you are most likely passing the wrong type of credential. – Rob Windsor Mar 18 '16 at 13:31
  • Yes, I went to Central Administration, Web applications and I configured my web application for Forms Authentication. I also restarted IIS... I actually almost gave up now... maybe I just can not use FA... – SPDeveloper Mar 18 '16 at 15:08
  • 1
    Can you log into the site through the browser using Forms Auth? – James Love Mar 18 '16 at 17:33

1 Answers1

1

try an alternate method to login as follows

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
using System.Security;
using System.Net;


namespace Study
{
    class Program
    {
        static void Main(string[] args)
        {
            ClientContext clientContext = new ClientContext("http://yourSiteUrl");
            SecureString passWord = new SecureString();

            foreach (char c in "yourPassword".ToCharArray())
                passWord.AppendChar(c);

            clientContext.Credentials = new NetworkCredential("yourLoginName", passWord, "yourSharePointDomain");

            Web web = clientContext.Web;
            clientContext.Load(web);

            clientContext.ExecuteQuery();

            List testList = web.Lists.GetByTitle("yourListTitle");
            clientContext.Load(testList);
            clientContext.ExecuteQuery();

        }
    }
}
Ashwin Kheta
  • 147
  • 15