6

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.

  • 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 Jan 21 '15 at 15:20
18

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("yourlogin@yoursite.onmicrosoft.com", passWord);

    Web web = clientContext.Web;

    clientContext.Load(web);

    clientContext.ExecuteQuery();

    Console.WriteLine(web.Title);

    Console.ReadLine();
}

Hope it helps!

2

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

protected by Community Jan 29 '17 at 8:11

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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