Connect to SharePoint site(configured with claim based authentication) with Managed CSOM

2

September 12, 2013 by Ravi C Khambhati

While I was working with my claim based authenticated site with Managed CSOM got below error.

System.Net.WebException: The remote server returned an error: (403) Forbidden.
at System.Net.HttpWebRequest.GetResponse()
at Microsoft.SharePoint.Client.SPWebRequestExecutor.Execute()
at Microsoft.SharePoint.Client.ClientContext.GetFormDigestInfoPrivate()
at Microsoft.SharePoint.Client.ClientContext.EnsureFormDigest()
at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()
at SharePointConsoleApplication1.Program.ConnectSharePoint() in c:\Ravi\Proje
cts\SharePointConsoleApplication1\SharePointConsoleApplication1\Program.cs:line
56
at SharePointConsoleApplication1.Program.Main(String[] args) in c:\Ravi\Proje
cts\SharePointConsoleApplication1\SharePointConsoleApplication1\Program.cs:line
31

This looks strange. Isn’t it? After doing some research on Google I got the below solution.


        private static void ConnectSharePoint()
        {
            try
            {
                using (ClientContext clientContext = new ClientContext("<URL>"))
                {
                    clientContext.ExecutingWebRequest += clientContext_ExecutingWebRequest;

                    List list = clientContext.Web.Lists.GetByTitle("<ListTitle>");
                    clientContext.Load(list);
                    clientContext.ExecuteQuery();

                    Console.WriteLine("Connected");
                }
            }
            catch
            {
                throw;
            }
        }

        static void clientContext_ExecutingWebRequest(object sender, WebRequestEventArgs e)
        {
            try
            {
                e.WebRequestExecutor.WebRequest.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
            }
            catch
            {
                throw;
            }
        }

Hope this solution will help you and save time.

Google+

Advertisements

2 thoughts on “Connect to SharePoint site(configured with claim based authentication) with Managed CSOM

  1. SP says:

    thank you, saved me a lot of time

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: