8

I wrote a simple code in CSOM to get web title. Hope code is fine. When I execute my application I got an exception from my third line (clientContext.Credentials = new SharePointOnlineCredentials("Domain\\Username", SecurePassword);)

Username argument is invalid

Any idea to solve this issue? Thanks in advance.

using (var clientContext = new ClientContext("http://siteURL"))
{
    SecureString SecurePassword = GetSecureString("password");
    clientContext.Credentials = new SharePointOnlineCredentials("Domain\\Username", SecurePassword);
    Web web = clientContext.Web;
    clientContext.Load(web,
    webSite => webSite.Title);
    clientContext.ExecuteQuery();
    Console.WriteLine(web.Title);
}
  • 1
    your tag says SharePoint-enterprise, while your code has SharePointOnlineCredential. What actual version of SharePoint you are using? SPO or SP? – Aakash Maurya Jun 22 '17 at 12:01
  • I use CSOM. Tag has been edited – Sivakumar Piratheeban Jun 22 '17 at 12:05
  • 1
    I guess you are working with SharePoint Online? – Aakash Maurya Jun 22 '17 at 12:07
9

The solution is very simple and clear.

While passing credentials do not use Domain name along with username. You just need to use Username.

So your code will be like:

clientContext.Credentials = new SharePointOnlineCredentials("Username", SecurePassword);

If it is SharePoint On-Prem then SharePointOnlineCredential is not required.

In such case you should use:

NetworkCredential _myCredentials = new NetworkCredential("user", "password", "domain"); // While domain will be your site url
//OR
NetworkCredential _myCredentials = new NetworkCredential(@"domain\user", "password");

and then pass credential as

clientContext.Credentials = _myCredentials;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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