【问题标题】:Upload documents to SharePoint with Metadata c# ASP.NET使用元数据 c# ASP.NET 将文档上传到 SharePoint
【发布时间】:2012-09-25 20:07:18
【问题描述】:

我正在开发一个使用 Web 服务将文档上传到 Sharepoint 的网站。我有一个可以工作并上传到 Sharepoint 的网络服务。但是,我需要将元数据添加到正在上传的文件中,例如数据库记录中的名字、姓氏、出生日期等,以及来自站点的实时数据。这些数据是在网站上生成并与该成员和文档相关联的“工作流程编号”、“协议编号”、“文档类型”之类的东西。

代码如下:

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

如何更改此设置以将元数据与文档一起上传?

【问题讨论】:

    标签: c# asp.net file-upload sharepoint-2010 metadata


    【解决方案1】:

    这是我的解决方案:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-06
      • 2014-11-06
      • 2010-09-23
      • 2010-10-04
      相关资源
      最近更新 更多