简体   繁体   English

在将文档上载到Sharepoint时添加元数据

[英]Adding metadata while uploading documents to Sharepoint

I am working on a site that makes use of a web service to upload documents to Sharepoint. 我正在使用Web服务将文档上载到Sharepoint的网站上工作。 I have a web service that works and uploads to Sharepoint. 我有一个工作的Web服务并上传到Sharepoint。 However, I need to add metadata to this file being uploaded, such as first name, last name, date of birth etc from a database record, as well as live data from the site. 但是,我需要从正在上传的文件中添加元数据,例如来自数据库记录的名字,姓氏,出生日期等,以及来自该站点的实时数据。 This data is stuff like a 'Workflow number', 'Agreement number', 'Document Type' which gets generated on the site and is associated to that Member and document. 这些数据类似于“工作流程编号”,“协议编号”,“文档类型”,它在网站上生成并与该成员和文档相关联。

Here is the Code: 这是代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using Microsoft.SharePoint.Client;

namespace DCTMAgent
{
    public class SharePoint
    {               
        internal void SPUploader(Stream fs, string fn)
        {
            ClientContext context = new ClientContext("http://SharepointSite/Home.aspx");

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

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

            string url = "/Members/";
            string fileName = Path.GetFileName(fn);                   

            string fnUrl = url + fn;
            Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, fnUrl, fs, true);           
        }       
    }
}

How can I add metadata to this file being uploaded? 如何向正在上传的文件添加元数据?

Here is my solution: 这是我的解决方案:

    public class SharePoint
        {
            internal void SPUploader(Stream fs, string fn)
             {
                ClientContext context = new ClientContext("http://Sharepointsite");///SitePages/Home.aspx");
                System.Net.ICredentials creds = System.Net.CredentialCache.DefaultCredentials;

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

                string url = "/Members/";
                string fileName = Path.GetFileName(fn);                   

                string fnUrl = url + fn;

                Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, fnUrl, fs, true);

                string uniqueRefNo = GetNextDocRefNo();

                SP.Web web = context.Web;

                Microsoft.SharePoint.Client.File newFile = web.GetFileByServerRelativeUrl(fnUrl);
                context.Load(newFile);
                context.ExecuteQuery();

                Web site = context.Web;
                List docList = site.Lists.GetByTitle("Members");

                context.Load(docList);
                context.ExecuteQuery();


                context.Load(docList.Fields.GetByTitle("Workflow Number"));
                context.Load(docList.Fields.GetByTitle("Agreement Number"));
                context.Load(docList.Fields.GetByTitle("First Name"));
                context.Load(docList.Fields.GetByTitle("Surname"));
                context.Load(docList.Fields.GetByTitle("ID Number"));
                context.Load(docList.Fields.GetByTitle("Date of Birth"));
                context.Load(docList.Fields.GetByTitle("Country"));
                context.Load(docList.Fields.GetByTitle("Document Description"));
                context.Load(docList.Fields.GetByTitle("Document Type"));
                context.Load(docList.Fields.GetByTitle("Document Group"));

                context.ExecuteQuery();                                

*********NEED TO GET THE INTERNAL COLUMN NAMES FROM SHAREPOINT************
                var name = docList.Fields.GetByTitle("Workflow Number").InternalName;
                var name2 = docList.Fields.GetByTitle("Agreement Number").InternalName;
                var name3 = docList.Fields.GetByTitle("First Name").InternalName;
                var name4 = docList.Fields.GetByTitle("Surname").InternalName;
                var name5 = docList.Fields.GetByTitle("ID Number").InternalName;
                var name6 = docList.Fields.GetByTitle("Date of Birth").InternalName;
                var name7 = docList.Fields.GetByTitle("Country").InternalName;
                var name8 = docList.Fields.GetByTitle("Document Description").InternalName;
                var name9 = docList.Fields.GetByTitle("Document Type").InternalName;
                var name10 = docList.Fields.GetByTitle("Document Group").InternalName;
                var name11 = docList.Fields.GetByTitle("Unique Document Ref No").InternalName;     

**********NOW SAVE THE METADATA TO SHAREPOINT**********
                newFile.ListItemAllFields[name] = "927015";
                newFile.ListItemAllFields[name2] = "1234565";
                newFile.ListItemAllFields[name3] = "Joe";
                newFile.ListItemAllFields[name4] = "Soap";
                newFile.ListItemAllFields[name5] = "7502015147852";
                newFile.ListItemAllFields[name6] = "1975-02-01";
                newFile.ListItemAllFields[name7] = "ZA";
                newFile.ListItemAllFields[name8] = "Test";
                newFile.ListItemAllFields[name9] = "Requirements";
                newFile.ListItemAllFields[name10] = "Requirements";
                newFile.ListItemAllFields[name11] = uniqueRefNo;

                newFile.ListItemAllFields.Update();
                context.Load(newFile);
                context.ExecuteQuery();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Sharepoint 2010上传文件 - Sharepoint 2010 Uploading Documents 上传包含元数据的单个 Sharepoint 文档 - Uploading a single Sharepoint document with metadata 使用Metadata C#ASP.NET将文档上传到SharePoint - Upload documents to SharePoint with Metadata c# ASP.NET 使用带有某些元数据的Web服务在SharePoint上以块形式上载文档 - Uploading Document in Chunks on SharePoint using Web Services with some MetaData 在 Sharepoint 服务器中添加/上传/更新文件的问题 - Issue adding/uploading/updating file in Sharepoint server 将文件上传到共享点时访问被拒绝错误 - Access denied error while uploading a file to sharepoint 该操作在使用clientcontext.executequery()上载文档和更新SharePoint库中的元数据时已超时 - The operation has timed out on uploading document and updating metadata in sharepoint library using clientcontext.executequery() 将Excel数据上载到SharePoint 2010上的.Net控件时出现UnauthorizedAccessException - UnauthorizedAccessException while uploading excel Data to .Net control on SharePoint 2010 救命! WebClient.UploadFile()在将文件上传到共享点时引发异常 - HELP! WebClient.UploadFile() throws exception while uploading files to sharepoint 使用C#将文件上传到Sharepoint时添加列值 - Add column values while uploading a file to Sharepoint with C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM