1

I'm trying to upload files to a folder using SharePoint and C#.

I managed to create folders with my code and it works fine.

This is my Document class:

[DataContractAttribute]
public class Document
{
    [DataMemberAttribute]
    public string Name { get; set; }

    [DataMemberAttribute]
    public byte[] Content { get; set; }

    [DataMemberAttribute]
    public bool ReplaceExisting { get; set; }

    [DataMemberAttribute]
    public string Folder { get; set; }

    [DataMemberAttribute]
    public Dictionary<string, object> Properties { get; set; }

    public Document()
    {
        Properties = new Dictionary<string, object>();
    }

    public Document(string name, byte[] content)
    {
        Name = name;
        ReplaceExisting = false;
        Content = content;
        Properties = new Dictionary<string, object>();
    }

    public Document(string name, byte[] content, bool replace)
    {
        Name = name; 
        Content = content;
        ReplaceExisting = replace;
        Properties = new Dictionary<string, object>();
    }
}

And this is the class where I use it to upload files (Document) to an existing SharePoint folder:

public class SharePointHandler : IDisposable
{
    private static string sharePointSite = "My Site";
    private static string documentLibraryName = "Root folder";

    public SharePointHandler() { }

    public void Upload(List<Document> documents, string company, int year, string quarter)
    {
        string Year = year.ToString();

        try
        {
            using (ClientContext context = new ClientContext(sharePointSite))
            {
                var list = context.Web.Lists.GetByTitle(documentLibraryName);
                context.Load(list);

                var root = list.RootFolder;
                context.Load(root);
                context.ExecuteQuery();
                .
                .
                .

                foreach (var document in documents)
                {
                    var fileCreationInformation = new FileCreationInformation();
                    fileCreationInformation.Content = document.Content;

                    fileCreationInformation.Overwrite = true;

                    fileCreationInformation.Url = list.RootFolder.ServerRelativeUrl + "/" + company + "/" + Year + "/" + quarter + "/" + document.Name;
                    Microsoft.SharePoint.Client.File uploadFile = quarterFolder.Files.Add(fileCreationInformation);


                    foreach (KeyValuePair<string, object> property in document.Properties)
                        uploadFile.ListItemAllFields[property.Key] = property.Value;


                    try
                    {
                        uploadFile.CheckOut();
                        context.ExecuteQuery();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    uploadFile.ListItemAllFields.Update();
                    context.ExecuteQuery();

                    uploadFile.CheckIn("", CheckinType.MajorCheckIn);
                    context.ExecuteQuery();
                }
            }
        }
        catch (Exception ex)
        {
            EventLog.WriteEntry(ex.Message, EventLogEntryType.Error);
            return;
        }
    }

    public void Dispose() { }
}

When I run the code I have one document with:

Content: {byte[11430]}

Folder: null

Name: Testing.docx

Properties: Count = 0

ReplaceExisting: false

Everything works fine and I get the URL needed. But when I get to these commands:

try
{
    uploadFile.CheckOut();
    context.ExecuteQuery();
}

The program fails and I get error that says: File not found.

Are there any detailed tutorials out there? Or what am I doing wrong?

  • Don't forget you are using CSOM, it means quarterFolder.Files.Add just add the operation the requestBatch which will be executed when context.executQuery is invoked. – must19 Nov 18 '17 at 4:07
2

If I am not wrong, you are trying to check out the file before uploading the file itself. Try this first.

    fileCreationInformation.Url = list.RootFolder.ServerRelativeUrl + "/" + company + "/" + Year + "/" + quarter + "/" + document.Name;
    Microsoft.SharePoint.Client.File uploadFile = quarterFolder.Files.Add(fileCreationInformation);
    context.Load(fileCreationInformation);
    context.ExecuteQuery();

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.