I m uploading file to Sharpoint online using C# code from a byte[].

Code is giving the below error:

Access denied. You do not have permission to perform this action or access this resource

I can logon to SP and crate and upload file. I have checked it is same user id which has rights or creating a list, adding documents, Full Control, and Use Remote Interfaces.

     public static bool UploadFile(SP sp, string folderName, string fileNameWithExtension, byte[] fileContent, TraceWriter log)
    bool result = false;
        SecureString securePwd = new SecureString();
        char[] pwdarray = sp.pwd.ToCharArray();

        foreach (var item in pwdarray)
        SharePointOnlineCredentials creds = new SharePointOnlineCredentials(sp.id, securePwd);        
        using (ClientContext clientContext = new ClientContext(sp.url))

            clientContext.Credentials = creds;
            clientContext.AuthenticationMode = ClientAuthenticationMode.Default;
            Web web = clientContext.Web;

            var fileCreationInformation = new FileCreationInformation();
            fileCreationInformation.Content = fileContent;
            fileCreationInformation.Overwrite = true;

            fileCreationInformation.Url = fileNameWithExtension;          
            Microsoft.SharePoint.Client.List docs = web.Lists.GetByTitle("FtpFiles");
            Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(fileCreationInformation);

            return result = true;            
    catch (Exception ex)
        throw ex;

Please guide me not getting what is wrong :)

1 Answer 1


Do you mean that you want to get the content of file in SharePoint?

We can use CSOM to achieve it.

Here is a demo for your reference and i have tested successfully.

    /// <summary>
/// upload file to Document Library
/// </summary>
/// <param name="context"></param>
/// <param name="documentLibraryName"></param>
/// <param name="filePath"></param>
/// <param name="fileName"></param>
public static void uploadFileToDocLib(ClientContext context, string documentLibraryName, string filePath,string fileName)

   string siteURL = context.Url.EndsWith("/") ? context.Url.Substring(0, context.Url.Length - 1) : context.Url;

    List list = context.Web.Lists.GetByTitle(documentLibraryName);

    FileStream fileStream = new FileStream(filePath, FileMode.Open);

    FileCreationInformation newFileInfo = new FileCreationInformation()
        ContentStream = fileStream,
        Url = siteURL + "/" + documentLibraryName + "/" + fileName,
        Overwrite = true

    Microsoft.SharePoint.Client.File file = list.RootFolder.Files.Add(newFileInfo);

Set credential of SharePoint Online.

    /// <summary>
/// Set credential of SharePoint Online
/// </summary>
/// <param name="clientContext"></param>
/// <param name="userName"></param>
/// <param name="password"></param>
public static void setOnlineCredential(ClientContext clientContext,string userName,string password)
    //set the user name and password
    SecureString secureString = new SecureString();
    foreach (char c in password.ToCharArray())
    clientContext.Credentials = new SharePointOnlineCredentials(userName, secureString);           

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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