如何使用Microsoft.SharePoint.Client将附件上传到列表项?

时间:2015-08-10 19:02:55

标签: sharepoint

我正在尝试使用附件创建新的列表项。贝娄是我的代码:

using (var clientContext = new ClientContext(_sharepointSite))
{
   clientContext.Credentials = _credentials;
   var list = clientContext.Web.Lists.GetByTitle("ListName");
   var listEntry = list.AddItem(new ListItemCreationInformation());
   listEntry["Title"] = model.Title;
   listEntry.Update();
   clientContext.ExecuteQuery();
   var attInfo = new AttachmentCreationInformation();
   attInfo.ContentStream = model.File.InputStream;
   attInfo.FileName = model.File.FileName;
   var attachment = listEntry.AttachmentFiles.Add(attInfo);
   clientContext.Load(attachment);
   clientContext.ExecuteQuery();
}

列表项创建代码工作正常,但文件附件代码会引发以下错误:

Microsoft.SharePoint.Client.ServerException: Data at the root level is invalid. Line 2, position 1.

在最后一个clientContext.ExecuteQuery()上。有没有人对这是什么原因有任何想法?

更新:

以下是我的模型架构:

public class UploadFileModel : IValidatableObject
{
    [Required, MaxLength(100), Display(Name = "File title")]
    public string Title { get; set; }

    [Required, Display(Name = "User type")]
    public UserTypes? UserType { get; set; }

    [MaxLength(100), Display(Name = "Health care organization name")]
    public string HealthCareOrgName { get; set; }

    [MaxLength(100), Display(Name = "User's email")]
    public string UserEmail { get; set; }

    [Required]
    public HttpPostedFileBase File { get; set; }
}

基本上,File属性只是用户尝试上传的文件......

0 个答案:

没有答案