【问题标题】:upload image to sharepoint generic list using csom C#使用 csom C# 将图像上传到共享点通用列表
【发布时间】:2017-03-25 09:02:20
【问题描述】:

如何使用 CSOM C# 将图像上传到 SharePoint 列表“自定义列表” 非库

这是我迄今为止尝试过的:

FieldUrlValue url = new FieldUrlValue();
url.Url = FileUpload.PostedFile.FileName;  
url.Description = "Your description here"; 
newItem["Image"] = url;

【问题讨论】:

  • 从桌面上传图片时出现错误“网址无效”
  • 如果您使用 URL(自定义列表中的字段 URL),您的图像需要已经在 SharePoint 上(并从 sharePoint 而不是本地计算机设置 URL)。这就是为什么您出现错误“URL 无效”

标签: c# .net sharepoint csom


【解决方案1】:

您可以使用此代码通过CSOM 将文档上传到SharePoint

using (ClientContext ctx = new ClientContext("http://urlToYourSiteCollection")) {
    FileCreationInformation fci = new FileCreationInformation();
    fci.Content = System.IO.File.ReadAllBytes("PathToSourceDocument");
    fci.Url = System.IO.Path.GetFileName("PathToSourceDocument");
    Web web = ctx.Web;
    List targetDocLib = ctx.Web.Lists.GetByTitle("yourTargetLibrary");
    ctx.ExecuteQuery();
    Microsoft.SharePoint.Client.File newFile = targetDocLib.RootFolder.Files.Add(fci);
    ctx.Load(newFile);
    ctx.ExecuteQuery();
}

如果你想设置新项目的属性,你可以这样做:

ListItem lItem = newFile.ListItemAllFields;
lItem.File.CheckOut(); //CHECK OUT VERY IMPORTANT TO CHANGE PROPS
ctx.ExecuteQuery();
lItem["yourProperty"] = "somewhat";
lItem.Update();
lItem.File.CheckIn("Z", CheckinType.OverwriteCheckIn);
ctx.ExecuteQuery();

【讨论】:

  • 嗨,我试过你的代码,它运行良好,我在图像库中有一个查找字段,我如何设置它的值,lItem[“我的查找字段”] = value;不起作用。
  • 这个解决方案甚至应该适用于查找字段。但我会尝试检查。
  • 谢谢它的工作,FieldLookupValue lookupf = new FieldLookupValue(); lookupf.LookupId = 1; lItem["Account"] = lookupf;
【解决方案2】:

如果您需要将文件上传到 SharePoint 网站,请访问以下链接,其中介绍了如何使用 CSOM 读取和上传文件

How to download/upload files from/to SharePoint 2013 using CSOM?

【讨论】:

    猜你喜欢
    • 2014-06-12
    • 2019-10-02
    • 2019-11-14
    • 1970-01-01
    • 1970-01-01
    • 2017-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多