使用C#将文件上传到共享点

时间:2015-12-06 16:52:45

标签: c# sharepoint file-upload

我尝试使用SharePoint和C#将文件上传到文件夹。

我设法使用我的代码创建文件夹,并且工作正常。

这是我的Document类:

[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>();
    }
}

这是我使用它将文件(Document)上传到现有共享点文件夹的类:

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() { }
}

当我运行代码时,我有一个文档:

内容:{byte [11430]}

文件夹:null

名称:Testing.docx

属性:Count = 0

ReplaceExisting:false

一切正常,我得到了所需的网址。 但是当我得到这些命令时:

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

该程序失败,我收到错误消息:文件未找到。

我做错了什么?

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

以下是通过CSOM上传到SharePoint的工作示例:

using (ClientContext conext = new ClientContext(site.url))
{
    List projectFiles = projects.Web.Lists.GetByTitle("Project Files");
    context.Load(projectFiles.RootFolder, w => w.ServerRelativeUrl);                       
    context.ExecuteQuery();

    FileStream documentStream = System.IO.File.OpenRead(filePath);
    byte[] info = new byte[documentStream.Length];
    documentStream.Read(info, 0, (int)documentStream.Length);

    string fileURL = projectFiles.RootFolder.ServerRelativeUrl + "/Folder/FileName.ext";

    FileCreationInformation fileCreationInformation = new FileCreationInformation();
    fileCreationInformation.Overwrite = true;
    fileCreationInformation.Content = info;
    fileCreationInformation.Url = fileURL;
    Microsoft.SharePoint.Client.File uploadFile = projectFiles.RootFolder.Files.Add(fileCreationInformation);
    context.Load(uploadFile, w => w.MajorVersion, w => w.MinorVersion);
    context.ExecuteQuery();
}

在您的情况下,我将上传文件和ExecuteQuery(),然后添加元数据并执行第二个查询,确保在files.Add()之后添加context.Load。您正在尝试同时执行太多操作,因此只需使用此代码即可上传文件,然后按照您的其他需求进行操作。此外,文件添加不会为您创建文件夹,因此请确保您指定了有效的路径。