首页 > 解决方案 > 如何使用 Asp.net Core MVC(Missing Namespace for .Net Core)在共享点上创建文档

问题描述

我知道如何使用 c# 创建一个 word 文档我遇到的问题我不知道如何将该文档放在共享点文件夹中。

var newfile = System.IO.Path.Combine(
Directory.GetCurrentDirectory(), "Documents", "filename" + ".docx");

System.IO.File.Create(newfile);

我使用下面的代码来访问共享点,但缺少“SPSite”的名称空间,SPFile 和 SPWeb 已经引用了 Microsoft.SharePoint.Client dll 和 Microsoft.SharePoint dll

#在 Asp.net Core MVC 2.1 上开发

  using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using Sharepoint.Models;
    using Microsoft.SharePoint;
    using System.IO;
    using Microsoft.SharePoint.Client;

 public IActionResult Index()
    {
        String fileToUpload = @"C:\YourFile.txt";
        String sharePointSite = "http://yoursite.com/sites/Research/";
        String documentLibraryName = "Shared Documents";

        using (SPSite oSite = new SPSite(sharePointSite))
        {
            using (SPWeb oWeb = oSite.OpenWeb())
            {
                if (!System.IO.File.Exists(fileToUpload))
                    throw new FileNotFoundException("File not found.", fileToUpload);

                SPFolder myLibrary = oWeb.Folders[documentLibraryName];

                // Prepare to upload
                Boolean replaceExistingFiles = true;
                String fileName = System.IO.Path.GetFileName(fileToUpload);
                FileStream fileStream = File.OpenRead(fileToUpload);

                // Upload document
                SPFile spfile = myLibrary.Files.Add(fileName, fileStream, replaceExistingFiles);

                // Commit 
                myLibrary.Update();
            }
        }

        return View();
    }

标签: c#sharepointms-word

解决方案


推荐阅读