0
votes

I'm setting up some initial test code in order to pull some basic SP properties from our SP site.

My ultimate goal is to download an Excel file from the company SharePoint server; however, my initial setup code is throwing a connection exception :

"The 'username' argument is invalid."

My c# code is as follows:

string spSite = "http://sharepoint.mycompany.com";
ClientContext context = new ClientContext(spSite);

// convert to secure password
string pwd = "myPassword";
SecureString securePwd = new SecureString();
foreach (var c in pwd.ToCharArray())
{
    securePwd.AppendChar(c);
};
            
// *** **THROWS EXCEPTION** "The 'username' argument is invalid."
NetworkCredential cred = new NetworkCredential(@"companydomain\user", securePwd, spSite );

context.Credentials = new SharePointOnlineCredentials(cred.UserName, cred.SecurePassword);

Web webContext = context.Web;
context.Load(web, w => w.Title, w => w.Description);

try
{   
    clientContext.ExecuteQuery();
}
catch(Exception ex)
{
    throw new Exception(ex.Message);
}

Some important points:

  1. Here's the definition from the SP namespace, so I know I'm passing the correct params:

    public SharePointOnlineCredentials(string username, SecureString password);

  2. If I do not add https to my site, I will get the error:

    Value does not fall within the expected range.

Some references I've used:

https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/complete-basic-operations-using-sharepoint-client-library-code

Authenticate network credential to access SharePoint site on client object model

var data = '{"env": "myEnv", "password": "TEST123", "userId": "theUser"}';
console.log(JSON.parse(data));

var dataEncr = base64.encode(data);
var dataDecr = base64.decode(dataEncr);

var dataJson = JSON.parse(dataDecr);


var base64 = {
        _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
        encode: function (e) { var t = ""; var n, r, i, s, o, u, a; var f = 0; e = base64._utf8_encode(e); while (f < e.length) { n = e.charCodeAt(f++); r = e.charCodeAt(f++); i = e.charCodeAt(f++); s = n >> 2; o = (n & 3) << 4 | r >> 4; u = (r & 15) << 2 | i >> 6; a = i & 63; if (isNaN(r)) { u = a = 64 } else if (isNaN(i)) { a = 64 } t = t + this._keyStr.charAt(s) + this._keyStr.charAt(o) + this._keyStr.charAt(u) + this._keyStr.charAt(a) } return t },
        decode: function (e) { var t = ""; var n, r, i; var s, o, u, a; var f = 0; e = e.replace(/[^A-Za-z0-9+/=]/g, ""); while (f < e.length) { s = this._keyStr.indexOf(e.charAt(f++)); o = this._keyStr.indexOf(e.charAt(f++)); u = this._keyStr.indexOf(e.charAt(f++)); a = this._keyStr.indexOf(e.charAt(f++)); n = s << 2 | o >> 4; r = (o & 15) << 4 | u >> 2; i = (u & 3) << 6 | a; t = t + String.fromCharCode(n); if (u != 64) { t = t + String.fromCharCode(r) } if (a != 64) { t = t + String.fromCharCode(i) } } t = base64._utf8_decode(t); return t },
        _utf8_encode: function (e) { e = e.replace(/rn/g, "n"); var t = ""; for (var n = 0; n < e.length; n++) { var r = e.charCodeAt(n); if (r < 128) { t += String.fromCharCode(r) } else if (r > 127 && r < 2048) { t += String.fromCharCode(r >> 6 | 192); t += String.fromCharCode(r & 63 | 128) } else { t += String.fromCharCode(r >> 12 | 224); t += String.fromCharCode(r >> 6 & 63 | 128); t += String.fromCharCode(r & 63 | 128) } } return t },
        _utf8_decode: function (e) { var t = ""; var n = 0; var r = c1 = c2 = 0; while (n < e.length) { r = e.charCodeAt(n); if (r < 128) { t += String.fromCharCode(r); n++ } else if (r > 191 && r < 224) { c2 = e.charCodeAt(n + 1); t += String.fromCharCode((r & 31) << 6 | c2 & 63); n += 2 } else { c2 = e.charCodeAt(n + 1); c3 = e.charCodeAt(n + 2); t += String.fromCharCode((r & 15) << 12 | (c2 & 63) << 6 | c3 & 63); n += 3 } } return t }
    }


// if needed
this.decode = function (itemValue) {
        var deValue = base64.decode(itemValue);
        var objValue = JSON.parse(deValue);
        return objValue;
    }
2

2 Answers

1
votes

Instead of using domain with user name:

NetworkCredential cred = new NetworkCredential(@"companydomain\user", securePwd, spSite );

use this:

NetworkCredential cred = new NetworkCredential(@"user_name", password, companydomain );
0
votes

SharePointOnlineCredential object wants the UPN/email login name format.

Try changing your new statement on that object like this-

context.Credentials = new SharePointOnlineCredentials("[email protected]", cred.SecurePassword);

Make sure to substitute 'tenantName' with an actual tenant name. If you have your own STS, then you may have to change the domain to match what your users use to login.