gpt4 book ai didi

sharepoint - 如何以编程方式将文档上传到 SharePoint?

转载 作者:行者123 更新时间:2023-12-03 12:40:40 24 4
gpt4 key购买 nike

我想以编程方式将文档从 Web 门户上传到 SharePoint。也就是说,当用户上传文档时,它应该直接进入 SharePoint。我是 SharePoint 的新手,正在寻找有关如何实现上述目标的建议/想法。谢谢

最佳答案

您有多种上传文档的方式,具体取决于您运行代码的位置。步骤几乎相同。

从服务器对象模型

如果您在 SharePoint 服务器端(Web 部件、事件接收器、应用程序页面等)工作,请使用此选项

// Get the context
var context = SPContext.Current;

// Get the web reference
var web = context.Web;

// Get the library reference
var docLib = web.Lists.TryGetList("NAME OF THE LIBRARY HERE");
if (docLib == null)
{
return;
}

// Add the document. Y asume you have the FileStream somewhere
docLib.RootFolder.Files.Add(docLib.RootFolder.Url + "FILE NAME HERE", someFileStream);

从客户端代码 (C#)

如果您正在使用使用 SharePoint 服务的客户端应用程序工作,请使用此选项。
// Get the SharePoint context
ClientContext context = new ClientContext("URL OF THE SHAREPOINT SITE");

// Open the web
var web = context.Web;

// Create the new file
var newFile = new FileCreationInformation();
newFile.Content = System.IO.File.ReadAllBytes("PATH TO YOUR FILE");
newFile.Url = "NAME OF THE NEW FILE";

// Get a reference to the document library
var docs = web.Lists.GetByTitle("NAME OF THE LIBRARY");
var uploadFile = docs.RootFolder.Files.Add(newFile);

// Upload the document
context.Load(uploadFile);
context.ExecuteQuery();

从 JS 使用 SharePoint Web 服务

如果您想从没有服务器往返的页面上传文档,请使用此方法:
// Get the SharePoint current Context
clientContext = new SP.ClientContext.get_current();

// Get the web reference
spWeb = clientContext.get_web();

// Get the target list
spList = spWeb.get_lists().getByTitle("NAME OF THE LIST HERE");


fileCreateInfo = new SP.FileCreationInformation();

// The title of the document
fileCreateInfo.set_url("my new file.txt");

// You should populate the content after this
fileCreateInfo.set_content(new SP.Base64EncodedByteArray());

// Add the document to the root folder of the list
this.newFile = spList.get_rootFolder().get_files().add(fileCreateInfo);

// Load the query to the context and execute it (successHandler and errorHandler handle result)
clientContext.load(this.newFile);
clientContext.executeQueryAsync(
Function.createDelegate(this, successHandler),
Function.createDelegate(this, errorHandler)
);

希望能帮助到你!

关于sharepoint - 如何以编程方式将文档上传到 SharePoint?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21508773/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com