2

I use the following method to upload a document into sharepoint document library. However, upon executing the query - get the following error: Message = "The remote server returned an error: (400) Bad Request."

the files are failing over 1mb, so i tested it via the sharepoint UI and the same file uploaded successfully.

any thoughts on what's the issue is? is it possible to stream the file over rather than 1 large file chunk? the file in question is only 3mb in size..

private ListItem UploadDocumentToSharePoint(RequestedDocumentFileInfo requestedDoc, ClientContext clientContext)
{
    try
    {
        var uploadLocation = string.Format("{0}{1}/{2}", SiteUrl, Helpers.ListNames.RequestedDocuments,
                                           Path.GetFileName(requestedDoc.DocumentWithFilePath));

        //Get Document List
        var documentslist = clientContext.Web.Lists.GetByTitle(Helpers.ListNames.RequestedDocuments);
        var fileCreationInformation = new FileCreationInformation
                                          {
                                              Content = requestedDoc.ByteArray,
                                              Overwrite = true,
                                              Url = uploadLocation //Upload URL,
                                          };

        var uploadFile = documentslist.RootFolder.Files.Add(fileCreationInformation);
        clientContext.Load(uploadFile);
        clientContext.ExecuteQuery();

        var item = uploadFile.ListItemAllFields;
        item["Title"] = requestedDoc.FileNameParts.FileSubject;
        item["FileLeafRef"] = requestedDoc.SharepointFileName;
        item.Update();
    }
    catch (Exception exception)
    {
        throw new ApplicationException(exception.Message);
    }
    return GetDocument(requestedDoc.SharepointFileName + "." + requestedDoc.FileNameParts.Extention, clientContext);
}

EDIT: i did find the following ms page regarding my issue (which seems identical to the issue they have raised) http://support.microsoft.com/kb/2529243 but appears to not provide a solution.

1 Answer 1

3

ok found the solution here: http://blogs.msdn.com/b/sridhara/archive/2010/03/12/uploading-files-using-client-object-model-in-sharepoint-2010.aspx

i'll need to store the document on the server hosting the file then using the filestream upload process i've done in my code below:

    private ListItem UploadDocumentToSharePoint(RequestedDocumentFileInfo requestedDoc, ClientContext clientContext)
    {
        try
        {
            using(var fs = new FileStream(string.Format(@"C:\[myfilepath]\{0}", Path.GetFileName(requestedDoc.DocumentWithFilePath)), FileMode.Open))
            {
                File.SaveBinaryDirect(clientContext, string.Format("/{0}/{1}", Helpers.ListNames.RequestedDocuments, requestedDoc.FileName), fs, true);
            }
        }
        catch (Exception exception)
        {
            throw new ApplicationException(exception.Message);
        }
        return GetDocument(requestedDoc.SharepointFileName + "." + requestedDoc.FileNameParts.Extention, clientContext);
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.