如何自动将文件加载到 SharePoint 中

本文关键字:SharePoint 加载 文件 何自动 | 更新日期: 2023-09-27 18:34:14

我们让某人手动将每周生成的Excel电子表格加载到SharePoint中。 我相信有一种方法可以自动化。 我对 SharePoint 了解不多,也许它真的就像知道 SharePoint 将文件移动到的文件夹并直接将它们复制到那里一样简单。 或者,也许需要一些编程才能使其自动将放置在给定目录中的新文件加载到SharePoint中。

无论哪种方式,我都希望有人在这里为我指出正确的方向。

如何自动将文件加载到 SharePoint 中

您需要

使用 SharePoint 中的复制 Web 服务上载文件。 我不确定您运行的是哪个版本的 SharePoint,但我假设是 2007 年。 下面是一个示例项目。

public void UploadFile(string destinationFolderPath,
                      byte[] fileBytes,
                      string fileName,
                      bool overwrite,
                      string sourceFileUrl,
                      string lastVersionUrl)
{
List<Sharepoint.FieldInformation> fields = new List<Sharepoint.FieldInformation>();
Sharepoint.FieldInformation fieldInfo;
fieldInfo = new Sharepoint.FieldInformation();
fieldInfo.Id = Microsoft.SharePoint.SPBuiltInFieldId.Title;
fieldInfo.Value = "New title";
fieldInfo.DisplayName = "Title";
fieldInfo.Type = YetAnotherMigrationTool.Library.SP2007.Sharepoint.FieldType.Text;
fieldInfo.InternalName = "Title";
fields.Add(fieldInfo);
string[] url;
if (string.IsNullOrEmpty(destinationFolderPath))
    url = new string[] { string.Format("{0}/{1}/{2}", _siteUrl, _name, fileName) };
else
    url = new string[] { string.Format("{0}/{1}/{2}{3}", _siteUrl, _name,    destinationFolderPath, fileName) };
Sharepoint.CopyResult[] result;
Sharepoint.Copy service = new Sharepoint.Copy();
service.Url = _siteUrl + "/_vti_bin/Copy.asmx";
service.Credentials = new NetworkCredential(Settings.Instance.User, Settings.Instance.Password);
service.Timeout = 600000;
uint documentId = service.CopyIntoItems(sourceFileUrl, url, fields.ToArray(), fileBytes, out result);
}
public void SetContentType(List<string> ids, string contentType)
{
ListsService.Lists service = new YetAnotherMigrationTool.Library.SP2007.ListsService.Lists();
service.Url = _siteUrl + "/_vti_bin/Lists.asmx";
service.Credentials = new NetworkCredential(Settings.Instance.User, Settings.Instance.Password);
string strBatch = "";
for (int i = 1; i <= ids.Count; i++)
{
    strBatch += @"<Method ID='"+i.ToString()+@"' Cmd='Update'><Field Name='ID'>" + ids[i-1] + "</Field><Field Name='ContentType'>"+contentType+"</Field></Method>";
}
XmlDocument xmlDoc = new XmlDocument();
XmlElement elBatch = xmlDoc.CreateElement("Batch");
elBatch.SetAttribute("OnError", "Continue");
elBatch.SetAttribute("ListVersion", "10");
elBatch.SetAttribute("ViewName", "");
elBatch.InnerXml = strBatch;
result = service.UpdateListItems(_name, elBatch);
}

您可以编写一个PowerShell脚本,通过WebDav将文档复制到文档库中:

假设您的文档库位于 http://server/SomeWeb/DocumentLibrary/Folder

copy-item somesheet.xlsx ''server'SomeWeb'DocumentLibrary'Folder