如何自动将文件加载到SharePoint

时间:2012-05-15 04:47:22

标签: c# .net sharepoint

我们正在让某人手动将每周生成的Excel电子表格加载到SharePoint中。我确信有一种方法可以实现自动化。我不太了解SharePoint,也许它只是简单地知道SharePoint正在将文件移动到那里并在那里直接复制它们。或者它可能需要一些编程才能让它自动将放在给定目录中的新文件加载到SharePoint中。

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

2 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:2)

您需要使用SharePoint中的复制Web服务上载文件。我不确定您正在运行的是什么版本的SharePoint,但我假设2007.这是一个示例project

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