8

I am developing a C# console application on my local computer running on Windows 8. My target SharePoint server is SharePoint Online on Office 365. I am trying to get the web site name using web.Title property of CSOM of SharePoint 2013. I wrote the following code inside the main function to do that.

ClientContext clientContext = new    ClientContext("https://innergen.sharepoint.com/sites/mydevsite");
        Web web = clientContext.Web;
        clientContext.Load(web);
        clientContext.ExecuteQuery();

        Console.WriteLine("Web Site Name : " + web.Title);
        Console.ReadLine();

Now when I run my project in debug mode, I receive an error saying "The remote server returned an error: (403) Forbidden". I used the same Microsoft Account to log in to both my SharePoint Online account and Windows 8 OS(local machine). Can anybody tell me what's wrong here? Thanks.

1
  • 1
    I too am writing a similar application and use the SharePointOnlineCredential exactly as shown here, but my program crashes "silently". Has anybody else seen this, and more importantly solved it? I don't do much with SP, so it would be a big help. Thanks in advance!
    – russd
    Commented Jan 21, 2015 at 15:20

2 Answers 2

21

You should use SharePointOnlineCredentials that handles all the handshaking, claims and oauth in SharePoint online.

using (ClientContext clientContext = new ClientContext("https://innergen.sharepoint.com/sites/mydevsite"))
{
    SecureString passWord = new SecureString();
 
    foreach (char c in "yourpassword".ToCharArray()) passWord.AppendChar(c);
 
    clientContext.Credentials = new SharePointOnlineCredentials("[email protected]", passWord);
 
    Web web = clientContext.Web;
 
    clientContext.Load(web);
 
    clientContext.ExecuteQuery();
 
    Console.WriteLine(web.Title);
 
    Console.ReadLine();
}
1
6

if you are trying to run it using network credential try the below code

clientContext.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

Also try adding headers

context.ExecutingWebRequest += clientContext_ExecutingWebRequest;


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

        }

Usage:

static void Main(string[] args)
        {
            ClientContext context = new ClientContext(SiteUrl);
            Web web = context.Web;
            context.ExecutingWebRequest += clientContext_ExecutingWebRequest;
            context.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
            context.ExecuteQuery();
        }
2
  • 1
    Is the order between .Credentials and .ExecutingWebRequest += important? And what about when comapred to Web web = context.web;?
    – Ama
    Commented Jan 26, 2020 at 21:19
  • 1
    This just saved me. Many thanks!
    – m4d
    Commented Sep 4 at 19:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.