使用C#将文件上载到Sharepoint时添加列值

时间:2018-03-29 14:16:08

标签: c# sharepoint

目前,我正在处理一个需要将文件上传到Sharepoint的项目。到目前为止,我可以通过编程方式上传文件而不会出现问题:

this.activeFolderSub = this.route.params.subscribe(params => {
this.folderName = params['folder'];

 this.messageSub = this.googleService.messages.subscribe(message => {
  this.messageIndex = 0;
    const moveToTrashMessage = message.filterMessages;
    const messageIds = [];
    const labelsToAdd = [];
    const labelsToRemove = [];
    labelsToAdd.push(message.action);
    labelsToRemove.push(this.folderName);
    moveToTrashMessage.forEach((it) => { messageIds.push(it.id) });      
    if (messageIds && messageIds.length > 0) {
      this.googleService.modifyMessages(messageIds, labelsToAdd, labelsToRemove).subscribe(messages => {
        if (messages.result && this.messageIndex == 0) {              
          this.toastr.success('Conversations have been moved! ', 'Success!');
          this.getAllMessageList(this.folderName, null);
           this.messageIndex++;
        }
      });
    }
  }
  );
});

此外,我还必须将特定值(连同文件)添加到Sharepoint文档列表中的列。由于这是我第一次使用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。

2 个答案:

答案 0 :(得分:0)

我建议您突出显示您正在使用的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窃取了答案,但这应该是一个很好的起点。

值得记住的是,当您使用doc libs时,您拥有上传的文件和相关的ListItem,您的代码处理上传和我复制的代码片段处理列表项。

我没有对此进行过测试,但我很确定这是你必须要做的。

干杯

Truez

答案 1 :(得分:0)

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

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

enter image description here

enter image description here

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

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



    }


}

有关事件接收器的更多信息,我们可以参考:Introduction To SharePoint Event Receivers