I am a newbie to sharepoint. I have a byte[] data and I want to upload it to sharepoint site using c#. I also want to pass credentials for it. Can anyone please guide me.

I do not want to install Sharepoint. I just want to create a file on sharepoint through c#.

The code I tried is:

        Uri destUri = new Uri("http://test.net/excel/docs/Forms/AllItems.aspx/");
        WebRequest req = WebRequest.Create(destUri);
        req.Method = "PUT";
        req.Credentials = CredentialCache.DefaultCredentials;
        using (req.GetRequestStream())
            string destFilename = @"\\test.net\excel\docs\501.xls";
            byte[] data = new byte[10];
            System.IO.File.WriteAllBytes(destFilename, data);


Access Denied

There are multiple ways to complete this request. Firstly, you will want to ensure your user account has the access to the particular document library. If so, the following code should function properly. Note: Using this approach, does not allow you to utilize the metadata. The preferred route, is using SharePoint Object Model or Web Services.

// Uri destUri = new Uri("http://server/sites/mysite/myweb/mylibrary/document.doc");
Uri destUri = new Uri("http://test.net/excel/docs/501.xls");
using(FileStream inStream = File.OpenRead(@"c:\docs\501.xls"))
    WebRequest req = WebRequest.Create(destUri);
    req.Method = "PUT";
    req.Credentials = CredentialCache.DefaultCredentials; 
    using(Stream outStream = req.GetRequestStream())
        CopyStream(inStream, outStream);

Alternatively, you can utilize Web Services, as per this example (the site can provide additional details and code),

//Copy WebService Settings
string webUrl = “http://test.net/excel/docs/″;
WSCopy.Copy copyService = new WSCopy.Copy();
copyService.Url = webUrl+”/_vti_bin/copy.asmx”;
copyService.Credentials = System.Net.CredentialCache.DefaultCredentials;

//Declare and initiates the Copy WebService members for uploading
string sourceUrl = “c:\docs\501.xls”;
string[] destinationUrl = { “http://test.net/excel/docs/501.xls” };
WSCopy.CopyResult cResult1 = new WSCopy.CopyResult();
WSCopy.CopyResult cResult2 = new WSCopy.CopyResult();
WSCopy.CopyResult[] cResultArray = { cResult1, cResult2 };
WSCopy.FieldInformation fFiledInfo = new WSCopy.FieldInformation();
fFiledInfo.DisplayName = “Description”;
fFiledInfo.Type = WSCopy.FieldType.Text;
fFiledInfo.Value = “Sample Description”;
WSCopy.FieldInformation[] fFiledInfoArray = { fFiledInfo };

//Reading the document contents in to stream
FileStream strm = new FileStream(sourceUrl, FileMode.Open, FileAccess.Read);
byte[] fileContents = new Byte[strm.Length];
byte[] r = new Byte[strm.Length];
int ia = strm.Read(fileContents, 0, Convert.ToInt32(strm.Length));

//Copy the document from SourceUrl to destinationUrl with metadatas
uint copyresult = copyService.CopyIntoItems(sourceUrl, destinationUrl, fFiledInfoArray, fileContents, out cResultArray);

//Where WSCopy is the WebReference Name and the document is uploaded successfully, copyresult returns 0.

If I understood your requirements properly, you need to upload file into SharePoint On-Premise and from the client side, right? If so, there are several options on how to accomplish it.

Send file via HTTP POST using .NET

At least the following components could be utilized for that purpose:


The example demonstrates how to upload file using WebClient.UploadFile Method:

public static void UploadFile(Uri targeUri, ICredentials credentials, string fileName)
     using (var client = new WebClient())
         client.Credentials = credentials;
         //client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
         var targetFileUri = targeUri + "/" + Path.GetFileName(fileName);
         client.UploadFile(targetFileUri, "PUT", fileName);      


 var filePath = @"C:\Documents\SharePoint User Guide.docx";
 var credentials = new NetworkCredential(userName, password, domain);
 UploadFile(new Uri("https://contoso.sharepoint.com/documents"),credentials, filePath);

Using Microsoft SharePoint Client SDK

How to upload a file to a SharePoint site using File.SaveBinaryDirect Method

using (var ctx = new ClientContext(url))
     ctx.Credentials = new NetworkCredential(userName, password, domain);
     using (var fs = new FileStream(fileName, FileMode.Open))
         var fi = new FileInfo(fileName);
         var list = ctx.Web.Lists.GetByTitle(listTitle);
         var fileUrl = String.Format("{0}/{1}", list.RootFolder.ServerRelativeUrl, fi.Name);

         Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, fileUrl, fs, true);

Using SharePoint Web Services

How to upload file using Copy Web Service:

 var webUri = new Uri("http://contoso.sharepoint.com");
 string sourceUrl = @"C:\Documents\SharePoint User Guide.docx";
 string destinationUrl = webUri + "/documents/SharePoint User Guide 2013.docx";
 var fieldInfo = new FieldInformation();
 FieldInformation[] fieldInfos = { fieldInfo };
 CopyResult[] result;
 using (var proxyCopy = new Copy())
      proxyCopy.Url = webUri + "/_vti_bin/Copy.asmx";
      proxyCopy.Credentials= new NetworkCredential(userName, password, domain);

      var fileContent = System.IO.File.ReadAllBytes(sourceUrl);

      proxyCopy.CopyIntoItems(sourceUrl, new[] { destinationUrl }, fieldInfos, fileContent, out result);

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.