【问题标题】:Add column values while uploading a file to Sharepoint with C#使用 C# 将文件上传到 Sharepoint 时添加列值
【发布时间】:2018-03-29 14:16:07
【问题描述】:

目前我正在处理一个需要将文件上传到 Sharepoint 的项目。到目前为止,我可以毫无问题地以编程方式上传文件:

  class SPAPI
    {
        internal void SPUploader(Stream fs, string filename)
        {
            ClientContext context = new ClientContext("https://....de");

            System.Net.ICredentials creds = System.Net.CredentialCache.DefaultCredentials;

            context.Credentials = creds;
            context.RequestTimeout = 60000000; // Time in milliseconds




            string fnUrl = "/software/ap_ck/Dokumenten Management System/" + filename;
            Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, fnUrl, fs, true);

        }

    }

此外,我还必须将特定值(连同文件)添加到 Sharepoint 文档列表中的列。由于这是我第一次使用 Sharepoint,所以在网上找到的一些解决方案有点令人困惑。

一个解决方案包含以下行:

List docList = site.Lists.GetByTitle("members"); 

我只能猜测“列表”是文件的存储空间?如何获取将每个文件上传到的列表标题?抱歉这个愚蠢的问题,但我以前从未使用过 Sharepoint。

【问题讨论】:

  • 不要随意尝试,而是要了解 SharePoint 是什么、做什么、它是如何工作的以及应该如何使用它。 90% 的 SharePoint 开发实际上是管理和配置。不,列表不是“存储”。 存储文档。列表项和文档都有属性,但 documents 有很多额外的功能。如果您上传 Word、Excel、PDF 等,您可能不需要添加任何内容。SharePoint 库会自动提取文档属性
  • 对不起,我的开场问题不是很具体。 C# 应用程序具有附加功能。我必须基本上拆分文件名字符串,分析它并确定它是什么类型的文档,它属于什么客户等等。我想添加到列中的每个文件(我从 SQL 数据库中获取)的这些特定值。但我想我必须在复活节假期进入 Sharepoint。

标签: c# sharepoint


【解决方案1】:

我们可以使用事件接收器来实现这一点。

首先如下创建一个事件接收器。

我们可以在添加的项目(上传的文档)之后添加列值,如下所示。

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using System.Web;
using System.Text;
using System.Data.SqlClient;

namespace SharePointFarmSolutionDev.DocumentEventReceiver
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class DocumentEventReceiver : SPItemEventReceiver
    {
        private readonly HttpContext _currentContext;
        public DocumentEventReceiver()
        {
            if (_currentContext == null)
            {
                _currentContext = HttpContext.Current;
            }
        }

        public override void ItemAdded(SPItemEventProperties properties)
        {
             base.ItemAdded(properties);
             //init
             Guid listId = properties.ListId;
             int itemId = properties.ListItemId;
             SPSite site = properties.Site;
             SPWeb web = properties.Web;
             SPList list = web.Lists[listId];
             SPListItem item = list.GetItemById(itemId);

             item["title"] = "hello";
             item.Update();
        }



    }


}

更多关于Event Receiver的信息,我们可以参考:Introduction To SharePoint Event Receivers

【讨论】:

  • 我如何获得事件接收者项目?我的 Visual C# 项目列表中没有 Office/SharePoint 子类别,也没有在“在线”选项卡中找到它。
  • 您需要打开Visual Studio安装程序来安装SharePoint开发组件。
【解决方案2】:

我建议您突出显示您正在使用的 SP 版本以获得更好的答案。执行上述代码后,您需要获取刚刚上传的对象的句柄。

Uri filename = new Uri("Your file Url");
string server = filename.AbsoluteUri.Replace(filename.AbsolutePath, "");
string serverrelative = filename.AbsolutePath;
Microsoft.SharePoint.Client.File file= web.GetFileByServerRelativeUrl(serverrelative);
ListItem lstItem = file.ListItemAllFields;
clientContext.Load(lstItem);
clientContext.ExecuteQuery();
Once you have got the list handle, you can update the editor field as below

lstItem[<Field Name Here>] =  <value here>
lstItem.Update();
clientContext.ExecuteQuery();

我从博客here 中窃取了答案,但这应该是一个很好的起点。

值得记住的是,当您使用文档库时,您拥有上传的文件和关联的 ListItem,您的代码处理上传,而我复制的代码处理列表项。

我没有对此进行测试,但我很确定这就是你必须这样做的方式。

干杯

真人

【讨论】:

  • 我感觉您的解决方案是最直接的解决方案,但它不适用于我的代码。我有那行: Microsoft.SharePoint.Client.File file = context.Web.GetFileByServerRelativeUrl(@"https://......de/software/ap_ck/Dokumenten%20Management%20System/100_001_000_1.txt");其余代码类似于您的解决方案......但文件似乎没有被加载,即使这是它的路径。
  • 虽然我知道这对您不起作用,但您没有提供您可能收到的任何形式的错误消息,这使得您很难提供帮助,我已经稍微更新了代码 sn-p
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-11
  • 1970-01-01
  • 1970-01-01
  • 2020-08-19
相关资源
最近更新 更多